如何确定是否一个GPS的矩形区域之内的坐标谎言? [英] How to determine if a GPS coordinate lies within a rectangular area?

查看:361
本文介绍了如何确定是否一个GPS的矩形区域之内的坐标谎言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历的StackOverflow和其他网站很多类似的问题,我根据我的解决方案对这些问题的答案,但我仍然无法得到它的工作...

I went through many similar questions on Stackoverflow and other websites and I based my solution on those answers but I still can't get it to work…

我的问题:我要确定一个特定的GPS位置 P 是否位于四个给定的GPS坐标<围成的长方形区域内code> A , B C ð

My problem: I want to determine whether or not a certain GPS location P lies within a rectangular area bounded by four given GPS coordinates A, B, C, D.

目前,我计算三角形的面积 ABP BCP CDP DAP 。如果有这些领域是大于零(不被打乱,数学家),则该点在我的矩形以外的解释<一个href="http://www.emanueleferonato.com/2012/03/09/algorithm-to-determine-if-a-point-is-inside-a-square-with-mathematics-no-hit-test-involved/"相对=nofollow>这里。

At the moment I am calculating the areas of triangles ABP, BCP, CDP and DAP. If any of those areas is bigger than zero (don't be upset, mathematicians) the point lies outside my rectangle as explained here.

code:

private static double triangleArea(Location a, Location b, Location c) {
    // (C.x*B.y-B.x*C.y)-(C.x*A.y-A.x*C.y)+(B.x*A.y-A.x*B.y)
    double result = (c.getLongitude()*b.getLatitude()-b.getLongitude()*c.getLatitude())-(c.getLongitude()*a.getLatitude()-a.getLongitude()*c.getLatitude())+(b.getLongitude()*a.getLatitude()-a.getLongitude()*b.getLatitude());
    return result;
}

public static boolean isInsideSquare(Location a, Location b, Location c, Location d, Location p) {
    if (triangleArea(a,b,p)>0 || triangleArea(b,c,p)>0 || triangleArea(c,d,p)>0 || triangleArea(d,a,p)>0) {
        return false;
    }
    return true;
}

但是,当我调用这个函数它总是返回。使用以下坐标(纬度,经度),甚至当

But when I call this function it's always returning false. Even when using the following coordinates (latitude, longitude):

A: (50.884706, 4.714151)
B: (50.884944, 4.716149)
C: (50.884679, 4.716228)
D: (50.884441, 4.714230)
P: (50.884538, 4.714615)

然而,当我绘制这些点在地图上(如这里)我可以看到,点P的谎言在ABCD ......(见下文)

Yet when I plot those points on a map (e.g. here) I can see that point P lies within ABCD… (see below)

我读过一些简单的解决方案,你只减去X,贵点的y坐标,但显然仅适用于矩形平行于x轴和y轴,而我的长方形可以朝向任意角。

I've read some easy solutions where you just subtract the x, y coordinates of your points, but that would obviously only work for rectangles parallel to the x-axis and y-axis, whereas my rectangle can be oriented in any angle.

谁能告诉我什么,我做错了,或提出一个更好的解决办法来解决这个问题呢?

Can anyone tell me what I am doing wrong or suggest a better solution to solve this problem?

非常感谢!

推荐答案

  • 允许延长的矩形的两侧。因此,我们有4直线L <子> AB ,L <子> BC ,L <子> CD ,L <子> DA ,或者,对于急促,L <子> 1 ,L 2 ,L <子> 3 ,L <子> 4 。
  • 请一个公式,每→<子>我。排序的公式:

If a point is inside a rectangle. On a plane. For mathematician or geodesy (GPS) coordinates

  • Lets prolong the sides of the rectangle. So we have 4 straight lines lAB, lBC, lCD, lDA, or, for shortness, l1, l2, l3, l4.
  • Make an equation for every li. The equation sort of:

    F <子>我(P)= 0。

    fi(P)=0.

    P是一个点。对于分,属于→<子>我,方程是真实的。

    P is a point. For points, belonging to li, the equation is true.

    • 我们需要在方程的左右两侧的功能。他们为F 1 F 2 F 3 F 4
    • 请注意,对于由L <子>的一侧每个点我函数f <子>我大于0,从另一面F积分<子>我比0小。
    • 所以,如果我们检查的矩形P是,我们只需要为p来是对所有四行正确的两侧。所以,我们要检查四个功能为自己的招牌。
    • 但是,侧线是正确的,哪个矩形所属?它是侧,其中位于矩形的不属于该行的顶点。为了检验我们可以选择两种不属于顶点任何人。
    • 所以,我们要检查这一点:

    • We need the functions on the left sides of the equations. They are f1, f2, f3, f4.
    • Notice, that for every point from one side of li the function fi is greater than 0, for points from the other side fi is lesser than 0.
    • So, if we are checking for P being in rectangle, we only need for the p to be on correct sides of all four lines. So, we have to check four functions for their signs.
    • But what side of the line is the correct one, to which the rectangle belongs? It is the side, where lie the vertices of rectangle that don't belong to the line. For checking we can choose anyone of two not belonging vertices.
    • So, we have to check this:

    F <子> AB (P)F <子> AB (C)> = 0

    fAB(P) fAB(C) >= 0

    F <子> BC (P)F <子> BC (D)> = 0

    fBC(P) fBC(D) >= 0

    F <子> CD (P)F <子> CD (A)> = 0

    fCD(P) fCD(A) >= 0

    F <子> DA (P)F <子> DA (B)> = 0

    fDA(P) fDA(B) >= 0

    的unequations不严格,因为如果一个点是在边界上,它属于矩形,太。如果你没有在边界上需要的点,你可以改变不等式严格的。但是,当你在浮点运算的工作,选择是无关紧要的。

    • 对于一个点,那就是在长方形,四个不等式是真实的。请注意,它的工作原理也为每一个凸多边形,线仅数/方程式将有所不同。
    • 剩下的唯一的事情就是得到一个方程行经历两分。它是一种公知的线性方程。让我们把它写了一个直线AB和点P:

    • For a point, that is in the rectangle, all four inequations are true. Notice, that it works also for every convex polygon, only the number of lines/equations will differ.
    • The only thing left is to get an equation for a line going through two points. It is a well-known linear equation. Let's write it for a line AB and point P:

    F <子> AB (P)&NBSP;&NBSP;&≡NBSP;&NBSP; (X <分> A -x <子> B )(Y <子> P -y <子> B ) - (Y <子> A -y <子> B )(X <子> P -x <子> B )

    fAB(P)   ≡   (xA-xB) (yP-yB) - (yA-yB) (xP-xB)

    该检查可以简化 - 让我们沿着矩形的顺时针 - A,B,C,D,A,然后正确的双方将是该行的权利。所以,我们不必与一侧的另一个顶点是比较。我们需要检查一组较短的不等式:

    The check could be simplified - let's go along the rectangle clockwise - A, B, C, D, A. Then all correct sides will be to the right of the lines. So, we needn't compare with the side where another vertice is. And we need check a set of shorter inequations:

    F <子> AB (P)> = 0

    fAB(P) >= 0

    F <子> BC (P)> = 0

    fBC(P) >= 0

    F <子> CD (P)> = 0

    fCD(P) >= 0

    F <子> DA (P)> = 0

    fDA(P) >= 0

    但是,这是正确的为正常,数学家设定坐标,其中X是正确的和Y顶端的。和用于在大地测量坐标,如被用在GPS的,其中X是顶端,和Y是在正确的,我们必须把不等式:

    But this is correct for the normal, mathematician set of coordinates, where X is to the right and Y to the top. And for the geodesy coordinates, as are used in GPS, where X is to the top, and Y is to the right, we have to turn the inequations:

    F <子> AB (P)&LT; = 0

    fAB(P) <= 0

    F <子> BC (P)&LT; = 0

    fBC(P) <= 0

    F <子> CD (P)&LT; = 0

    fCD(P) <= 0

    F <子> DA (P)&LT; = 0

    fDA(P) <= 0

    如果您不能确定与轴的方向,要小心这个简单的检查 - 检查一分手动,如果你选择了正确的不等式

    顺便说一句,在球体的矩形具有意义了。它是两个平行和两个经络,当然之间的区域,与主轴线的任何方向。但你并不需要它,根据意见。

    BTW, the rectangle on the sphere has sense too. It is the area between two parallels and two meridians, of course, with any orientation of the main axis. But you don't need it, according to the comment.

    这篇关于如何确定是否一个GPS的矩形区域之内的坐标谎言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆