# Math question about gps co'ords

Hi guys
I’m trying to work out the simplest and most efficient way to determine if a current gps position lat and long is inside a box formed of 4 gps lat/long points.
Now I could do it by comparing the current position lat long against each of the four points in turn but I don’t think that’s very efficient and long way to do this. I did a bit of research and came up with things like bilinear interpolation but to be honest it might as well try to learn brain surgery first based on what I saw (algebra was never my strongest subject when I was at school back in the annuls of time)
Any suggestions are most welcome

Ray casting is the way to do it

That way you can have a any sided polygon.
I created some code awhile back for a GPS boundary detection fail safe of a UAV…

``````
public static bool LocationInsideBoundry(Position point, Position[] boundry)
{
bool insideBoundry = false;
try
{
int j = boundry.Length - 1;
for (int i = 0; i < boundry.Length; i++)
{
if (boundry[i].Longitude < point.Longitude && boundry[j].Longitude >= point.Longitude ||
boundry[j].Longitude < point.Longitude && boundry[i].Longitude >= point.Longitude)
{
if (boundry[i].Latitude +
(point.Longitude - boundry[i].Longitude) / (boundry[j].Longitude - boundry[i].Longitude) * (boundry[j].Latitude - boundry[i].Latitude) < point.Latitude)
{
insideBoundry = !insideBoundry;
}
}
j = i;
}
}
catch (Exception)
{
return false;
}
return insideBoundry;
}

``````
2 Likes

``````
public struct Position
{
public double Latitude;
public double Longitude;
public double Altitude;
public string LatitudeDms;
public string LongitudeDms;
}

``````

Many thanks Justin. I edited the topic to a question and gave you a tick for your efforts. Much appreciated.
I will give this a go later on

@ HughB - Cheers fella, i have a few other Library Bits and Bobs for doing GPS type fluff like distance between points and bearing to points etc if it helps.

You are welcome chap
I’m kinda getting really excited about gps at the moment so anything you have would be most welcome. Maybe post it to code share??

I’ll dig it up and put a bit more polish on it before i add it, what sort of thing are you trying to do so i can focus on those bits first perhaps.

Mainly checking for position in zones but distance to a point would also be very usefull

Brilliant! Also saw the GPS library [1] you just posted on codeshare - will save me some time on my rover project!

Cheers!
-Steve

There is a function in his library that does that: GetDistanceBetweenPoints(Position pos1, Position pos2)

Does this code consider plus and minus values?

@ Mike - which bit?

@ Mike - if you take these 2 locations

51.974852, -0.221769 and 51.972303, -0.224007 will give you a distance of 0.3225km and a bearing of 208deg

Does this cover your +/- values or are you talking about something else?

I was thinking of something like -10, 10 to 10,-10?

I did not study the code in detail, so there might not be an issue. But, from prior experiences, positive to negative transitions always cause a problem.

No it;s not an issue -36.847593,174.757641 (Downtown Auckland NZ) to -36.815208,174.743798 (North Shore Auckland NZ) gives you a distance of 3.8km which you can see in the attachment Giggle backs up

Also using your -10, 10 to 10,-10 +/- example

Auckland -36.847593,174.757641
New York 40.71710,-74.014556

We get a distance of 14,206 km

So all good and you’ll need a battery bigger than Elvis to get the uC from point A to B

Or a cigarette lighter to USB adapter in the car

1 Like

For that trip you would might need a Toybota instead(Toybota | Top Gear Wiki | Fandom)

One of my favorite episodes.

1 Like

Great!

mine too, as Clarkson would say “there is nothing that cant be solved with more power”

My absolute fav is the Reliant Robin Space Shuttle !