多边形中的点,包括 c# 中带有边距的边缘情况

我正在使用 Unity3D,并且有一个多边形(Vector2 数组)以及要检查的点。在过去的几天里,我一直在寻找包括 pnpoly 和其他算法在内的解决方案。问题是我的误差高达 0.001f,因为我在使用 TransformPoint(以获取网格顶点的世界位置)后立即乘以四元数,将 3D 面投影到 2D 平面上。


我对多边形一无所知,因为它们是由许多网格三角形组成的 - 它们可以具有任何形状。


我如何处理这种极端的不准确性并找到多边形内部或边界上的所有点?


    public static bool IsInsidePolygon(Vector2[] vertices, Vector2 checkPoint)

    {

        float[] vertX = new float[vertices.Length];

        float[] vertY = new float[vertices.Length];

        for (int i = 0; i < vertices.Length; i++)

        {

            vertX[i] = vertices[i].x;

            vertY[i] = vertices[i].y;

        }


        return IsInsidePolygon(vertices.Length, vertX, vertY, checkPoint.x, checkPoint.y);

    }


    public static bool IsInsidePolygon3(int nvert, float[] vertx, float[] verty, float testx, float testy)

    {

        int i, j = 0;

        bool c = false;

        for (i = 0, j = nvert - 1; i < nvert; j = i++)

        {

            if (((verty[i] > testy) != (verty[j] > testy)) &&

         (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))

                c = !c;

        }

        return c;

    }


慕村225694
浏览 97回答 1
1回答

明月笑刀无情

解决方案是找到到多边形的最近距离,如果距离在边距内,则返回 true,以下是完整代码:'''public static float DistancePointLine2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd){&nbsp; &nbsp; return (ProjectPointLine2D(point, lineStart, lineEnd) - point).magnitude;}public static Vector2 ProjectPointLine2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd){&nbsp; &nbsp; Vector2 rhs = point - lineStart;&nbsp; &nbsp; Vector2 vector2 = lineEnd - lineStart;&nbsp; &nbsp; float magnitude = vector2.magnitude;&nbsp; &nbsp; Vector2 lhs = vector2;&nbsp; &nbsp; if (magnitude > 1E-06f)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; lhs = (Vector2)(lhs / magnitude);&nbsp; &nbsp; }&nbsp; &nbsp; float num2 = Mathf.Clamp(Vector2.Dot(lhs, rhs), 0f, magnitude);&nbsp; &nbsp; return (lineStart + ((Vector2)(lhs * num2)));}public static float ClosestDistanceToPolygon(Vector2[] verts, Vector2 point){&nbsp; &nbsp; int nvert = verts.Length;&nbsp; &nbsp; int i, j = 0;&nbsp; &nbsp; float minDistance = Mathf.Infinity;&nbsp; &nbsp; for (i = 0, j = nvert - 1; i < nvert; j = i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; float distance = DistancePointLine2D(point, verts[i], verts[j]);&nbsp; &nbsp; &nbsp; &nbsp; minDistance = Mathf.Min(minDistance, distance);&nbsp; &nbsp; }&nbsp; &nbsp; return minDistance;}public static bool IsInsidePolygon(Vector2[] vertices, Vector2 checkPoint, float margin = 0.01f){&nbsp; &nbsp; if(ClosestDistanceToPolygon(vertices, checkPoint) < margin)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; float[] vertX = new float[vertices.Length];&nbsp; &nbsp; float[] vertY = new float[vertices.Length];&nbsp; &nbsp; for (int i = 0; i < vertices.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertX[i] = vertices[i].x;&nbsp; &nbsp; &nbsp; &nbsp; vertY[i] = vertices[i].y;&nbsp; &nbsp; }&nbsp; &nbsp; return IsInsidePolygon(vertices.Length, vertX, vertY, checkPoint.x, checkPoint.y);}public static bool IsInsidePolygon(int nvert, float[] vertx, float[] verty, float testx, float testy){&nbsp; &nbsp; bool c = false;&nbsp; &nbsp; int i, j = 0;&nbsp; &nbsp; for (i = 0, j = nvert - 1; i < nvert; j = i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ((((verty[i] <= testy) && (testy < verty[j])) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;((verty[j] <= testy) && (testy < verty[i]))) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = !c;&nbsp; &nbsp; }&nbsp; &nbsp; return c;}'''
打开App,查看更多内容
随时随地看视频慕课网APP