From 5e8399d16c6848e38b89cf3bf049bc61520e5887 Mon Sep 17 00:00:00 2001 From: danij Date: Mon, 22 Apr 2013 05:36:16 +0100 Subject: [PATCH] Partition: Added intersection; intercept; isParallelTo Derived from their libdeng1 counterparts. --- doomsday/client/include/partition.h | 53 ++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/doomsday/client/include/partition.h b/doomsday/client/include/partition.h index 4b3519354b..e94014585a 100644 --- a/doomsday/client/include/partition.h +++ b/doomsday/client/include/partition.h @@ -21,6 +21,7 @@ #ifndef DENG_MATH_PARTITION #define DENG_MATH_PARTITION +#include #include namespace de { @@ -63,9 +64,59 @@ class Partition * @param x X coordinate for the point. * @param y Y coordinate for the point. */ - ddouble pointOnSide(ddouble x, ddouble y) const { + inline ddouble pointOnSide(ddouble x, ddouble y) const { return pointOnSide(Vector2d(x, y)); } + + /** + * Returns @c true iff "this" line @a other are parallel. In special + * case where either of the two lines having a zero-length direction + * then @c true is always returned. + */ + bool isParallelTo(Partition const &other, ddouble epsilon = .99999999) const + { + ddouble len = direction.length(); + if(len == 0) return true; + + ddouble otherLen = other.direction.length(); + if(otherLen == 0) return true; + + ddouble dot = direction.dot(other.direction) / len / otherLen; + + // If it's close enough, we'll consider them parallel. + epsilon = de::abs(epsilon); + return dot > epsilon || dot < -epsilon; + } + + /** + * Determines how far along the line (relative to the origin) where the + * @a other line and "this" intersect. + * + * @return Intersection point expressed as a scale factor relative to the + * partition origin. In the special case of the two lines being parallel + * @c 0 is returned. + * + * @see intercept() + */ + double intersection(Partition const &other) const + { + ddouble divsor = direction.x * other.direction.y - + direction.y * other.direction.x; + + // Special case: parallel? + if(divsor == 0) return 0; + + Vector2d delta = origin - other.origin; + return (delta.y * other.direction.x - delta.x * other.direction.y) / divsor; + } + + /** + * Determine the intercept point where "this" line and @a other intersect + * and return the point at which the two intercept. + */ + inline Vector2d intercept(Partition const &other) const { + return origin + direction * intersection(other); + } }; } // namespace de