Search code examples
java.netgpsnumbers

Detecting two GPS coordinates bounding boxs overlap


I have two GPS locations. For each I am creating a bounding box in a different range. Each bounding box has min/max latitude and min/max longitude.

Need to implement a method to detect if those two boxes overlap (don't mind the overlap range.. only true/false). Also, this method will be integrated in a long loop so I am looking for the most efficient way to do it.

note: when saying overlap I mean - "there is at least one single point on the map that is contained in both bounding boxes".

Any ideas?


Solution

  • It's enough to check if one of the corners of one rectangle is within the other rectangle. This is true of these two hold:

    • rect1.minX or rect1.maxX is between rect2.minX and rect2.maxX
      and
    • rect1.minY or rect1.maxY is between rect2.minY and rect2.maxY

    This check should take no time at all to do, so efficiency isn't a problem. Also, the order of the arguments is irrelevant.