diff --git a/source/Ox/js/Geo.js b/source/Ox/js/Geo.js index 283391c0..2c6b3cda 100644 --- a/source/Ox/js/Geo.js +++ b/source/Ox/js/Geo.js @@ -62,6 +62,21 @@ * Math.abs(pointA.lng - pointB.lng); }; + /*@ + Ox.getAverageBearing Returns the average of two bearings + > Ox.getAverageBearing(0, 90) + 45 + > Ox.getAverageBearing(10, 350) + 0 + @*/ + // FIXME: find the proper name of this operation + // FIMXE: use in manhattan grid example + Ox.getAverageBearing = function(bearingA, bearingB) { + return Ox.mod((bearingA + bearingB) / 2 + ( + Math.abs(bearingA - bearingB) > 180 ? 180 : 0 + ), 360); + }; + /*@ Ox.getBearing Returns the bearing from one point to another > Ox.getBearing({lat: -45, lng: 0}, {lat: 45, lng: 0}) @@ -80,6 +95,12 @@ return (Ox.deg(Math.atan2(y, x)) + 360) % 360; }; + // FIXME: name, docs + Ox.getBearingDifference = function(bearingA, bearingB) { + var difference = Math.abs(bearingA - bearingB); + return difference > 180 ? 360 - difference : difference; + }; + /*@ Ox.getCenter Returns the center of a recangle on a spehre > Ox.getCenter({lat: -45, lng: -90}, {lat: 45, lng: 90}) @@ -115,6 +136,14 @@ }); }; + // FIXME: name, docs + Ox.getClosestBearing = function(bearing, bearings) { + var differences = bearings.map(function(bearing_) { + return getBearingDifference(bearing, bearing_); + }); + return bearings[differences.indexOf(Ox.min(differences))]; + }; + /*@ Ox.getDegreesPerMeter Returns degrees per meter at a given latitude > 360 / Ox.getDegreesPerMeter(0)