Skip to content

Instantly share code, notes, and snippets.

@lqt0223
Last active May 10, 2018 01:14
Show Gist options
  • Save lqt0223/796cc8e8152911b2998d99066b45413f to your computer and use it in GitHub Desktop.
Save lqt0223/796cc8e8152911b2998d99066b45413f to your computer and use it in GitHub Desktop.
36 the haversine function and distance between locations

Deriving the Haversine Formula

The haversine formula is useful in calculating the distance between two locations on the Earth, when the latitudes & longitudes of the two locations are provided.

An implementation of calcDistance function based on haversine formula in JavaScript is as follows: (the degToRad function is solely a unit converter)

function degToRad(degrees) {
  return degrees * Math.PI / 180;
}

function calcDistance(lat1, lon1, lat2, lon2) {
  var earthRadiusKm = 6371;

  var dLat = degToRad(lat2-lat1);
  var dLon = degToRad(lon2-lon1);

  lat1 = degToRad(lat1);
  lat2 = degToRad(lat2);

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  return earthRadiusKm * c;
}

The algorithm in the calcDistance function and the meaning of intermediate values (a and c) can be explained using the following geometrical derivation.

Phase 1

The following is a isosceles triangle, with the length of the segment AO and BO equal to 1.

(please ignore the notation and as the image is shared in explaining another phase)

We can tell that: defining the angle ∠AOB as θ, then the length of the segment AB that is oppsite to the angle will be

https://lh3.googleusercontent.com/zdxVDUYOicK_RukyWulixsTpMcR8I_TFSv-6-zSKymgXGZWNpiMgYLmcsfRuoUmvxpmALcncuG1VrZDsHstf8JAobJrNO_O1WzwcLHySBlhf29HhEwY67xuOqcoHL9UVa53jFRBHB6uUy4a5Vu248z0cY1jaFhhFTa2dQOkVlr9GQY0NhGakO3WTcVlg7Ep58M4L4rMb82L5buLo347T8GtVg-ZKWzUKW2Mvmb_ex0EdynvgHtKYDoEkzelDxi5IuqAcyvCi11U24oIKcmNqkBHXsPvy7HvyDr-im1XDa0Y1ZOb_vEeexkjn0sYBLT5sxXzN_Tr-g6-wpJYPQs5172mSgifppmoUqeUkjI7VAjcoa3wAdQKYyqkPcGQ9swBbubPfvPb9muWKu-qjXJVVl4t7E86cu9w6jm8kdR9WOkeDHWWQZmmLz7WG1YusORghF6V1lRJ1-u6ZCI_6kipTXHO3atqf3azLJCXS6_gw3mEWb9nxZleN1MM0hCSE2J0yIZPGd-lnYkGmm-uDkH0T3PNkvxPoW4yoqahn9cbTfB3v80sI5P6LwDYMBqo8vWxIzgyPC-P-SsUCu_sKzbmDxR1Roob3_WQmZLnsFIk=w800-h740-no

Phase 2

A unit sphere (with radius as 1) with two points on the surface of the sphere A(lat1, lon1) and B(lat2, lon2), as well as some auxillary points and lines are drawn as follows. Point N is the north pole and point O is the center of the sphere.

https://lh3.googleusercontent.com/znUJwq9hAe48M1p11-9dbd1MQUH0J-Z8zdXGVbrS6bJXsPZagvRPIaEThpO1QeRQaxYeQ6pPwi-TmKMMNMExIj-Yg3TNIIJ5rULi1y2CA7yK0I4HuCYYs-Axmg6isYtVu19fJ1MRjh9cEYRN_S-xFo3yIp-MRiA35JV_DHoSgLBzZZmhGB7z8zjrufbAfFPUwPk7DPihz438-482d49HRuRWS6hRzxpsn150Pb13JjF0-_UTi2BqvPvOgMeABip_cLzr3u2VsAffsTEuwg0DIz0Vv_q1i3G5yLrjxZZHAi9VpoxwhpiCQSNvWmySLk5M-5kXxYS4estVgY8Q8EaNwg-NeG6i1SgFz2pNI9c-Md841QwqLCPubRj9fUUMrB4V2zwA8_lxUZXZ-ekjNqPoNV90ZxQAd3TSwD0pMDQyXSwmQx_Jp-DcjYCM6ydFiE45okT0Y0pZ9wQgMm_ptTS3fYc4-J1WIwE_PDW4jEvGwhvSg4VLAtCFWz2oQy5lggDuKESKpkTUqLr1KYnS3tihQSg_YrQsS4-io0PU4cvb588hrD_g1wy5Tjjif_Sp8S6xHQI2_HCqD5DwElY0Y_bwczUNbFwGtUbgDfU6nZk=w800-h772-no

We can tell that:

  • in the triangle formed by point A, O and C, the angle ∠AOC shows the difference between latitude values of A and B. Therefore the angle will be (or provided that lat1 and lat2 have been converted to radian)
  • the radius for a unit sphere is 1. Therefore, the length of chord AC will be according to the conclusion in phase1
  • as the difference of latitude between AC and BD are the same, the length of chord BD will be
  • similarly, in the triangle EOF, the angle ∠EOF shows the difference between longitude values of A and B. Therefore, the length of chord EF will be
  • AG is a perpendicular line to OE in the triangle AOE, the length of OG will be , which is
  • the ratio between the length of OG and OE is the length of OG since OE is the radius 1
  • OG is the radius of the circular plane parallel to the ecliptic plane at lat1. As lat1 goes near the north pole, OG goes to 0
  • the chord EF is at the ecliptic plane and the chord AD is at the circular plane at lat1. As lat1 goes near the north pole, AD goes to 0
  • we can tell that OG and AD will decrese to 0 at the same rate, which is the value of OG. Therefore, the length of AD will be , which is
  • similarly, the length of BC will be

At the end of derivation phase 2, we get the length of all 4 sides of a quadrilateral ▱ABCD

Phase 3

The 2-dimensional shape ▱ABCD derived from the previous phase is as follows. We need to calculate the length of segment AB (which is the chord of AB from the sphere).

https://lh3.googleusercontent.com/1HFx1i6EjpSYO7fZ61VWyQpuDNHA0HDi3ZVOHLkkqH8mflEsRZpw8O9ZjzZIE7WV_gpVzv0uyZplAEHw01-NNzsB2i79mAGmFTCncx8Kq3zbLoXCERWXMdTE_pwnkJmhT7pzg-c7x-WQYJtqOQ93c1ZwU348S3oeJCJwqj_3nu8a-Vg1oyOSGD39CMCZK9tPJEsrCyvxQ_zwKe0bqThvGc01oLHH7-4lzPkdB_va9H6YORasMJp5h-n4ab16gecH-l_luQbl27f8McON6IEM6qtBtvjOO8gFnswEJmCaPvV7CkhVD0EGhsZnFlRmmge_JOBVUKcE69B6mx5dWpwcM7GS21vvRSIvwQAElTtNQi2cOzQPOAQ9jZXYK6k8bX37xmqjmnz6tQV76UA2ZzNl1FzszqWuA2eue4zm3JOOIwNFgSgMuD9OSBrqE_Q_FkuqBdSsGIQYxftI9JjIdn2CN6cewHe-6hxM8fmtBEz0Wp37THVHCjgqdwnDLtC20enP1HGNb7UOfQwtNVDLTTOXInDJqzprJSSNlxokNshV4rXp0PEhU3ivQeDV_MV8ttnvLdMEbNx8O8VupuZkxV5rC_zID7i2-ZjB4NjaUL4=w800-h434-no

We can tell that:

  • AH is a perpendicular line to BC
  • as the shape is a isosceles trapezoid
  • by the Pythagorean Theorem
  • by the Pythagorean Theorem
  • By substituting results from the previous phase, eventually we will get that:
  • For convenience in the next phase, we define an intermediate value a to be half of the chord length AB squared. That is:

Phase 4

In the sphere in phase 2, the triangle formed by point A, B and O is as follows:

https://lh3.googleusercontent.com/zdxVDUYOicK_RukyWulixsTpMcR8I_TFSv-6-zSKymgXGZWNpiMgYLmcsfRuoUmvxpmALcncuG1VrZDsHstf8JAobJrNO_O1WzwcLHySBlhf29HhEwY67xuOqcoHL9UVa53jFRBHB6uUy4a5Vu248z0cY1jaFhhFTa2dQOkVlr9GQY0NhGakO3WTcVlg7Ep58M4L4rMb82L5buLo347T8GtVg-ZKWzUKW2Mvmb_ex0EdynvgHtKYDoEkzelDxi5IuqAcyvCi11U24oIKcmNqkBHXsPvy7HvyDr-im1XDa0Y1ZOb_vEeexkjn0sYBLT5sxXzN_Tr-g6-wpJYPQs5172mSgifppmoUqeUkjI7VAjcoa3wAdQKYyqkPcGQ9swBbubPfvPb9muWKu-qjXJVVl4t7E86cu9w6jm8kdR9WOkeDHWWQZmmLz7WG1YusORghF6V1lRJ1-u6ZCI_6kipTXHO3atqf3azLJCXS6_gw3mEWb9nxZleN1MM0hCSE2J0yIZPGd-lnYkGmm-uDkH0T3PNkvxPoW4yoqahn9cbTfB3v80sI5P6LwDYMBqo8vWxIzgyPC-P-SsUCu_sKzbmDxR1Roob3_WQmZLnsFIk=w800-h740-no

We can tell that:

  • OM is a perpendicular line to AB. As the triangle is isosceles, M will be the middle point of AB
  • from the previous phase, . Therefore,
  • by the Pythagorean Thereom
  • , and

Finale

The angle ∠AOB in radian tells about the spherical distance between A and B. In the case of a unit sphere, the distance between A and B will be ∠AOB in radian. In the case of the Earth, the answer will be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment