📄 单边裁剪.txt
字号:
单边裁剪算法:
#include<graphics.h>
#define smoveto(x,y) moveto(x0+x/3,y0-y/3)
#define slineto(x,y) lineto(x0+x/3,y0-y/3)
#define pi 3.1415926
#define DIM 8
float xx,yy,kx=12.0,ky=12.0,startx,starty,k;
int ii,ixx,iyy,ix,iy,x0=30,y0=400,istart;
char axname[2];
float xll,yll,xur,yur;
int dx[5]={0,0,450,900,1350},dy[5]={600,0,0,0,0};
main()
{
int code();
float clip(float, float, float, float);
int gd=DETECT,gm,ierror,i,j;
int ix1,iy1,ixr,iyr;
float x[DIM]={10.0,10.0,0.0,17.5,35.0,25.0,25.0,10.0};
float y[DIM]={0.0,25.0,25.0,40.0,25.0,25.0,0.0,0.0};
intigraph(&gd,&gm,”d:\\tc);
ierror=graphresult();
if(ierror!=0)
{
printf(“graphics error:%s\n”,grapherrormsg(ierror));
printf(“press any key to halt.”);
}
k=1.0;
for(i=0;i<5;i++)
{
ii=i;
istart=0;
switch(i)
{
case 0: xll=0.0;yll=0.0;
xur=35.0;yur=40.0;
break;
case 1: xll=0.0;yll=0.0;
xur=35.0;yur=35.0;
break;
case 2: xll=0.0;yll=0.0;
xur=30.0;
break;
case 3: xll=0.0;yll=0.0;
break;
case 4: xll=5.0;
break;
default:break;
}
tran2(x[0],y[0]);
smoveto(ix,iy);
for(j=1;j<DIM;j++)
clip(x[j-1],y[j-1],x[j],y[j]);
tran2(startx,starty);
slineto(ix,iy);
setfillstyle(2,1);
tran2(17.5,20.0);
ixx=x0+ix/3;
iyy=y0-iy/3;
floodfill(ixx,iyy,getcolor());
tran2(5.0,10.0);
ix1=ix;
iy1=iy;
slineto(ixr,iyl);
slineto(ixr,iyr);
slineto(ixl,iyr);
slineto(ixl,iyl);
}
getch();
closegraph();
}
int tran2(tx,ty)
float tx,ty;
{
ix=kx*tx+dx[ii];
iy=ky*ty+dy[ii];
}
float clip(float x1, float y1, float x2, float y2)
{
unsigned char code1,code2;
int itt,i;
float tleft,tright,tuper,tlow,xc[2],yc[2],dist[2];
code1=code(x1,y1);
code2=code(x2,y2);
if((code1&&code2)!=0) return;
istart=istart+1;
if((code1||code2)==0)
{
xc[0]=x1;
yc[0]=y1;
xc[1]=x2;
yc[1]=y2;
itt=2;
}
else
{
itt=0;
if((x1!=x2)&&(y1!=y2))
{
tleft=(xll-x1)/(x2-x1);
tright=(xur-x1)/(x2-x1);
tuper=(yur -y1)/(y2-y1);
tlow=(yll-y1)/(y2-y1);
if((tleft>=0)&&(tleft<=1.0))
{
yy=y1+(y2-y1)*tleft;
if((yy>=yll)&&(yy<=yur))
{
xc[itt]=xll;
yc[itt]=yy;
itt=itt+1;
}
}
if((tright>=0)&&(tright<=1.0))
{
yy=y1+(y2-y1)*tleft;
if((yy>=yll)&&(yy<=yur))
{
xc[itt]=xur;
yc[itt]=yy;
itt=itt+1;
}
}
if((tuper>=0)&&(tuper<=1.0))
{
xx=x1+(x2-x1)*tuper;
if((xx>=xll)&&(xx<=xur))
{
xc[itt]=xx;
yc[itt]=yur;
itt=itt+1;
}
}
if((tlow>=0)&&(tlow<=1.0))
{
xx=x1+(x2-x1)*tlow;
if((xx>=xll)&&(xx<=xur))
{
xc[itt]=xx;
yc[itt]=yll;
itt=itt+1;
}
}
}
if((x1==x2)&&(y1!=y2))
{
xc[0]=x1;
xc[1]=x1;
tuper=(yur-y1)/(y2-y1);
tlow=(yll-y1)/(y2-y1);
if((tuper>=0)&&(tuper<=1.0))
{
yc[itt]=yur;
itt=itt+1;
}
if((tlow>=0)&&(tlow<=1.0))
{
yc[itt]=yll;
itt=itt+1;
}
}
if((x1!=x2)&&(y1==y2))
{
yc[0]=y1;
yc[1]=y1;
tleft=(xll-x1)/(x2-x1);
tright=(xur-x1)/(x2-x1);
if((tleft>=0)&&(tleft<=1.0))
{
xc[itt]=xll;
itt=itt+1;
}
if((tright>=0)&&(tright<=1.0))
{
xc[itt]=xur;
itt=itt+1;
}
}
}
if(itt==2)
{
for(i=0;i<2;i++)
dist[i]=(xc[i]-x1)* (xc[i]-x1)*(yc[i]-y1)* (yc[i]-y1);
if(dist[1]<dist[0]){
xx=xc[0];
xc[0]=xc[1];
xc[1]=xx;
yy=yc[0];
yc[0]=yc[1];
yc[1]=yy;
}
}
if((code1==0)&&(itt==1))
{
xc[1]=xc[0];
xc[0]=x1;
yc[1]=yc[0];
yc[0]=y1;
itt=2;
}
if((code2==0)&&(itt==1))
{
xc[1]=x2;
yc[1]=y2;
itt=2;
}
for(i=0;i<2;i++)
{
tran2(xc[i],yc[i]);
if(i==0&&istart==1)
{
startx=xc[0];
starty=yc[0];
smoveto(ix,iy);
}
else
{
slineto(ix,iy);
}
}
}
int code(float x,float y)
{
unsigned char icode;
icode=0;
if(y>yur)
icode=8+icode;
if(y<yll)
icode=4+icode;
if(x>xur)
icode=2+icode;
if(x<xll)
icode=1+icode;
return icode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -