Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do you remember your trig functions? I.e. SOHCAHTOA:</p> <ul> <li><strong>SOH</strong>: Sin(θ) = Opposite over Hypotenuse</li> <li><strong>CAH</strong>: Cos(θ) = Adjacent over Hypotenuse</li> <li><strong>TOA</strong>: Tan(θ) = Opposite over Adjacent</li> </ul> <p>In pseudo-code:</p> <pre><code>function getDir(lat1, long1, lat2, long2) { margin = π/90; // 2 degree tolerance for cardinal directions o = lat1 - lat2; a = long1 - long2; angle = atan2(o,a); if (angle &gt; -margin &amp;&amp; angle &lt; margin): return "E"; elseif (angle &gt; π/2 - margin &amp;&amp; angle &lt; π/2 + margin): return "N"; elseif (angle &gt; π - margin &amp;&amp; angle &lt; -π + margin): return "W"; elseif (angle &gt; -π/2 - margin &amp;&amp; angle &lt; -π/2 + margin): return "S"; } if (angle &gt; 0 &amp;&amp; angle &lt; π/2) { return "NE"; } elseif (angle &gt; π/2 &amp;&amp; angle &lt; π) { return "NW"; } elseif (angle &gt; -π/2 &amp;&amp; angle &lt; 0) { return "SE"; } else { return "SW"; } } </code></pre> <p><strong>Edit 1:</strong> As Pete and Dean pointed out, this does not take into account the curvature of the Earth. For more accurate calculations for points away from the equator, you'll need to use <a href="http://williams.best.vwh.net/avform.htm#Crs" rel="nofollow noreferrer">spherical triangle formulas</a>, as used in Dean's answer.</p> <p><strong>Edit 2:</strong> Another correction; as Pete noted, <code>arctan()</code> does not give the correct angles, as -1/-1 and 1/1 are the same (as are -1/1 and 1/-1). <a href="http://en.wikipedia.org/wiki/Atan2" rel="nofollow noreferrer"><code>arctan2(y, x)</code></a> is a two argument variation of <code>arctan()</code> that is designed to compensate for this. <code>arctan()</code> has a range of (-π, π], positive for <code>y &gt;= 0</code> and negative for <code>y &lt; 0</code>.</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.
 

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