|
算法的程序实现如下:
函数Cohen_Sutherland用来实现算法 函数makecode用来编码,利用数值位运算
double xl, xr, yt, yb;(事先给出窗口的位置,四个数值是已知的)
void Cohen_Sutherland(double x0, y0, x2, y2)
{ int c, c1, c2; double
x, y; makecode(x0, y0,c1); makecode(x2,
y2, c2); while (c1!=0 || c2!=0)
{ if
(c1&c2!=0) return; c=c1;if
(c==0) c=c2; if
(c&1==1) {y=y0+(y2-y0)*(x1-x0)/(x2-x0);x=x1;} else
if (c&2==2) {y=y0+(y2-y0)*(xr-x0)/(x2-x0); x=xr;} else
if (c&4==4) {x=x0+(x2-x0)*(yb-y0)/(y2-y0); y=yb;} else
if (c&8==8) {x=x0+(x2-x0)*(yt-y0)/(y2-y0); y=yt;} if
(c==c1) {x0=x;
y0=y; makecode(x, y, c1);} else
{x2=x; y2=y;
makecode(x, y, c2);} }
showline(x0, y0, x2, y2); //显示可见线段}
void makecode(double x, y;int c)
{ c=0; if
(x<xl) c=1; else if (x>xr)
c=2; if (y<yb) c=c+4; else
if (y>yt) c=c+8;
} |
|
|