]> Shamusworld >> Repos - architektonas/blobdiff - src/geometry.cpp
Initial bring-back of line to line intersection detection.
[architektonas] / src / geometry.cpp
index e0ad270c019d3e297c61786efe00974218384caf..11e633f380aef100a282cda4eeb5bf52410f34e7 100644 (file)
 
 #include "geometry.h"
 #include <math.h>
-#include "circle.h"
-#include "dimension.h"
-#include "line.h"
+#include <stdio.h>
 #include "mathconstants.h"
 
 
+// This is unused
+#if 0
 Point Geometry::IntersectionOfLineAndLine(Point p1, Point p2, Point p3, Point p4)
 {
        // Find the intersection of the lines by formula:
@@ -42,6 +42,7 @@ Point Geometry::IntersectionOfLineAndLine(Point p1, Point p2, Point p3, Point p4
 
        return Point(px / d, py / d, 0);
 }
+#endif
 
 
 // Returns the parameter of a point in space to this vector. If the parameter
@@ -104,6 +105,15 @@ double Geometry::Determinant(Point p1, Point p2)
 }
 
 
+int Geometry::Intersects(Object * obj1, Object * obj2, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/)
+{
+       if ((obj1->type == OTLine) && (obj2->type == OTLine))
+               return CheckLineToLineIntersection(obj1, obj2, tp, up);
+
+       return 0;
+}
+
+
 /*
 Intersecting line segments:
 An easier way:
@@ -123,22 +133,16 @@ So check if the above two numbers are both >=0 and <=1.
 */
 
 
-#if 0
-// Finds the intesection between two objects (if any)
-bool Geometry::Intersects(Object * obj1, Object * obj2, double * t, double * s)
-{
-}
-#endif
-
-#if 0
 // Finds the intersection between two lines (if any)
-int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
+int Geometry::CheckLineToLineIntersection(Object * l1, Object * l2, double * tp, double * up)
 {
-       Vector r(l1->position, l1->endpoint);
-       Vector s(l2->position, l2->endpoint);
-       Vector v1 = l2->position - l1->position;        // q - p
-//     Vector v2 = l1->position - l2->position;        // p - q
-//printf("l1: (%lf, %lf) (%lf, %lf), l2: (%lf, %lf) (%lf, %lf)\n", l1->position.x, l1->position.y, l1->endpoint.x, l1->endpoint.y, l2->position.x, l2->position.y, l2->endpoint.x, l2->endpoint.y);
+       Vector r(l1->p[0], l1->p[1]);
+       Vector s(l2->p[0], l2->p[1]);
+       Vector v1 = l2->p[0] - l1->p[0];        // q - p
+#if 0
+       Vector v2 = l1->p[0] - l2->p[0];        // p - q
+printf("l1: (%lf, %lf) (%lf, %lf), l2: (%lf, %lf) (%lf, %lf)\n", l1->p[0].x, l1->p[0].y, l1->p[1].x, l1->p[1].y, l2->p[0].x, l2->p[0].y, l2->p[1].x, l2->p[1].y);
+#endif
        double rxs = (r.x * s.y) - (s.x * r.y);
        double t, u;
 
@@ -146,10 +150,12 @@ int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*
        {
                double qpxr = (v1.x * r.y) - (r.x * v1.y);
 
-//printf("  --> R x S = 0! (q - p) x r = %lf\n", qpxr);
-//printf("  -->(q - p) . r = %lf, r . r = %lf\n", v1.Dot(r), r.Dot(r));
-//printf("  -->(p - q) . s = %lf, s . s = %lf\n", v2.Dot(s), s.Dot(s));
-//printf("  -->(q - p) . s = %lf, (p - q) . r = %lf\n", v1.Dot(s), v2.Dot(r));
+#if 0
+printf("  --> R x S = 0! (q - p) x r = %lf\n", qpxr);
+printf("  -->(q - p) . r = %lf, r . r = %lf\n", v1.Dot(r), r.Dot(r));
+printf("  -->(p - q) . s = %lf, s . s = %lf\n", v2.Dot(s), s.Dot(s));
+printf("  -->(q - p) . s = %lf, (p - q) . r = %lf\n", v1.Dot(s), v2.Dot(r));
+#endif
 
                // Lines are parallel, so no intersection...
                if (qpxr != 0)
@@ -168,13 +174,13 @@ int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*
                        return 0;
 #else
                // Check to see which endpoints are connected... Four possibilities:
-               if (l1->position == l2->position)
+               if (l1->p[0] == l2->p[0])
                        t = 0, u = 0;
-               else if (l1->position == l2->endpoint)
+               else if (l1->p[0] == l2->p[1])
                        t = 0, u = 1.0;
-               else if (l1->endpoint == l2->position)
+               else if (l1->p[1] == l2->p[0])
                        t = 1.0, u = 0;
-               else if (l1->endpoint == l2->endpoint)
+               else if (l1->p[1] == l2->p[1])
                        t = 1.0, u = 1.0;
                else
                        return 0;
@@ -213,6 +219,7 @@ Now there are five cases (NOTE: only valid if vectors face the same way!):
 }
 
 
+#if 0
 // Finds the intersection between two lines (if any)
 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
 {