凸多边形求并的简单算法
求两个凸多边形相交之后的外边界, 就是多边形求并, 以下是c++源码仅供参考
求两个凸多边形相交之后的外边界, 就是多边形求并, 以下是c++源码仅供参考
bool MergePolygon(vector<Vertex2d> &Polygon1,vector<Vertex2d> &Polygon2,vector<Vertex2d> &ResultPolygon)
{
{
Vertex2d tempPoint1;
Vertex2d tempPoint2;
Polygon1.push_back(Polygon1[0]);
Polygon2.push_back(Polygon2[0]);
Polygon2.push_back(Polygon2[0]);
int m,n;
n = Polygon1.size()-1;
m = Polygon2.size()-1;
int flag;
flag=0;
vector<Vertex2d> tempPolygon1;
vector<Vertex2d> tempPolygon2;
n = Polygon1.size()-1;
m = Polygon2.size()-1;
int flag;
flag=0;
vector<Vertex2d> tempPolygon1;
vector<Vertex2d> tempPolygon2;
vector<Vertex2d> temp_Polygon1;
vector<Vertex2d> temp_Polygon2;
vector<Vertex2d> temp_Polygon2;
//求得tempPolygon1的过程
// for(vector<Vertex2d>::iterator iter1=Polygon1.begin();iter1!=Polygon1.end();++iter1)
for(int i=0;i<n;i++)
{
if (!PointInPolygon(Polygon1[i],Polygon2))
{
bool boo = false;
if (i==0)
{
boo = PointInPolygon(Polygon1[n-1],Polygon2);
}
if ((flag==1)||(boo==true))
{
for(int j=0;j<m;j++)
{
Vector line1,line2;
if (i==0)
{
line1.begin = Polygon1[n-1];
}
else
{
line1.begin = Polygon1[i-1];
}
line1.end = Polygon1[i];
// for(vector<Vertex2d>::iterator iter1=Polygon1.begin();iter1!=Polygon1.end();++iter1)
for(int i=0;i<n;i++)
{
if (!PointInPolygon(Polygon1[i],Polygon2))
{
bool boo = false;
if (i==0)
{
boo = PointInPolygon(Polygon1[n-1],Polygon2);
}
if ((flag==1)||(boo==true))
{
for(int j=0;j<m;j++)
{
Vector line1,line2;
if (i==0)
{
line1.begin = Polygon1[n-1];
}
else
{
line1.begin = Polygon1[i-1];
}
line1.end = Polygon1[i];
line2.begin = Polygon2[j];
line2.end = Polygon2[j+1];
line2.end = Polygon2[j+1];
if (Across(line1,line2) == 1)
{
flag = 1;
tempPoint1 = CrossPoint( line1,line2);
tempPolygon1.push_back(tempPoint1);
}
}
flag = 0;
}
tempPolygon1.push_back(Polygon1[i]);
}
else
{
for(int j=0;j<m;j++)
{
Vector line1,line2;
if (i==0)
{
line1.begin = Polygon1[n-1];
}
else
{
line1.begin = Polygon1[i-1];
}
line1.end = Polygon1[i];
{
flag = 1;
tempPoint1 = CrossPoint( line1,line2);
tempPolygon1.push_back(tempPoint1);
}
}
flag = 0;
}
tempPolygon1.push_back(Polygon1[i]);
}
else
{
for(int j=0;j<m;j++)
{
Vector line1,line2;
if (i==0)
{
line1.begin = Polygon1[n-1];
}
else
{
line1.begin = Polygon1[i-1];
}
line1.end = Polygon1[i];
line2.begin = Polygon2[j];
line2.end = Polygon2[j+1];
line2.end = Polygon2[j+1];
if (Across(line1,line2) == 1)
{
tempPoint1 = CrossPoint( line1,line2);
tempPolygon1.push_back(tempPoint1);
flag = 1;
{
tempPoint1 = CrossPoint( line1,line2);
tempPolygon1.push_back(tempPoint1);
flag = 1;
}
}
}
}
}
}
//求得tempPolygon2的过程
// for(vector<Vertex2d>::iterator iter2=Polygon2.begin();iter2!=Polygon2.end();++iter2)
for(int j=0;j<n;j++)
{
if (!PointInPolygon(Polygon2[j],Polygon1))
{
if (flag == 1)
{
for(int i=0;i<m;i++)
{
Vector line1,line2;
if (j==0)
{
line2.begin = Polygon2[m-1];
}
else
{
line2.begin = Polygon2[j-1];
}
line2.end = Polygon2[j];
// for(vector<Vertex2d>::iterator iter2=Polygon2.begin();iter2!=Polygon2.end();++iter2)
for(int j=0;j<n;j++)
{
if (!PointInPolygon(Polygon2[j],Polygon1))
{
if (flag == 1)
{
for(int i=0;i<m;i++)
{
Vector line1,line2;
if (j==0)
{
line2.begin = Polygon2[m-1];
}
else
{
line2.begin = Polygon2[j-1];
}
line2.end = Polygon2[j];
line1.begin = Polygon1[i];
line1.end = Polygon1[i+1];
line1.end = Polygon1[i+1];
if (Across(line1,line2) == 1)
{
tempPoint2 = CrossPoint(line1,line2);
tempPolygon2.push_back(tempPoint2);
}
}
flag = 0;
}
{
tempPoint2 = CrossPoint(line1,line2);
tempPolygon2.push_back(tempPoint2);
}
}
flag = 0;
}
tempPolygon2.push_back(Polygon2[j]);
}
else
{
// for(vector<Vertex2d>::iterator iter1 = Polygon1.begin();iter1 != Polygon1.end();++iter1)
for(int i=0;i<m;i++)
{
Vector line1,line2;
if (j==0)
{
line2.begin = Polygon2[m-1];
}
else
{
line2.begin = Polygon2[j-1];
}
line2.end = Polygon2[j];
}
else
{
// for(vector<Vertex2d>::iterator iter1 = Polygon1.begin();iter1 != Polygon1.end();++iter1)
for(int i=0;i<m;i++)
{
Vector line1,line2;
if (j==0)
{
line2.begin = Polygon2[m-1];
}
else
{
line2.begin = Polygon2[j-1];
}
line2.end = Polygon2[j];
line1.begin = Polygon1[i];
line1.end = Polygon1[i+1];
line1.end = Polygon1[i+1];
if (Across(line1,line2) == 1)
{
tempPoint2 = CrossPoint(line1,line2);
tempPolygon2.push_back(tempPoint2);
{
tempPoint2 = CrossPoint(line1,line2);
tempPolygon2.push_back(tempPoint2);
}
}
flag =1;
}
}
}
//合并tempPolygon1和tempPolygon2
if (tempPoint1 == tempPoint2)
{
if (tempPoint1 == tempPoint2)
{
// vector<Vertex2d> temp_Polygon1;
// vector<Vertex2d> temp_Polygon2;
int p,q;
p= tempPolygon1.size();
q= tempPolygon2.size();
// vector<Vertex2d> temp_Polygon2;
int p,q;
p= tempPolygon1.size();
q= tempPolygon2.size();
//重新排列多边形顶点顺序
for (int s=0;s<p;s++)
{
if (tempPoint1 == tempPolygon1[s])
{
for (int i=0;i<p;i++)
{
temp_Polygon1.push_back(tempPolygon1[((s+i)%p)]);
for (int s=0;s<p;s++)
{
if (tempPoint1 == tempPolygon1[s])
{
for (int i=0;i<p;i++)
{
temp_Polygon1.push_back(tempPolygon1[((s+i)%p)]);
}
break;
}
}
for (int t=0;t<q;t++)
{
if (tempPoint2 == tempPolygon2[t])
{
for (int i=0;i<p;i++)
{
temp_Polygon2.push_back(tempPolygon2[((t+i)%p)]);
break;
}
}
for (int t=0;t<q;t++)
{
if (tempPoint2 == tempPolygon2[t])
{
for (int i=0;i<p;i++)
{
temp_Polygon2.push_back(tempPolygon2[((t+i)%p)]);
}
break;
}
}
break;
}
}
if ((temp_Polygon1[0] == temp_Polygon2[0])&&(temp_Polygon1[1] == temp_Polygon2[q-1]))
{
//串联两个坐标串
for (int t=0;t<q;t++)
{
ResultPolygon.push_back(temp_Polygon2[t]);
}
for (int s=2;s<p;s++)
{
ResultPolygon.push_back(temp_Polygon1[s]);
{
//串联两个坐标串
for (int t=0;t<q;t++)
{
ResultPolygon.push_back(temp_Polygon2[t]);
}
for (int s=2;s<p;s++)
{
ResultPolygon.push_back(temp_Polygon1[s]);
}
}
else
{
else
{
//串联两个坐标串
for (int s=0;s<p;s++)
{
ResultPolygon.push_back(temp_Polygon1[s]);
}
for (int s=0;s<p;s++)
{
ResultPolygon.push_back(temp_Polygon1[s]);
}
for (int t=2;t<q;t++)
{
ResultPolygon.push_back(temp_Polygon2[t]);
{
ResultPolygon.push_back(temp_Polygon2[t]);
}
}
}
}
else
{
// vector<Vertex2d> temp_Polygon1;
// vector<Vertex2d> temp_Polygon2;
int p,q;
p= tempPolygon1.size();
q= tempPolygon2.size();
else
{
// vector<Vertex2d> temp_Polygon1;
// vector<Vertex2d> temp_Polygon2;
int p,q;
p= tempPolygon1.size();
q= tempPolygon2.size();
//重新排列多边形顶点顺序
for (int s=0;s<p;s++)
{
if (tempPoint1 == tempPolygon1[s])
{
for (int i=0;i<p;i++)
{
temp_Polygon1.push_back(tempPolygon1[((s+i)%p)]);
for (int s=0;s<p;s++)
{
if (tempPoint1 == tempPolygon1[s])
{
for (int i=0;i<p;i++)
{
temp_Polygon1.push_back(tempPolygon1[((s+i)%p)]);
}
break;
}
}
break;
}
}
for (int t=0;t<q;t++)
{
if (tempPoint2 == tempPolygon2[t])
{
for (int i=0;i<p;i++)
{
temp_Polygon2.push_back(tempPolygon2[((t+i)%p)]);
{
if (tempPoint2 == tempPolygon2[t])
{
for (int i=0;i<p;i++)
{
temp_Polygon2.push_back(tempPolygon2[((t+i)%p)]);
}
break;
}
}
break;
}
}
//串联两个坐标串
for (int t=0;t<q;t++)
{
ResultPolygon.push_back(temp_Polygon2[t]);
}
for (int t=0;t<q;t++)
{
ResultPolygon.push_back(temp_Polygon2[t]);
}
for (int s=1;s<p-1;s++)
{
ResultPolygon.push_back(temp_Polygon1[s]);
{
ResultPolygon.push_back(temp_Polygon1[s]);
}
}
return true;
}