-
大小: 74KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-05-13
- 语言: C/C++
- 标签: CohenSutherl 中点分割 裁剪算法
资源简介
可运行。环境是vc++6.0 。请看txt必读。谢谢
代码片段和文件信息
#include
#include
#include
#include
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
typedef struct point
{
double x;
double y;
point(double x1double y1)
{
x=x1;
y=y1;
}
}Point;
int Encode(double xdouble ydouble XLdouble XRdouble YBdouble YT)
{
int Ncode=0;
if(x else if(x>XR)Ncode=Ncode|RIGHT;
if(y>YB)Ncode=Ncode|BOTTOM;
else if(y return Ncode;
}
int CohenSutherlandClip(Point P0Point P1double XLdouble XRdouble YBdouble YT)
{
double x0x1y0y1xy;
int code0code1code;
x0=P0.x;x1=P1.x;y0=P0.y;y1=P1.y;
code0=Encode(x0y0XLXRYBYT);
code1=Encode(x1y1XLXRYBYT);
while(code0!=0||code1!=0)
{
if((code0&code1)!=0)
{
return 0;
}
code=code0;
if(code0==0)
{
code=code1;
}
if((LEFT&code)!=0)
{
x=XL;
y=y0+(y1-y0)*(XL-x0)/(x1-x0);
}
else if((RIGHT&code)!=0)
{
x=XR;
y=y0+(y1-y0)*(XR-x0)/(x1-x0);
}
else if((BOTTOM&code)!=0)
{
y=YB;
x=x0+(x1-x0)*(YB-y0)/(y1-y0);
}
else if((TOP&code)!=0)
{
x=YT;
x=x0+(x1-x0)*(YT-y0)/(y1-y0);
}
if(code==code0)
{
x0=x;
y0=y;
code0=Encode(xyXLXRYBYT);
}
else
{
x1=x;
y1=y;
code1=Encode(xyXLXRYBYT);
}
}
line(int(x0+0.5)int(y0+0.5)int(x1+0.5)int(y1+0.5));
return 1;
}
//中点分割法裁剪算法
int MidCut(Point P0Point P1double XLdouble XRdouble YBdouble YT)
{
double x0x1y0y1xy;
int code0code1;
x0=P0.x;x1=P1.x;y0=P0.y;y1=P1.y;
x=(x0+x1)/2.0;
y=(y0+y1)/2.0;
code0=Encode(x0y0XLXRYBYT);
code1=Encode(x1y1XLXRYBYT);
if((code0&code1)!=0)
{
//在矩形外面
return 0;
}
else
{
if(code0==0 && code1==0)
{
line(int(x0)int(y0)int(x1)int(y1));
return 1;
}
else
{
if((pow(x2)+pow(y2)) - (pow(x02) + pow(y02)) < 10e-6)
{
return 1;
}
else
{
MidCut(Point(x0y0)Point(xy)XLXRYBYT);
MidCut(Point(xy)Point(x1y1)XLXRYBYT);
}
}
}
return 1;
}
void main()
{
initgraph(640 480);
Point p0(11)p1(200200)p2(100200)p3(200100);
//原来显示的图形是
line(p0.xp0.yp1.xp1.y);
line(p0.xp0.yp2.xp2.y);
line(p0.xp0.yp3.xp3.y);
Sleep(3000); //为了显示效果,停顿3秒
cleardevice(); //为了显示效果 清屏
//CohenSutherland裁剪后的图形显示
// CohenSutherlandClip(p0p13010010010);
// CohenSutherlandClip(p0p23010010010);
// CohenSutherlandClip(p0p33010010010);
//利用中点分割算法裁剪后的图形显示
MidCut(p0p13010010010);
MidCut(p0p23010010010);
MidCut(p0p33010010010);
getch();
closegraph();
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 281 2009-11-02 07:06 必读.txt
文件 75226 2009-10-29 21:48 VCBGI-20090909.zip
文件 2803 2009-11-01 13:24 Cut.cpp
----------- --------- ---------- ----- ----
78310 3
评论
共有 条评论