凸多边形求并的简单算法

凸多边形求并的简单算法 
  求两个凸多边形相交之后的外边界, 就是多边形求并, 以下是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]);
 int m,n;
 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;
 //求得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];
     line2.begin = Polygon2[j];
     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];
    line2.begin = Polygon2[j];
    line2.end = Polygon2[j+1];
    if (Across(line1,line2) == 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];
     line1.begin = Polygon1[i];
     line1.end = Polygon1[i+1];
     if (Across(line1,line2) == 1)
     {
      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];
    line1.begin = Polygon1[i];
    line1.end = Polygon1[i+1];
    if (Across(line1,line2) == 1)
    {
     tempPoint2 = CrossPoint(line1,line2);
     tempPolygon2.push_back(tempPoint2);

    }
   }
   flag =1;
  }
 }
 //合并tempPolygon1和tempPolygon2
 if (tempPoint1 == tempPoint2)
 {
 //  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)]);
     }
     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;
    }
   }
  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]);
   }
  }
  else
  {
   //串联两个坐标串
   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]);
   }
  }
 }
 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)]);
    }
    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++)
  {
   ResultPolygon.push_back(temp_Polygon2[t]);
  }
  for (int s=1;s<p-1;s++)
  {
   ResultPolygon.push_back(temp_Polygon1[s]);
  }
 }

 return true;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值