title: Distance from Lat/Lng point to Minor Arc segment
body: <p>I need to calculate the shortest distance from a lat/lng GPS point P to a line segment described by 2 other lat/lng GPS points A and B.</p>
  
  <p>'Cross-track distance' helps me to calculate the shortest distance between P and the great circle described by A and B.</p>
  
  <p>However, this is not what I want. I need need the distance between P and the line <strong>segment</strong> of A-B, not the entire great circle.</p>
  
  <p>I have used the following implementation from <a href="http://www.movable-type.co.uk/scripts/latlong.html" rel="noreferrer">http://www.movable-type.co.uk/scripts/latlong.html</a></p>
  
  <pre><code>Formula:    dxt = asin( sin(δ13) ⋅ sin(θ13−θ12) ) ⋅ R
  where:
  δ13 is (angular) distance from start point to third point
  θ13 is (initial) bearing from start point to third point
  θ12 is (initial) bearing from start point to end point
  R is the earth’s radius
  </code></pre>
  
  <p>The following images hopefully demonstrate the problem I am trying to solve:
  <a href="https://i.stack.imgur.com/FmhxI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FmhxI.png" alt="Cross-Track distance Correct"></a>
  <a href="https://i.stack.imgur.com/XJTKC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XJTKC.png" alt="Cross-Track distance Incorrect"></a></p>
  
  <p>In the first image the Cross-Track distance, indicated by the <strong>green</strong> line is correct and indeed the shortest distance to the line segment AB.</p>
  
  <p>In the second image the problem with cross-track distance is shown, In this case I would want the shortest distance to be the simple distance AP, but Cross-Track distance gives me the distance indicated by the <strong>red</strong> line. </p>
  
  <p>How do I change my algoritm to take this into account, or check whether or not point X is within AB. Is it possible to do this computationally? Or is iterative the only possible (expensive) solution? (take N points along AB and calculate the min distance from P to all these points)</p>
  
  <p><em>For simplicity purposes all lines in the images are straight. In reality, these are minor arcs on a great circle</em></p>