Permalink
Browse files

Extract calculation of 'bearing' from MM.Location.interpolate; never …

…used. Repurpose it for a standalone 'bearing' function, since that's useful too.
  • Loading branch information...
1 parent a2133e1 commit f8885b27f152c29163346f804d9b424c7b959796 @chrisforbes chrisforbes committed Apr 26, 2012
Showing with 18 additions and 11 deletions.
  1. +18 −11 src/location.js
View
@@ -65,17 +65,6 @@
Math.pow(Math.sin((lat1 - lat2) / 2), 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
- var bearing = Math.atan2(
- Math.sin(lon1 - lon2) *
- Math.cos(lat2),
- Math.cos(lat1) *
- Math.sin(lat2) -
- Math.sin(lat1) *
- Math.cos(lat2) *
- Math.cos(lon1 - lon2)
- ) / -(Math.PI / 180);
-
- bearing = bearing < 0 ? 360 + bearing : bearing;
var A = Math.sin((1-f)*d)/Math.sin(d);
var B = Math.sin(f*d)/Math.sin(d);
@@ -90,3 +79,21 @@
return new MM.Location(latN / deg2rad, lonN / deg2rad);
};
+
+ // Returns bearing from one point to another
+ //
+ // * FIXME: bearing is not constant along significant great circle arcs.
+ MM.Location.bearing = function(l1, l2) {
+ var result = Math.atan2(
+ Math.sin(lon1 - lon2) *
+ Math.cos(lat2),
+ Math.cos(lat1) *
+ Math.sin(lat2) -
+ Math.sin(lat1) *
+ Math.cos(lat2) *
+ Math.cos(lon1 - lon2)
+ ) / -(Math.PI / 180);
+
+ // map it into 0-360 range
+ return (result < 0) ? result + 360 : result;
+ };

0 comments on commit f8885b2

Please sign in to comment.