I have a box with known bounding coordinates (latitudes and longitudes): latN, latS, lonW, lonE
.
I have a mystery point P
with unknown coordinates. The only data available is the distance from P
to any point p
. dist(p,P)
.`
I need a function that tells me whether this point is inside or outside the box.
Best Answer
Once we know the coordinates of P then the problem revers to a well known answer. To get the coordinates of $P=(x, y)$ we take three measurements. I have moved the coordinate system onto one corner and named the objects accordingly, with $a$ the horizontal side and $b$ the vertical side. In addition, we are going to find the coordinates of P in polar notation, with the distance $r$ and angle $\theta$.
Measuring $\vec{AP}$ we get the distance $r$. Measuring $\vec{BP}$ gives us the cosine and measuring $\vec{DP}$ gives us the sine, by means of the law of cosines.
$$ \cos\theta = \frac{r^2+a^2-d_{BP}^2}{2 a r} $$ $$ \sin\theta = \frac{r^2+b^2-d_{DP}^2}{2 b r} $$
So the location of P is
$$ x = r \cos\theta = d_{AP} \frac{r^2+a^2-d_{BP}^2}{2 a r} $$ $$ y = r \sin\theta = d_{AP} \frac{r^2+b^2-d_{DP}^2}{2 b r} $$
The point is inside if $x>=0$ and $x<=a$ and $y>=0$ and $y<=b$.
I have checked this with the above
C#
codeand it all checks out ok. No need to check for signs and quadrants. It all works out cleanly.