我正在开发一个从GPS设备接收GPS位置的应用程序。这部分还可以。
在我的应用程序中,我得到了一个多边形列表,我想测试我的位置是女巫多边形中的每个位置。
我的多边形采用文本格式:"30.5,15.6;25.4,15;..."
我问女巫图书馆,我可以用来确定我的GPS位置是否在那些多边形中。
为此,我添加了对 System.Space 的引用,但我不明白如何创建一个新的几何多边形和我可以用来确定我的点是否在多边形中的方法。
如果在 2D 中工作足够好,您可以使用 System.Drawing.Drawing2D
中的GraphicsPath
。
这是在Console
应用程序中使用GraphicsPath
的最小示例:
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
static void Main(string[] args)
{
List<PointF> points = new List<PointF>();
points.Add(new PointF(30.5f, 15.6f));
points.Add(new PointF(25.4f, 5f));
points.Add(new PointF(0, 20));
float y = 11f;
for (int x = 0; x < 30; x++)
if (IsInPolygon(points, new PointF(x, y)))
Console.WriteLine("(" + x + ", y) is inside");
Console.ReadLine();
}
public static bool IsInPolygon(List<PointF> points, PointF location)
{
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddPolygon(points.ToArray());
return gp.IsVisible(location);
}
}
}
您还需要添加对 System.Drawing
的引用。