Finding the Start Point

Armed with distToLeg finding the start of the overlap is relatively easy.
We work our way along the segment SegA taking each leg and testing every waypoint on SegB until distToLeg returns true for isOnLine. We now have a candidate start point for a common part of the routes.

If the two walks are walking the path in the same direction then we maybe done.

Reverse Direction

But if SegB is doing it in the opposite direction then there is an extra factor to consider.

We tested WP1022 against WP3001 … WP3035, WP3036 and it’s only when we hit WP3037 do we get a match. But because we are coming from the opposite direction there maybe other waypoints that are a better choice for the start of the common section. In this case we would continue to test the next few points on SegB to check if are also close to SegA. In this instance it would determine that it start at WP3040.
We consider B is coming from the opposite direction if

dist(WP1022,WP3028)<dist(WP1022,WP2027){\displaystyle \operatorname {dist}(\text {WP1022}, \text {WP3028}) \lt \operatorname {dist}(\text {WP1022}, \text {WP2027})}

i.e. is the next point on B getting us closer to WP1022 or moving away. This test isn’t perfect but is good enough for our purposes.

Joining Mid Leg

If the start point involves points on SegA and SegB that are close to each other then we done.

Otherwise it will be that the point from SegB lies in the middle of the leg on SegA.

There are two ways that this could be interpreted. In the two charts below, the search so far has given the start of the common section as being WP3037.


This is correct because SegB has joined the path being followed by SegA.


Here SegA is joining the path being followed by SegB and so the common section really starts at WP1022.

To determine if this the case we need a final check. If WP1022 lies on the leg of SegB from WP3026 to WP3027 then we need to adjust the start to WP1022

The Start Point

The function to find the start point returns an object encapsulating all the data.

  • The key field is type which has 3 possible values
    • AB if there is a point common to both segments
    • A if the start is a point on SegA. i.e. A joins B.
    • B if the start is a point on SegB. i.e. B joins A.
  • iA and iB the index to the relevant points on the segments.
  • ptA and ptB the actual points involved at the start. May be undefined.