The shortest distance between two points is a straight line. This mathematical fact has even become a common expression in everyday speech. Technically, the distance in this term is the Euclidic distance. To calculate the distance between a point $$(x_1, y_1, z_1)$$ and a point $$(x_2,y_2,z_2)$$ in a three-dimensional space, you can use the following formula:  \[d=\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+(z_1-z_2)^2}\]

However, on the round shaped earth, one has to confide in route definition and travel roads that follow the ground. Here, the shortest distance between two points remains a straight line. The only difference, is that this line is defined a bit different. On a globe, a straight line follows a big circle. Such a circle is the intersecting line of the globe with an area that intersects with the center of the globe.

 

There are different formulas to determine the shortest distance between two points on a globe. From a numerical point of view, the haversine formula is the most stable formula. The word haversine is a contraction of halved versed sine, where the versed sine of an angle $$\theta$$ equals $$1-\cos(\theta)$$. If this is divided by two, that equals \[\frac{1-\cos(\theta)}{2} = \left(\sin\left(\frac{\theta}{2}\right)\right)^2\] If $$r$$ represents the earth's radius, the haversine formula to determine the distance $$d$$ between a point with co-ordinates $$(b_1,l_1)$$ and a point with co-ordinates $$(b_2,l_2)$$ is given by \[\begin{aligned}[c] a & = \left(\sin\left(\frac{b_2-b_1}{2}\right)\right)^2 + \cos(b_1)\cos(b_2)\left(\sin\left(\frac{l_2-l_1}{2}\right)\right)^2 \\ c & = \arctan\left(\sqrt{\frac{a}{1-a}}\right) \\ d & = 2rc \end{aligned}\]

If you want to know the Euclidian distance between two points on the earth's surface, you first have to convert their co-ordinates to $$x$$, $$y$$ and $$z$$ co-ordinates. The formulas for this are very simple. If a point has the degree of latitude and the degree of longitude $$(b,l)$$, you can convert this to $$x$$, $$y$$ and $$z$$ co-ordinates with the following formulas \[ \left\{ \begin{aligned}[c] x & = r\cos(b)\cos(l) \\ y & = r\cos(b)\sin(l) \\ z & = r\sin(b) \end{aligned} \right.\] Here, $$r$$ represents the earth's radius.

Assignment

Example

>>> latLng2Cartesian(0.0, 0.0)
(6371.0, 0.0, 0.0)
>>> euclideanDistance(0.0, 0.0, 0.0, 0.0)
0.0
>>> euclideanDistance(0.0, 0.0, 0.0, 180.0)
12742.0
>>> haversineDistance(0.0, 0.0, 0.0, 0.0)
0.0
>>> haversineDistance(0.0, 0.0, 0.0, 180.0)
20015.086796