{
GetHovered(hover);
- double t, u;
- int numIntersecting = Geometry::Intersects((Object *)hover[0], (Object *)hover[1], &t, &u);
+// double t, u;
+// int numIntersecting = Geometry::Intersects((Object *)hover[0], (Object *)hover[1], &t, &u);
+ Geometry::Intersects((Object *)hover[0], (Object *)hover[1]);
+ int numIntersecting = Global::numIntersectParams;
+ double t = Global::intersectParam[0];
+ double u = Global::intersectParam[1];
if (numIntersecting > 0)
{
hoveringIntersection = true;
intersectionPoint = v1;
}
+
+ numIntersecting = Global::numIntersectPoints;
+
+ if (numIntersecting > 0)
+ {
+ Vector v1 = Global::intersectPoint[0];
+ QString text = tr("Intersection <%1, %2>");
+ informativeText = text.arg(v1.x).arg(v1.y);
+
+ hoveringIntersection = true;
+ intersectionPoint = v1;
+ }
}
// Do tool handling, if any are active...
#include "geometry.h"
#include <math.h>
#include <stdio.h>
+#include "global.h"
#include "mathconstants.h"
}
-int Geometry::Intersects(Object * obj1, Object * obj2, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/)
+void 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);
+ Global::numIntersectPoints = Global::numIntersectParams = 0;
- return 0;
+ if ((obj1->type == OTLine) && (obj2->type == OTLine))
+ CheckLineToLineIntersection(obj1, obj2);
+ else if ((obj1->type == OTCircle) && (obj2->type == OTCircle))
+ CheckCircleToCircleIntersection(obj1, obj2);
}
// Finds the intersection between two lines (if any)
-int Geometry::CheckLineToLineIntersection(Object * l1, Object * l2, double * tp, double * up)
+void Geometry::CheckLineToLineIntersection(Object * l1, Object * l2)//, double * tp, double * up)
{
+ 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
// 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
else if (l1->p[1] == l2->p[1])
t = 1.0, u = 1.0;
else
- return 0;
+ return;
#endif
}
else
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;
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;
}
// 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
// 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
static Point MirrorPointAroundLine(Point, Point, Point);
static Point RotatePointAroundPoint(Point, Point, double);
static double Determinant(Point, Point);
- static int Intersects(Object *, Object *, double * tp = 0, double * up = 0, double * vp = 0, double * wp = 0);
- static int CheckLineToLineIntersection(Object *, Object *, double *, double *);
+ static void Intersects(Object *, Object *);//, double * tp = 0, double * up = 0, double * vp = 0, double * wp = 0);
+ static void CheckLineToLineIntersection(Object *, Object *);
+ static void CheckCircleToCircleIntersection(Object *, Object *);//, Point *, Point *);
// static int Intersects(Line *, Line *, double * tp = 0, double * up = 0);
// static int Intersects(Line *, Dimension *, double * tp = 0, double * up = 0);
// static int Intersects(Line * l, Circle * c, double * tp = 0, double * up = 0, double * vp = 0, double * wp = 0);