Cohen–Sutherland算法

之前写文章想插代码时,各种不爽,都想换地方写了,然后发现原来这里支持markdown,所以入坑了,相关介绍如下:
简书markdown

简介

Cohen–Sutherland是一个线段裁剪算法

原理

将窗口区域分为9个部分,每个部分给一个区域码,然后计算线段两端端点的区域码,根据区域码来选择抛弃线段

Paste_Image.png
  • 两端点都在视口区域内,区域码相或为0,接受
  • 两端点至少共享一个不可见区域,区域码相与不为1,拒绝

实现

typedef int OutCode;

const int INSIDE = 0; // 0000
const int LEFT = 1;   // 0001
const int RIGHT = 2;  // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8;    // 1000

// Compute the bit code for a point (x, y) using the clip rectangle
// bounded diagonally by (xmin, ymin), and (xmax, ymax)

// ASSUME THAT xmax, xmin, ymax and ymin are global constants.
//判断端点的区域码
OutCode ComputeOutCode(double x, double y)
{
    OutCode code;
    code = INSIDE;          // initialised as being inside of clip window

    if (x < xmin)           // to the left of clip window
        code |= LEFT;
    else if (x > xmax)      // to the right of clip window
        code |= RIGHT;
    if (y < ymin)           // below the clip window
        code |= BOTTOM;
    else if (y > ymax)      // above the clip window
        code |= TOP;

    return code;
}
// Cohen–Sutherland clipping algorithm clips a line from
// P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with 
// diagonal from (xmin, ymin) to (xmax, ymax).
void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1)
{
    // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle
    OutCode outcode0 = ComputeOutCode(x0, y0);
    OutCode outcode1 = ComputeOutCode(x1, y1);
    bool accept = false;

    while (true) {
        if (!(outcode0 | outcode1)) { //相或为0,接受并且退出循环
            accept = true;
            break;
        } else if (outcode0 & outcode1) { // 相与为1,拒绝且退出循环
            break;
        } else {
            // failed both tests, so calculate the line segment to clip
            // from an outside point to an intersection with clip edge
            double x, y;

            //找出在界外的点
            OutCode outcodeOut = outcode0 ? outcode0 : outcode1;

            // 找出和边界相交的点
            // 使用点斜式 y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
            if (outcodeOut & TOP) {           // point is above the clip rectangle
                x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
                y = ymax;
            } else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
                x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
                y = ymin;
            } else if (outcodeOut & RIGHT) {  // point is to the right of clip rectangle
                y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
                x = xmax;
            } else if (outcodeOut & LEFT) {   // point is to the left of clip rectangle
                y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
                x = xmin;
            }

            // Now we move outside point to intersection point to clip
            // 为什么继续循环,两个端点都有可能在外面
            if (outcodeOut == outcode0) {
                x0 = x;
                y0 = y;
                outcode0 = ComputeOutCode(x0, y0);
            } else {
                x1 = x;
                y1 = y;
                outcode1 = ComputeOutCode(x1, y1);
            }
        }
    }
    if (accept) {
               // Following functions are left for implementation by user based on
               // their platform (OpenGL/graphics.h etc.)
               DrawRectangle(xmin, ymin, xmax, ymax);
               LineSegment(x0, y0, x1, y1);
    }
}

其他

在计算交点的时候,之前有这样的做法:求两点的中点,然后用中点和另一个点的位运算判断,然后再用中点和另一个点位运算判断,一直这样下去,直到中点区域交点。这样主要是效率考虑:以前cpu做位运算速度高于乘法除法等,乘法就是加法的累积,所以求中点(右移)比点斜式的计算快。
我在VS上做了测试,却发现加减乘除的速度都几乎一致

#include "stdafx.h"
#include <iostream>
#include <time.h>

int _tmain(int argc, _TCHAR* argv[])
{
    
    int wait;
    int times = 100000000;
    float res = 0;
    clock_t nowtime, endtime;
    nowtime = clock();
    while(times--)
    {     
        res = 201+311;//这里改成加减乘除结构几乎一致
    }
    endtime = clock();
    std::cout<<"the cal is spend " << (endtime - nowtime)<< " mili seconds" << nowtime<<std::endl<<endtime;

    std::cin>>wait;
    return 0;
}

网上查找相关资料后,应该是现在的cpu已经有乘法器等相关硬件支持,而且编辑器也足够先进,所以加减乘除速度几乎没什么区别了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容