private ProjectionLocation LocateProjection( DoublePoint point )
{
// Modified from http://www.codeguru.com/forum/showthread.php?t=194400
/* How do I find the distance from a point to a line segment?
Let the point be C (Cx,Cy) and the line be AB (Ax,Ay) to (Bx,By).
Let P be the point of perpendicular projection of C on AB. The parameter
r, which indicates P's position along AB, is computed by the dot product
of AC and AB divided by the square of the length of AB:
(1) AC dot AB
r = ---------
||AB||^2
r has the following meaning:
r=0 P = A
r=1 P = B
r<0 P is on the backward extension of AB (and distance C-AB is distance C-A)
r>1 P is on the forward extension of AB (and distance C-AB is distance C-B)
0<r<1 P is interior to AB (and distance C-AB(segment) is distance C-AB(line))
The length of the line segment AB is computed by:
L = sqrt( (Bx-Ax)^2 + (By-Ay)^2 )
and the dot product of two 2D vectors, U dot V, is computed:
D = (Ux * Vx) + (Uy * Vy)
So (1) expands to:
(Cx-Ax)(Bx-Ax) + (Cy-Ay)(By-Ay)
r = -------------------------------
(Bx-Ax)^2 + (By-Ay)^2
*/
// the above is modified here to compare the numerator and denominator, rather than doing the division
DoublePoint abDelta = end - start;
DoublePoint acDelta = point - start;
double numerator = acDelta.X * abDelta.X + acDelta.Y * abDelta.Y;
double denomenator = abDelta.X * abDelta.X + abDelta.Y * abDelta.Y;
ProjectionLocation result = ( numerator < 0 ) ? ProjectionLocation.RayA : ( numerator > denomenator ) ? ProjectionLocation.RayB : ProjectionLocation.SegmentAB;
return result;
}