The process for finding a match in the routes is to loop through each point on the first segment and match it with every point on the second looking for a co-location. If the two roots have been encoded in a similar way, then it could be thought that just looking at the distance between the two points we would be able judge whether they are the same.
However, in practice, this is insufficient. Points on one line are often located between the points on the other and are close enough to the route that they a match.
I keep saying we comparing two points, but in practice, what this really means is that we are judging that point P0 against the leg of the walk from one point P1 to the nextP2.
Distance from a point to line
In order to handle this second case, we need to be able to calculate the distance from a point to a line. This is covered in great detail on this wikapedia page and the equations shown below are all taken from here.
In coordinate geometry, any point P(x,y) on straight line satisfies the formula ax+by+c=0 where a, b and c are constants.
If we know the location of two points on the line, P1=(x1,y1) and P2=(x2,y2) then we calculate the values of the constants in the equation abc=y2−y1=x1−x2=x2y1−x1y2
The distance from the point P0=(x0,y0) to the line is given by the formula distance(ax+by+c=0,(x0,y0))=a2+b2∣ax0+by0+c∣.
False Positives
This in itself is in sufficient. The line is not limited to the segment between P1 and P2 but extends to infinity in both directions. This means that the distance to this line could be small but the point itself is a long way from our route.
The point X on this line that is closest to P0=(x0,y0) has coordinates: x=a2+b2b(bx0−ay0)−ac and y=a2+b2a(−bx0+ay0)−bc
If X lies between P1 and P2 the following equation will hold distance(P1,X)+distance(X,P2))−distance(P1,P2)=0
and if not then the left hand side will return a positive value equal to twice the distance from X to the nearest point defining the leg.
Fallacy
It might be tempting to thing that we can work solely with the distance from the point to the leg of the segment that it lies on. There are two problems here
A point can lie very close to the route but not actually line between any two waypoints
And conversely it could actually lie between two pairs of waypoints at the same time.
The solution is to work with both approaches at the same time. i.e. distance of P0 to points P1 and P2, and to the line between them.