Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An approximated solution (based on an equirectangular projection), <strong>much faster</strong> (it requires only 1 trig and 1 square root).</p> <p>This approximation is relevant if your points are not too far apart. It will <strong>always over-estimate</strong> compared to the real haversine distance. For example it will add <strong>no more than 0.05382 %</strong> to the real distance if the delta latitude or longitude between your two points <strong>does not exceed 4 decimal degrees</strong>.</p> <p>The standard formula (Haversine) is the <strong>exact</strong> one (that is, it works for any couple of longitude/latitude on earth) but is <strong>much slower</strong> as it needs 7 trigonometric and 2 square roots. If your couple of points are not too far apart, and absolute precision is not paramount, you can use this approximate version (Equirectangular), which is much faster as it uses only one trigonometric and one square root.</p> <pre><code>// Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2) int R = 6371; // km double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2); double y = (lat2 - lat1); double distance = Math.sqrt(x * x + y * y) * R; </code></pre> <p>You can <strong>optimize this further</strong> by either:</p> <ol> <li><strong>Removing the square root</strong> if you simply compare the distance to another (in that case compare both squared distance);</li> <li><strong>Factoring-out the cosine</strong> if you compute the distance from one master point to many others (in that case you do the equirectangular projection centered on the master point, so you can compute the cosine once for all comparisons).</li> </ol> <p>For more info see: <a href="http://www.movable-type.co.uk/scripts/latlong.html" rel="noreferrer">http://www.movable-type.co.uk/scripts/latlong.html</a></p> <p>There is a nice reference implementation of the Haversine formula in several languages at: <a href="http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe" rel="noreferrer">http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload