【问题标题】:Given a list of GPS coordinates making up a boundary, how can I calculate if my location is within that boundary?给定构成边界的 GPS 坐标列表,我如何计算我的位置是否在该边界内?
【发布时间】:2016-02-19 17:20:14
【问题描述】:
假设我有数百甚至数千个 GPS 坐标、纬度和经度,它们构成了一个国家的边界。
我也有我当前的位置纬度和经度。
我如何确定(使用 C#,为 Windows10 UWP 编程)我的位置是否在一个国家/地区的边界内?
例如假设我有构成下图中红线的所有点。如果我在 X 位置,我的函数将返回 true。如果我在 Y 位置,我的函数将返回 false。
【问题讨论】:
标签:
latitude-longitude
computational-geometry
point-in-polygon
【解决方案3】:
感谢收到的两个答案,我发现了我正在尝试做的事情的实际名称......“多边形中的点”。
知道了这一点,我找到了这个Stack Overflow question and answer
如果上述内容消失,以下是代码:
/// <summary>
/// Determines if the given point is inside the polygon
/// </summary>
/// <param name="polygon">the vertices of polygon</param>
/// <param name="testPoint">the given point</param>
/// <returns>true if the point is inside the polygon; otherwise, false</returns>
public static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint)
{
bool result = false;
int j = polygon.Count() - 1;
for (int i = 0; i < polygon.Count(); i++)
{
if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y)
{
if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X)
{
result = !result;
}
}
j = i;
}
return result;
}
它需要一个组成多边形的点数组和一个要测试的点并返回真或假。在我的所有测试中运行良好。