老师叫我们做几个题目,但是裁剪我不会做
哪个好心人有源码的帮帮
线段裁剪算法描述:
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int encode(int x,int y,int xmin,int xmax,int ymin,int ymax)
{ int c=0;
if(x<xmin) c |= LEFT;
if(x>xmax) c |= RIGHT;
if(y<ymin) c |= BOTTOM;
if(y>ymax) c |= TOP;
retrun c;
void CS_LineClip(int x1,int y1,int x2,int y2,int xmin,int xmax,int ymin ,int ymax)
{ int code1,code2,code;
code1=encode(x1,y1);
code2=encode(x2,y2);
}
while(code1!=0 ||code2!=0)
{ if(code1&code2 !=0) return;
code = code1;
if(code1==0) code = code2;
if(LEFT&code !=0)
{ x=xmin;
y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
}
else if(RIGHT&code !=0)
{ x=xmax;
y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
}
else if(BOTTOM&code !=0)
{ y=ymin;
x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
}
else if(TOP & code !=0)
{ y=ymax;
x=x1+(x2-x1)*(ymax-y1)/(y2-y1);
}
if(code ==code1)
{ x1=x; y1=y; code1 =encode(x,y);}
else
{ x2=x; y2=y; code2 =encode(x,y);}
} //end of while
displayline(x1,y1,x2,y2);
}//end of CS_LineClip
(希望大虾们帮忙加做做,再过两天就要交了。急啊!)