X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fgeometry.cpp;h=95d12c65068b16715086191e1d70c7e01483dcbd;hb=bf5a50feb0f84a4627a65c5b82c3ca2d2eefe54b;hp=b52ff7e97d058aaa7626b2568a0b667bc647c0d1;hpb=6533354910fbf76d9747deeae02b2e910ef9aa48;p=architektonas diff --git a/src/geometry.cpp b/src/geometry.cpp index b52ff7e..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 @@ -341,6 +364,13 @@ Point Geometry::GetPointForParameter(Object * obj, double t) } +Point Geometry::Midpoint(Line * line) +{ + return Point((line->p[0].x + line->p[1].x) / 2.0, + (line->p[0].y + line->p[1].y) / 2.0); +} + + /* How to find the tangent of a point off a circle: