地球坐标系下判断多边形是否相交

本文基础是地球坐标系下判断线段相交,请参考

http://blog.csdn.net/philipzeng/archive/2010/07/22/5754339.aspx

能判断线段相交了,多边形相交问题也就迎刃而解。

 

class PolygonRegion
{
public:
 explicit PolygonRegion(vector<TSectorPoint> &vRegion);
 bool __stdcall IsOverlap(vector<TSectorPoint> vRegion);
private:
 vector<TSectorPoint> vPoints;
};

 

/**********************************************************************
* 函数名称: Polygon
* 功能描述: 多边形类构造函数
* 输入参数:
* 输出参数:
* 返 回 值: 无
* 其它说明:
* 修改日期    版本号     修改人      修改内容
***********************************************************************/
PolygonRegion::PolygonRegion(vector<TSectorPoint> &vRegion)
{
 vPoints=vRegion;
}
/**********************************************************************
* 函数名称: IsOverlap
* 功能描述: 判断是否与另外一个多边形相交
* 输入参数:
* 输出参数:
* 返 回 值: 无
* 其它说明:
* 修改日期    版本号     修改人      修改内容
***********************************************************************/
bool __stdcall PolygonRegion::IsOverlap(vector<TSectorPoint> vPolygon)
{
 vPoints.push_back(vPoints[0]);
 vPolygon.push_back(vPolygon[0]);
 for(unsigned int iLoop=0;iLoop < vPoints.size()-1;iLoop++)
 {
  TLine line(vPoints[iLoop],vPoints[iLoop+1]);
  for(unsigned int iLoop2=0;iLoop2 < vPolygon.size()-1;iLoop2++)
  {
   TLine checkline(vPolygon[iLoop2],vPolygon[iLoop2+1]);
   if(line.IsLineCross(checkline)==true)
   {
    vPoints.pop_back();
    return true;
   }
  }
  TLine checkline(vPolygon[0],vPolygon[vPolygon.size()-1]);
  if(line.IsLineCross(checkline)==true)
  {
   vPoints.pop_back();
   return true;
  }
 }
 vPoints.pop_back();
 return false;
}

判断多边形是否相交,可以使用射线法。具体实现步骤如下: 1. 遍历多边形的每条边,依次将每条边作为射线,从一个端点向另一个端点发出一条射线。 2. 统计每条射线与多边形的交点数量,若交点数量为奇数,则说明射线从该端点出发与多边形相交,否则不相交。 3. 对于所有的射线进行判断,若有一条射线与多边形相交,则说明多边形相交,否则不自相交。 具体实现可参考以下代码: ```c++ struct Point { int x, y; Point(int x = 0, int y = 0): x(x), y(y) {} }; // 判断两条线段是否相交 bool intersects(Point& a1, Point& a2, Point& b1, Point& b2) { int c1 = (a2.x - a1.x) * (b1.y - a1.y) - (a2.y - a1.y) * (b1.x - a1.x); int c2 = (a2.x - a1.x) * (b2.y - a1.y) - (a2.y - a1.y) * (b2.x - a1.x); int c3 = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x); int c4 = (b2.x - b1.x) * (a2.y - b1.y) - (b2.y - b1.y) * (a2.x - b1.x); return ((c1 ^ c2) < 0 && (c3 ^ c4) < 0); } // 判断是否多边形内 bool inside(Point& p, vector<Point>& polygon) { int n = polygon.size(), cnt = 0; for (int i = 0; i < n; ++i) { Point& a1 = polygon[i], a2 = polygon[(i + 1) % n]; if (a1.y == a2.y) continue; // 忽略水平线段 if (a1.y > a2.y) swap(a1, a2); // 保证 a1 在 a2 下方 if (p.y < a1.y || p.y >= a2.y) continue; // 忽略射线与线段不相交的情况 int x = (p.y - a1.y) * (a2.x - a1.x) / (a2.y - a1.y) + a1.x; // 计算交点的 x 坐标 if (x > p.x) cnt++; // 统计交点数量 } return (cnt & 1) == 1; // 判断交点数量的奇偶性 } // 判断多边形是否相交 bool isSelfIntersecting(vector<Point>& polygon) { int n = polygon.size(); for (int i = 0; i < n; ++i) { Point& a1 = polygon[i], a2 = polygon[(i + 1) % n]; for (int j = i + 2; j < n; ++j) { Point& b1 = polygon[j], b2 = polygon[(j + 1) % n]; if (intersects(a1, a2, b1, b2)) return true; // 判断相邻两条边是否相交 } } for (int i = 0; i < n; ++i) { if (inside(polygon[i], polygon)) return true; // 判断顶点是否多边形内部 } return false; } ``` 其中,`intersects` 函数用于判断两条线段是否相交,`inside` 函数用于判断是否多边形内部,`isSelfIntersecting` 函数用于判断多边形是否相交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值