]> Shamusworld >> Repos - architektonas/blobdiff - src/geometry.cpp
Added preliminary circle-to-circle intersection code.
[architektonas] / src / geometry.cpp
index e0ad270c019d3e297c61786efe00974218384caf..6ed6fab1020c70449e64955f4bc9c6c0548f28b8 100644 (file)
 
 #include "geometry.h"
 #include <math.h>
-#include "circle.h"
-#include "dimension.h"
-#include "line.h"
+#include <stdio.h>
+#include "global.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 +43,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
@@ -89,7 +91,6 @@ Point Geometry::MirrorPointAroundLine(Point point, Point tail, Point head)
 //
 Point Geometry::RotatePointAroundPoint(Point point, Point rotationPoint, double angle)
 {
-//     Vector v = Vector(point, rotationPoint);
        Vector v = Vector(rotationPoint, point);
        double px = (v.x * cos(angle)) - (v.y * sin(angle));
        double py = (v.x * sin(angle)) + (v.y * cos(angle));
@@ -104,6 +105,17 @@ double Geometry::Determinant(Point p1, Point p2)
 }
 
 
+void Geometry::Intersects(Object * obj1, Object * obj2)//, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/)
+{
+       Global::numIntersectPoints = Global::numIntersectParams = 0;
+
+       if ((obj1->type == OTLine) && (obj2->type == OTLine))
+               CheckLineToLineIntersection(obj1, obj2);
+       else if ((obj1->type == OTCircle) && (obj2->type == OTCircle))
+               CheckCircleToCircleIntersection(obj1, obj2);
+}
+
+
 /*
 Intersecting line segments:
 An easier way:
@@ -123,22 +135,18 @@ 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)
+// Finds the intersection between two lines (if any)
+void Geometry::CheckLineToLineIntersection(Object * l1, Object * l2)//, double * tp, double * up)
 {
-}
-#endif
+       Global::numIntersectPoints = Global::numIntersectParams = 0;
 
+       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
-// Finds the intersection between two lines (if any)
-int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
-{
-       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 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,14 +154,16 @@ 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)
-                       return 0;
+                       return;
 
 #if 0
 //this works IFF the vectors are pointing in the same direction. everything else
@@ -168,16 +178,16 @@ 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;
+                       return;
 #endif
        }
        else
@@ -199,6 +209,7 @@ Now there are five cases (NOTE: only valid if vectors face the same way!):
 5. Otherwise, the two line segments are not parallel but do not intersect.
 */
        // Return parameter values, if user passed in valid pointers
+#if 0
        if (tp)
                *tp = t;
 
@@ -210,9 +221,60 @@ Now there are five cases (NOTE: only valid if vectors face the same way!):
                return 1;
 
        return 0;
+#else
+       Global::intersectParam[0] = t;
+       Global::intersectParam[1] = u;
+
+       // If the parameters are in range, we have overlap!
+       if ((t >= 0) && (t <= 1.0) && (u >= 0) && (u <= 1.0))
+               Global::numIntersectParams = 1;
+#endif
 }
 
 
+void Geometry::CheckCircleToCircleIntersection(Object * c1, Object * c2)
+{
+       // Set up global vars
+       Global::numIntersectPoints = Global::numIntersectParams = 0;
+
+       // Get the distance between the centers of the circles
+       Vector centerLine(c1->p[0], c2->p[0]);
+       double d = centerLine.Magnitude();
+       double clAngle = centerLine.Angle();
+
+       // If the distance between centers is greater than the sum of the radii or
+       // less than the difference between the radii, there is NO intersection
+       if ((d > (c1->radius[0] + c2->radius[0]))
+               || (d < fabs(c1->radius[0] - c2->radius[0])))
+               return;
+
+       // If the distance between centers is equal to the sum of the radii or
+       // equal to the difference between the radii, the intersection is tangent
+       // to both circles.
+       if ((d == (c1->radius[0] + c2->radius[0]))
+               || (d == fabs(c1->radius[0] - c2->radius[0])))
+       {
+               Global::intersectPoint[0].x = c1->p[0].x + (cos(clAngle) * c1->radius[0]);
+               Global::intersectPoint[0].y = c1->p[0].y + (sin(clAngle) * c1->radius[0]);
+               Global::numIntersectPoints = 1;
+               return;
+       }
+
+       // Use the Law of Cosines to find the angle between the centerline and the
+       // radial line on Circle #1
+       double a = acos(((c1->radius[0] * c1->radius[0]) + (d * d) - (c2->radius[0] * c2->radius[0])) / (2.0 * c1->radius[0] * d));
+
+       // Finally, find the points of intersection by using +/- the angle found
+       // from the centerline's angle
+       Global::intersectPoint[0].x = c1->p[0].x + (cos(clAngle + a) * c1->radius[0]);
+       Global::intersectPoint[0].y = c1->p[0].y + (sin(clAngle + a) * c1->radius[0]);
+       Global::intersectPoint[1].x = c1->p[0].x + (cos(clAngle - a) * c1->radius[0]);
+       Global::intersectPoint[1].y = c1->p[0].y + (sin(clAngle - a) * c1->radius[0]);
+       Global::numIntersectPoints = 2;
+}
+
+
+#if 0
 // Finds the intersection between two lines (if any)
 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
 {
@@ -353,6 +415,9 @@ int Geometry::Intersects(Circle * c1, Circle * c2, double * tp/*= 0*/, double *
 
        // Find the distance from the center of c1 to the perpendicular chord
        // (which contains the points of intersection)
+       // [N.B.: This is derived from Pythagorus by using the unknown distance
+       //        from the center line to the point where the two radii coincide as
+       //        a common unknown to two instances of the formula.]
        double x = ((d * d) - (c2->radius * c2->radius) + (c1->radius * c1->radius))
                / (2.0 * d);
        // Find the the length of the perpendicular chord
@@ -393,9 +458,9 @@ int Geometry::Intersects(Circle * c1, Circle * c2, double * tp/*= 0*/, double *
 
 // should we just do common trig solves, like AAS, ASA, SAS, SSA?
 // Law of Cosines:
-// c^2 = a^2 + b^2 -2ab*cos(C)
+// c² = a² + b² - 2ab * cos(C)
 // Solving for C:
-// cos(C) = (c^2 - a^2 - b^2) / -2ab = (a^2 + b^2 - c^2) / 2ab
+// cos(C) = (c² - a² - b²) / -2ab = (a² + b² - c²) / 2ab
 // Law of Sines:
 // a / sin A = b / sin B = c / sin C
 
@@ -415,6 +480,17 @@ void Geometry::FindAnglesForSides(double s1, double s2, double s3, double * a1,
        // Use law of sines to find 2nd & 3rd angles
 // sin A / a = sin B / b
 // sin B = (sin A / a) * b
+// B = arcsin( sin A * (b / a))
+// ??? ==> B = A * arcsin(b / a)
+/*
+Well, look here:
+sin B = sin A * (b / a)
+sin B / sin A = b / a
+arcsin( sin B / sin A ) = arcsin( b / a )
+
+hmm... dunno...
+*/
+
        double angle2 = asin(s2 * (sin(angle1) / s1));
        double angle3 = asin(s3 * (sin(angle1) / s1));
 
@@ -428,3 +504,17 @@ void Geometry::FindAnglesForSides(double s1, double s2, double s3, double * a1,
                *a3 = angle3;
 }
 
+
+Point Geometry::GetPointForParameter(Object * obj, double t)
+{
+       if (obj->type == OTLine)
+       {
+               // Translate line vector to the origin, then add the scaled vector to
+               // initial point of the line.
+               Vector v = obj->p[1] - obj->p[0];
+               return obj->p[0] + (v * t);
+       }
+
+       return Point(0, 0);
+}
+