如何判断点是否位于含圆弧的2D多边形内?C#实现假阴性问题求解
含圆弧2D多边形的点包含判断实现问题
我一直在参考相关资料尝试实现点是否在含圆弧的2D多边形内的判断逻辑,参考的核心内容方向包括:
- 无曲线的普通2D多边形点包含判断方法
- C#语言下复合多边形的点包含判断实现方案
- 曲线包含点判断的相关理论
我的方案采用光线投射法判断点是否在多边形边界内,以下是我结合参考资料编写的C#代码:
public static bool IsInsideBoundary(Polyline pline, Point3d pnt, Document currentDoc) { try { if (pline.Closed == true) { Tools.Log("Polyline is Closed"); // Check if point is on the line -> WORKS if (IsPointOnCurve(pline, pnt)) { Log("Point is on the Boundary line"); return true; } else { int numOfVerts = pline.NumberOfVertices; // Get bounding box of boundary double minX = pline.GetPoint3dAt(0).X; double maxX = pline.GetPoint3dAt(0).X; double minY = pline.GetPoint3dAt(0).Y; double maxY = pline.GetPoint3dAt(0).Y; for (int i = 1; i < numOfVerts; i++) { Point3d q = pline.GetPoint3dAt(i); minX = Math.Min(q.X, minX); maxX = Math.Max(q.X, maxX); minY = Math.Min(q.Y, minY); maxY = Math.Max(q.Y, minY); } // Check if the given point outside of the bounding box if (pnt.X < minX || pnt.X > maxX || pnt.Y < minY || pnt.Y > maxY) { return false; } Log("Bounding Box check passed..."); bool inside = false; for (int i = 0, j = numOfVerts - 1; i < numOfVerts; j = i++) { double a1 = pline.GetPoint3dAt(i).X, a2 = pline.GetPoint3dAt(i).Y; double b1 = pnt.X, b2 = pnt.Y; double c1 = pline.GetPoint3dAt(j).X, c2 = pline.GetPoint3dAt(j).Y; // Use the Jordan Curve Theorem double d1 = (c1 - a1) * (b2 - a2) / (c2 - a2) + a1; // Split arc into monotone parts and check bounds if ((a2 > b2) != (c2 > b2) && b1 < d1) { inside = !inside; } } Log("Point is within the boundary"); return inside; } } else { Log("Boundary is not closed."); } } catch (System.Exception e) { Log("InsideBoundary() Failed. Exception: " + e.Message, true); } return false; }
注:代码中用到的
GetPoint3dAt()方法来自AutoCAD .NET API。该方案对不含圆弧的多边形运行正常,但引入圆弧后,若点位于多边形周长范围内会产生假阴性结果。
我测试的多边形示例如下:
内容的提问来源于stack exchange,提问作者wisenickel
相关产品推荐
相关产品推荐

