3

我需要在 C# 中实现地理围栏。地理围栏区域可以是圆形、矩形、多边形等。有人在 C# 中实现了地理围栏吗?

我发现地理围栏 - 点内/外多边形。但是,它只支持多边形。

4

2 回答 2

6

我已经测试了各种实现,这个例子对我来说很有效:

例子

public static bool PolyContainsPoint(List<Point> points, Point p) {
    bool inside = false;

    // An imaginary closing segment is implied,
    // so begin testing with that.
    Point v1 = points[points.Count - 1];

    foreach (Point v0 in points)
    {
        double d1 = (p.Y - v0.Y) * (v1.X - v0.X);
        double d2 = (p.X - v0.X) * (v1.Y - v0.Y);

        if (p.Y < v1.Y)
        {
            // V1 below ray
            if (v0.Y <= p.Y)
            {
                // V0 on or above ray
                // Perform intersection test
                if (d1 > d2)
                {
                    inside = !inside; // Toggle state
                }
            }
        }
        else if (p.Y < v0.Y)
        {
            // V1 is on or above ray, V0 is below ray
            // Perform intersection test
            if (d1 < d2)
            {
                inside = !inside; // Toggle state
            }
        }

        v1 = v0; //Store previous endpoint as next startpoint
    }

    return inside; 
}
于 2014-07-30T15:06:05.567 回答
1

参考我的实现:

多边形

圆圈

于 2011-03-23T11:55:42.383 回答