I need to sort a coordinate list for a rectangle counterclockwise, and make the north-east corner the first coordinate. These are geographic coordinates (i.e. Longitude, Latitude) in decimal form.1
For example, here are the 4 corners of a rectangle, starting with the north-west corner and moving clockwise:
[
{ "lat": 34.495239, "lng": -118.127747 }, # north-west
{ "lat": 34.495239, "lng": -117.147217 }, # north-east
{ "lat": 34.095174, "lng": -117.147217 }, # south-east
{ "lat": 34.095174, "lng": -118.127747 } # south-west
]
I need to sort these counterclockwise and change the "anchor"/starting point to be north-east:
[
{ "lat": 34.495239, "lng": -117.147217 }, # north-east
{ "lat": 34.495239, "lng": -118.127747 }, # north-west
{ "lat": 34.095174, "lng": -118.127747 }, # south-west
{ "lat": 34.095174, "lng": -117.147217 } # south-east
]
I do not know what order the list will be in initially (i.e. clockwise or counterclockwise). I do not know which corner the first coordinate in the list represents.
1This is not a true rectangle when mapped to the surface of the earth, however since I do have 2 opposing corners I am calling it a rectangle for readability. Shapes that wrap +180/-180 longitude or +90/-90 latitude are not an issue.