- Vector lineSegment = endpoint - position;
- Vector v1 = point - position;
- Vector v2 = point - endpoint;
- double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance;
-
- // Geometric interpretation:
- // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
- // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
- // then the perpendicular lies beyond the 2nd endpoint.
-
- if (parameterizedPoint < 0.0)
- distance = v1.Magnitude();
- else if (parameterizedPoint > lineSegment.Magnitude())
- distance = v2.Magnitude();
- else // distance = ?Det?(ls, v1) / |ls|
- distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude());
-
- // If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
- // intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
- // {e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
- // computed by first computing the area of the triangle the three points form, then dividing by the
- // length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
- // triangle formed by three points is the determinant of the following matrix:
- //
- // sx sy 1
- // ex ey 1
- // px py 1
- //
- // By translating the start point to the origin, this can be rewritten as:
- // By subtracting row 1 from all rows, you get the following:
- // [because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
- // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
- //
- // 0 0 0 0 0 0
- // (ex - sx) (ey - sy) 0 ==> ex ey 0
- // (px - sx) (py - sy) 0 px py 0
- //
- // which greatly simplifies the calculation of the determinant.
-
- if (state == OSInactive)