Maths has never been my strong point and I'm struggling with a task.
I need to determine if a set of longitude and latitude co-ordinates fall inside a box, where the max/min longitude and max/min latitude have been given.
Any help would be appreciated.
** EDITED **
I'm using GPS co-ordinates, so the minimum longitude could be larger than the maximum longitude. i.e if the box is over date line.
Given minlat
, maxlat
, minlong
and maxlong
(ie the 4 corners of your box) then it's a simple matter of determining whether your lat
lies between minlat
and maxlat
and your long
between minlong
and maxlong
.
If the box straddles 180°
either shift all your longitudes by adding (or subtracting) the same offset (take care with signs) or split the box into two along longitude 180°
and test both halves separately.
if( latitude>= given.minimumLatitude and lat <= given.maximumLatitude )
{
if( longitude >= given.minimumLongitude and longitude <= given.maximumLongitude )
{
return true;
}
}
return false;
box = left, right, top, bottom
point = x, y
if (x >= left && x <= right && y >= bottom && y <= top) {
return true
} else {
return false
}