【问题标题】:Determine if point is in a rectangle using map coordiates使用地图坐标确定点是否在矩形中
【发布时间】:2012-05-18 17:25:28
【问题描述】:

我的大脑今天不工作了。我需要测试一个点(纬度,经度)是否位于地图上的一个矩形内。矩形由它的北、东、南和西边界定义。打嗝是所有点或值都在地图坐标系中。为了处理日期线环绕,我假设如果我们从左到右,经度总是“介于”之间。

bool PointInRectangle(Point pt, double North, double East, double South, double West)
{
    // ????
}

【问题讨论】:

  • @Jeppe 类似的问题,但他们的要求有点不同(可视化地图,不需要 100% 准确)。我希望有一个小代码片段。

标签: c# geography


【解决方案1】:

假设东和北是正数:

bool PointInRectangle(Point pt, double North, double East, double South, double West)
{
    // you may want to check that the point is a valid coordinate
    if (West < East)
    {
        return pt.X < East && pt.X > West && pt.Y < North && pt.Y > South;
    }

    // it crosses the date line
    return (pt.X < East || pt.X > West) && pt.Y < North && pt.Y > South;        
}

【讨论】:

  • 啊 - 那个简单的 OR 就是我所缺少的。
  • 除非我误解了这个问题,否则我认为您的日期线条件不起作用。假设 West 是 170,East 是 -170,那么唯一有效的经度是 170 到 180 和 -180 到 -170。但是,经度 165 通过pt.X &gt; East 条件。我是不是误会了?
  • 谢谢 - 我知道这是一些简单的答案!我花了 30 分钟处理 180、360 和模逻辑,并且知道必须有更好的方法。
猜你喜欢
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多