Distance to Leg

The heavy lifting in terms of deciding the relationship between points and line is done the distToLeg function.

distToLeg(P1,P2,P0){\displaystyle \operatorname {distToLeg} (P_{1},P_{2}, P_{0})}

This start life as simple distToLine function and returned the distance from a point to to line as described in the previous section. Over time, it evolve into something more powerful and now returns multiple values as an object. Some of the values were used in previous incarnations of the program logic and others are there to provide information to aid debugging.
The key items are

  1. it calculates the distance from P0{\displaystyle P_{0}} to the line defined by P1{\displaystyle P_{1}} and P2{\displaystyle P_{2}}. If P0{\displaystyle P_{0}} doesn’t actually lie on the leg then this value is inflated to 1000m so it is effectively ignored.
  2. It calculates the distance from P0{\displaystyle P_{0}} to P1{\displaystyle P_{1}}
  3. It calculates the distance from P0{\displaystyle P_{0}} to P2{\displaystyle P_{2}}
  4. It gets the minimum value of the three previous calculations and returns that as dist.
  5. It sets a boolean isOnLine to indicate whether to point is indeed close to the line. Currently this is true if dist30m{\displaystyle \operatorname {dist}\leqslant 30m}.
  6. It sets a second boolean isNearLine which is true if dist60m{\displaystyle \operatorname {dist}\leqslant 60m}. The use of this will be seen later when we look at the continuing co-incidence of the two segments.
  7. If either of the values calculated in steps 2 or 3 is less than 30m then corresponding point is returned as ptA.
  8. Finally, a boolean isNearerP2 is returned to indicate which of the two leg defining points,P1{\displaystyle P_{1}} and {\displaystyle P_{2}}, ${\displaystyle P_{0}} is nearest to.

A lot of this historical and now the most important value returned is isOnLine and most subsequent decisions are based on this.