我自己写的一个图形学的程序,是关于weiler-Atherton算法裁减多边形的程序,有问题,特别是在后面主程序的方面,有一点问题,哪位朋友可以帮助改改啊?
#include
const int M=200;
const int N=200;
struct point
{int x;
int y;
int flag; /*点出入标志,0为原多边形顶点,1为入点,-1为出点*/
}joinpoit[N],point1[M],point2[M],point3[N],point4[N],S,Q[N];
void InitGraph();
//初始化窗口
void InputWindows(point,point);
//初始化进行输入裁减多边形和被裁减多边形。
void DrawPoly(int,point);
//画多边形带出入标志
void DrawPoly(int,point);
//画多边形
void InsertNewLine(point,point,point,point);
//把交点插入到P3和P4交点序列里。
int IsInside(point,int,int);
//判断点是否在被裁减多边形内。
int GetJoin(int,int,int,int,int,int,int,int,int,int);
//判断两条线段是否有交点。
int GetJoin(int,int,int,int,int,int,int,int,int,int,int);
//求两条线段的交点和出入标志
void main()
{
int i;
InitGraph();
InputWindows(point1,point2);
InsertNewLine(point1,point2,point3,point4);
}
void InitGraph()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"");
}
void InputWindows(point P1[],point P2[])
{
int i,s,a;
cout<<"请输入被裁减多边形的顶点数: ";
cin>>P1[0].x;
cout<<"\n请输入裁减多边形的顶点数: ";
cin>>P2[0].x;
for(i=1;i<=P1[0].x;i++)
{
cout<<"\n请输入被裁减多边形的第 "< cin>>P1[i].x>>P1[i].y;
P1[i].flag=0;
}
P1[P1[0].x+1].x=P1[1].x;
P1[P1[0].x+1].y=P1[1].y;
P1[P1[0].x+1].flag=P1[1].flag;
P1[0].x++;
for(i=1;i<=P2[0].x;i++)
{
cout<<"\n请输入裁减多边形的第 "< cin>>P2[i].x>>P2[i].y;
P2[i].flag=0;
}
P2[P2[0].x+1].x=P2[1].x;
P2[P2[0].x+1].y=P2[1].y;
P2[P2[0].x+1].flag=P2[1].flag;
P2[0].x++;
}
void DrawPoly(point A[])
{
int i,n;
n=A[0].x;
if(n>2)
{
for(i=1;i {
line(A[i].x,A[i].y,A[i+1].x,A[i+1].y);
}
}
else printf("\nCan not draw the polygon\n");
}
int BothSideOrInLine(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3)
{
int k,delt1,delt2;
k=(y3-y2)/(x3-x2);
delt1=k*(x0-x2)+y2-y0;
delt2=k*(x1-x2)+y2-y1;
if(delt1*delt2<0)
return(0);
else if(delt1==0)
{
return(1);
}
else if(delt2==0)
{
return(2);
}
return(-1);
}
int IsJoin(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3)
//该函数调用上面的函数BothSideOrInLine来判断两个线段是否有交点的
{
if ((BothSideRoInLine(x0,y0,x1,y1,x2,y2,x3,y3)>=0)&&(BothSideRoInLine(x2,y2,x3,y3,x0,y0,x1,y1)>=0)
return 1;
else return 0;
}
int IsInside(pointedge edge[],int xIN,int yIN)
{
int x,y,xmax=edge[1].x;
int i,j,size=edge[0].x;
int flag=1;
for (i=1;i<=size;i++)
{
if(xmax cout<<"xmax is "<
for(i=1;i {
if(IsJoin(x0,y0,xmax,y0,edge[i].x,edge[i].y,edge[i+1].x,edge[i+1].y,x,y))
flag=-flag;
}
}
if(flag==1)return 0;
else return 1;
}
int GetJoin(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3,int &x,int &y,int &flag)
{
double m,n;
if((y1==y0 && y3==y2) || (x1==x0 && x3==x2) ||((y1-y0)/(x1-x0) == (y3-y2)))return 0;
//问题:如果两条直线重合呢?
if(y1==y0 && x3==x2)
{
x=x2;
y=y0;
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
};
if(y3==y2 && x1==x0)
{
x=x0;
y=y2;
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
};
if(y1==y0)
{
n=(y3-y2)/(x3-x2);
y=y0;
x=(y-y2)/m+x2;
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
};
if(x1==x0)
{
n=(y3-y2)/(x3-x2);
x=x0;y=n(x-x0)+y2;
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
};
if(y3==y2)
{
m=(y1-y0)/(x3-x2);
y=y2;x=(y-y0)/m+x0;
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
};
if(x3==x2)
{
m=(y1-y0)/(x3-x2);
x=x2;y=m(x-x2)+y0;
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
};
else
{
m=(y1-y0)/(x3-x2);n=(y3-y2)/(x3-x2);
x=(m*x0-y0-(n*x2-y2))/)m-n);
y=(m*n*(x0-x2)-n*y0+m*y2)/(m-n);
if(IsInside(point2,x0,y0)&&!IsInside(point2,x1,y1)) flag=-1;
else
{
if(!IsInside(point2,x0,y0)&&IsInside(point2,x1,y1)) flag=1;
};
return 1;
}
}
void InsertNewLine(point *p1,point *p2,point *p3,point *p4)
{
int i,j,Jx,Jy,Jflag,count=0;
p3[count].x=0;
count++;
for(i=1;i
{
p3[count].x=p1[i].x;p3[count].y=p1[i].y;p3[count].flag=p1[i].flag;
p3[0].x++;count++;
for(j=1;j {
if(GetJoin(p1[i].x,p1[i].y,p1[i+1].x,p1[i+1].y,p2[j].x,p2[j].y,p2[j+1].x,p2[j+1].y,Jx,Jy,Jflag))
{
if((Jx==p3[count-1].x)&&(Jy==p3[count-1].y))
continue; //如果这个交点和前一个交点相同的话,就不插入继续。
p3[count].x=Jx;p3[i].y=Jy;p3[i].flag=Jflag;
p3[0].x++;
count++;
}
}
}
count=0;
p4[count].x=0;
count++;
for(i=1;i {
p4[count].x=p2[i].x;p4[count].y=p2[i].y;p4[count].flag=p2[i].flag;
p4[0].x++;count++;
for(j=1;j {
if(GetJoin(p1[j].x,p1[j].y,p1[j+1].x,p1[j+1].y,p2[i].x,p2[i].y,p2[i+1].x,p2[i+1].y,Jx,Jy,Jflag))
{
if((Jx==p4[count-1].x)&&(J==p4[count-1].y))
continue; //如果这个交点和前一个交点相同的话,就不插入继续。
p4[count].x=Jx;p4[i].y=Jy;p4[i].flag=Jflag;
p4[0].x++;
count++;
}
}
}
}
void Clip()
{
int count=1;
Q[0].x=0;
int i=1,CC=1;
Q3: if(i<=p3[0].x)
{
if(p3[i].flag==1)
{
if(CC==1)
{
S.x=p3[i].x;S.y=p3[i].y;S.flag=p3[i].flag;
CC=-CC;
}
Q[count].x=p3[i].x;
Q[count].y=p3[i].y;
Q[count].flag=p3[i].flag;
Q[0].x++;count++;
p3[i].flag=0;
i++;
}
}
else goto
comeon: if(p3[i].flag!=-1)
{
Q[count].x=p3[i].x;
Q[count].y=p3[i].y;
Q[count].flag=p3[i].flag;
Q[0].x++;count++;
p3[i].flag=0;
i++;
}
else goto Q4;
goto comeon;
Q4: if(p4[j].flag!=1)
{
Q[count].x=p4[j].x;
Q[count].y=p4[j].y;
Q[count].flag=p4[j].flag;
Q[0].x++;count++;
p4[j].flag=0;
j++;
}
else goto Next;
goto Q4;
Next: if(Q[coun-1].x!=S.x||Q[coun-1].y!=S.y||Q[coun-1].flag!=S.flag)
goto Q3;
else Drawpoly(Q);
goto Q3;
End: Drawpoly(Q);
void Clip2(point Q[],point p4[],int & count)
{
}
if(S.flag!=1)return 0;
}
int BothSideOrInLine(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3)
{
int k,delt1,delt2;
k=(y3-y2)/(x3-x2);
delt1=k*(x0-x2)+y2-y0;
delt2=k*(x1-x2)+y2-y1;
if(delt1*delt2<0)
return(0);
else if(delt1==0)
{
return(1);
}
else if(delt2==0)
{
return(2);
}
return(-1);
}
int IsJoin(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3)
{
if ((BothSideRoInLine(x0,y0,x1,y1,x2,y2,x3,y3)>=0)&&(BothSideRoInLine(x2,y2,x3,y3,x0,y0,x1,y1)>=0)
return 1;
else return 0;
}
goto
int GetJoin(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3,int &x,int &y)
{
double m,n;
if((y1==y0 && y3==y2) || (x1==x0 && x3==x2) ||((y1-y0)/(x1-x0) == (y3-y2)))return 0;
//问题:如果两条直线重合呢?
if(y1==y0 && x3==x2){x=x2; y=y0; if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;};
if(y3==y2 && x1==x0){x=x0; y=y2; if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;};
if(y1==y0){n=(y3-y2)/(x3-x2);y=y0;x=(y-y2)/m+x2;if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;};
if(x1==x0){n=(y3-y2)/(x3-x2);x=x0;y=n(x-x0)+y2; if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;};
if(y3==y2){m=(y1-y0)/(x3-x2);y=y2;x=(y-y0)/m+x0;if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;};
if(x3==x2){m=(y1-y0)/(x3-x2);x=x2;y=m(x-x2)+y0; if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;};
else{
m=(y1-y0)/(x3-x2);n=(y3-y2)/(x3-x2);
x=(m*x0-y0-(n*x2-y2))/)m-n);
y=(m*n*(x0-x2)-n*y0+m*y2)/(m-n);
if((x>=x0&&x<=x1)&&(x>=x2&&x<=x2)) return 1;else return 0;
}
}