简体   繁体   English

如何在C#中的凸多边形算法中实现对点的有效测试?

[英]How to implement Efficient Test for a Point to Be in a Convex Polygon algorithm in C#?

Simple problem - find if a point is inside a convex Polygon. 简单问题-查找一个点是否在凸多边形内。 There is algorithm described yet due to be beeng an in Wolfram language and I ve got something wrong. 由于使用Wolfram语言进行了算法描述 ,因此我发现了一些错误。 This is what I have: 这就是我所拥有的:

private static bool PointInside2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd) {
    var v1 = lineStart - point;
    var edge = lineStart - lineEnd;
    return !(edge.x * v1.y - edge.y * v1.x < 0);
}

private static bool PointInsideRect2D(Vector2 point, IList<Vector2> rect) {
    var lastPoint = rect.Count - 1;
    bool? lastResult = null;
    for (var i = 0; i < lastPoint; ++i) {
        if (lastResult == null) {
            lastResult = PointInside2D(point, rect[i], rect[i + 1]);
        }
        else {
            if (lastResult != PointInside2D(point, rect[i], rect[i + 1])) {
                return false;
            }
        }
    }
    return lastResult == PointInside2D( point, rect[lastPoint], rect[0] );
}

and it does not work sadly... I looked at some refrence implementations here tried them seems also not to work.. 而且它不会令人遗憾地工作...我在这里查看了一些refrence实现,尝试过它们似乎也不起作用。

test data I use is for convex: 我使用的测试数据是凸的:

 [(262.8, 669.1); (1623.9, 718.2); (200.4, 895.4); (1817.8, 1540.8)]

and (288, 815) and (1078, 890) as test points. (288, 815)(1078, 890)作为测试点。

Can any one explain what I've got wrong in that algorithm/its implementations? 谁能解释我在该算法/其实现中出错的地方?

I believe your algorithm works correctly. 我相信您的算法可以正常工作。 It tests, if tested point lies on the same side (left or right) of all edges of polygon. 它测试是否测试点位于多边形所有边的同一侧(左侧或右侧)。 But it requires, that all points in polygon declaration are sorted in clockwise or anti-clockwise order, that is not true for [(262.8, 669.1); 但是它要求多边形声明中的所有点都必须按顺时针或逆时针顺序排序,但对于[(262.8,669.1); (1623.9, 718.2); (1623.9,718.2); (200.4, 895.4); (200.4,895.4); (1817.8, 1540.8)]. (1817.8,1540.8)]。

When I changed order of points in polygon, following program seem to return correct results: 当我更改多边形中点的顺序时,以下程序似乎返回正确的结果:

    public static void Main()
    {
        Vector2 p1 = new Vector2(288, 815);
        Vector2 p2 = new Vector2(1078, 890);

        //Please notice order of points is changed to clockwise
        IList<Vector2> Polygon = new List<Vector2>(new Vector2[] { new Vector2(262.8f, 669.1f), new Vector2(200.4f, 895.4f), new Vector2(1817.8f, 1540.8f), new Vector2(1623.9f, 718.2f) });

        bool p1Result = PointInsideRect2D(p1, Polygon);
        bool p2Result = PointInsideRect2D(p2, Polygon);
    }

    private static bool PointInside2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd)
    {
        var v1 = lineStart - point;
        var edge = lineStart - lineEnd;
        return !(edge.X * v1.Y - edge.Y * v1.X < 0);
    }

    private static bool PointInsideRect2D(Vector2 point, IList<Vector2> rect)
    {
        var lastPoint = rect.Count - 1;
        bool? lastResult = null;
        for (var i = 0; i < lastPoint; ++i)
        {
            if (lastResult == null)
            {
                lastResult = PointInside2D(point, rect[i], rect[i + 1]);
            }
            else
            {
                if (lastResult != PointInside2D(point, rect[i], rect[i + 1]))
                {
                    return false;
                }
            }
        }
        return lastResult == PointInside2D(point, rect[lastPoint], rect[0]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM