+
+/*virtual*/ bool Line::HitTest(Point point)
+{
+// SaveState();
+
+ hitPoint1 = hitPoint2 = hitLine = false;
+ Vector lineSegment = endpoint - position;
+ Vector v1 = point - position;
+ Vector v2 = point - endpoint;
+ double t = Vector::Parameter(position, endpoint, point);
+ double distance;
+
+ // Geometric interpretation:
+ // The parameter "t" on the vector lineSegment is where the normal of
+ // lineSegment coincides with point. If t < 0, the normal lies beyond the
+ // 1st endpoint. If t > 1, then the normal lies beyond the 2nd endpoint. We
+ // only calculate the length of the normal between the point and the
+ // lineSegment when the parameter is between 0 and 1.
+
+ // Geometric interpretation of "distance = ?Det?(ls, v1) / |ls|":
+ // 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 0 0 1 0 0 0
+ // ex ey 1 ==> ex ey 1 ==> ex ey 0
+ // px py 1 px py 1 px py 0
+ //
+ // By translating the start point to the origin, and subtracting row 1 from
+ // all other rows, we end up with the matrix on the right which greatly
+ // simplifies the calculation of the determinant.
+
+ if (t < 0.0)
+ distance = v1.Magnitude();
+ else if (t > 1.0)
+ distance = v2.Magnitude();
+ else
+ // distance = ?Det?(ls, v1) / |ls|
+ distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y)
+ / lineSegment.Magnitude());
+
+ if ((v1.Magnitude() * Painter::zoom) < 8.0)
+ hitPoint1 = true;
+ else if ((v2.Magnitude() * Painter::zoom) < 8.0)
+ hitPoint2 = true;
+ else if ((distance * Painter::zoom) < 5.0)
+ hitLine = true;
+
+ return (hitPoint1 | hitPoint2 | hitLine ? true : false);
+// return StateChanged();
+}
+
+