使用地图坐标确定点是否在矩形中

时间:2012-05-18 17:25:29

标签: c# geography

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

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

1 个答案:

答案 0 :(得分:3)

假设东方和北方都是积极的:

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;        
}