判断点是否在(凸凹)多边形内

痞子三分冷 提交于 2020-08-19 20:46:37
template<typename T> struct Point3_ {
  Point3_() : x(0), y(0), z(0) {}
  Point3_(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
  T x;
  T y;
  T z;
};

typedef Point3_<int> Point3I;
typedef Point3_<float> Point3F;
typedef Point3_<double> Point3D;
 
bool Esvi3DGraph::PointInPolygon(
    const std::vector<Point3D> &points, const Point3D &testPoint) {
  int i = 0, j = 0;
  bool result  = false;
  int size     = points.size();
  double testX = testPoint.x;
  double testY = testPoint.y;
  for (i = 0, j = size - 1; i < size; j = i++) {
    const Point3D &vertI = points[i];
    const Point3D &vertJ = points[j];
    if (((vertI.y > testY) != (vertJ.y > testY)) &&
        (testX < (vertJ.x - vertI.x) * (testY - vertI.y) / (vertJ.y - vertI.y) +
                vertI.x))
      result = !result;
  }
  return result;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!