X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fgeometry.cpp;h=95d12c65068b16715086191e1d70c7e01483dcbd;hb=bf5a50feb0f84a4627a65c5b82c3ca2d2eefe54b;hp=4cd229f76da7967d10811ed3ff803a0d46427e92;hpb=c20f82651fdc4dd26172bcb71a85e6eb29eacd43;p=architektonas diff --git a/src/geometry.cpp b/src/geometry.cpp index 4cd229f..95d12c6 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -39,6 +39,29 @@ double Geometry::ParameterOfLineAndPoint(Point tail, Point head, Point point) } +double Geometry::DistanceToLineFromPoint(Point tail, Point head, Point point) +{ + // Interpretation: given a line in the form x = a + tu, where u is the + // unit vector of the line, a is the tail and t is a parameter which + // describes the line, the distance of a point p to the line is given by: + // || (a - p) - ((a - p) . u) u || + // We go an extra step: we set the sign to reflect which side of the line + // it's on (+ == to the left if head points away from you, - == to the + // right) + Vector line(tail, head); + Vector u = line.Unit(); + Vector a_p = tail - point; + Vector dist = a_p - (u * (a_p).Dot(u)); + + double angle = Vector::Angle(tail, point) - line.Angle(); + + if (angle < 0) + angle += TAU; + + return dist.Magnitude() * (angle < HALF_TAU ? +1.0 : -1.0); +} + + Point Geometry::MirrorPointAroundLine(Point point, Point tail, Point head) { // Get the vector of the intersection of the line and the normal on the