Working algorithm for testing two rectangles for overlapping in Earth GPS coordinates plain Here is a seemingly simple, but actually quite tricky problem: 
I am trying to figure out the correct algorithm to test intersection/overlapping of two rectangles, which are plotted on the Earth's surface (have GPS coordinates). The rectangles represent geographical locations' bounds (i.e. Nort-most, South-most, East-most, West-most points).
Some ilustration of the right algorithm on PHP, c++ or whatever comp. language will be greatly welcome.  
Input data (refer to the diagram):
(1) GPS coords of two diagonal edges 


*

*RECTANGLE I: D(D1,D2) and B(B1,B2)

*RECTANGLE II: I(I1,I2) and G(G1,G2)


This is sufficient to resolve the GPS coords of the other two edges: A(B1,D2) and C(D1,B2) . Same aplied for the other rectangle
(2) GPS coords of the diagonals intersection K(K1,K2) and E(E1,E2).
This is sufficient to rule out which constitutes the rectangle (e.g. Greenland bounds OR everything BUT Greelnand ) 
(3) The rectangles are ploted out of bounds of geographical locations, so some 'perpendicularity' to the Equator/0-merridian is inherent
Issues:
(1) The test of edges of RECTANGE I against RECTANGLE II and vice-versa fails (as the rectangles may be situated cross-like (see the diagram)
(2) The switch from positive lon (+180) to negative lon (-180) of the Eartch GPS coord system kills all properly working in a rectangular coordinates plain algorithms (the issue here can be understood easily by viewing the same example ilustrated over the map)
(3) The location/size of the rectangles can be arbitrary, so assumptions for location/size (e.g. rectangle ploted over Europe) also kill some of the tested algorithms
Any ideas or suggestions how to properly do the test for intersection ? 

 A: I'd start with this north-most, south-most, west-most and east-most coordinates, since they will be eaiser to handle and seem to be the source of these “rectangles” in any case.
Take one “rectangle” defined by (n1, s1, w1, e1), and the other by (n2, s2, w2, e2). I assume coordinates to increase northwards and eastwards.
If s2 > n1 or s1 > n2, then the two rectangles are in different latitudes, and can not intersect. Otherwise they will share some latitude and need a second look. If w2 < e1 and w1 < e2, then the rectangles do overlap.
But so far I have avoided the problem of wrap-around. You might have to adjust some longitudes by some multiple of 360°. To deal with that, first make sure that wi < ei < wi+360°, i.e. the “rectangle” does not go in the wrong direction or around the earth more than once. Also normalize “rectangles” by ensuring that 0 ≤ wi < 360°. This still does not ensure that you will correctly detect an overlap. But you can test as above, then move one rectangle east by 360° and repeat the test, then move the first back an dinstead the other east and do a third test. All of this is only required if they have a non-empty range of latitudes in common.
A: Here is something I think should work, using the fact that (when writing code) it is easy to measure the distance between two points defined by GPS coordinates.  
Let $A$ be the top left hand corner of rectangle 1, $B$ the top right hand corner of rectangle 1, and $C$ the top left hand corner of rectangle 2; form the triangle $\triangle{ABC}$, and determine the angles $\angle{CAB}$ and $\angle{CBA}$ using standard trig.  If both of these angles are less than 90 degrees, then it means that the rectangles potentially overlap in this region of the horizontal axis, but then you also have to check the vertical direction as well.  Then repeat this procedure for all relevant triples of points.
