+ c² = a² + b² - 2ab·cos µ
+2ab·cos µ = a² + b² - c²
+ cos µ = (a² + b² - c²) / 2ab
+*/
+ // 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;
+}
+
+//
+// N.B.: l is the line, c is the circle
+//
+void Geometry::CheckLineToCircleIntersection(Object * l, Object * c)
+{
+ // Set up global vars
+ Global::numIntersectPoints = Global::numIntersectParams = 0;
+
+ // Step 1: Find shortest distance from center of circle to the infinite line
+ double t = ParameterOfLineAndPoint(l->p[0], l->p[1], c->p[0]);
+ Point p = l->p[0] + (Vector(l->p[0], l->p[1]) * t);
+ Vector radial = Vector(c->p[0], p);
+ double distance = radial.Magnitude();
+
+ // Step 2: See if we have 0, 1, or 2 intersection points
+
+ // Case #1: No intersection points
+ if (distance > c->radius[0])
+ return;
+ // Case #2: One intersection point (possibly--tangent)
+ else if (distance == c->radius[0])
+ {
+ // Only intersects if the parameter is on the line segment!
+ if ((t >= 0.0) && (t <= 1.0))
+ {
+ Global::intersectPoint[0] = c->p[0] + radial;
+ Global::numIntersectPoints = 1;
+ }
+
+ return;
+ }
+
+ // Case #3: Two intersection points (possibly--secant)
+
+ // So, we have the line, and the perpendicular from the center of the
+ // circle to the line. Now figure out where the intersection points are.
+ // This is a right triangle, though do we really know all the sides?
+ // Don't need to, 2 is enough for Pythagoras :-)
+ // Radius is the hypotenuse, so we have to use c² = a² + b² => a² = c² - b²
+ double perpendicularLength = sqrt((c->radius[0] * c->radius[0]) - (distance * distance));
+
+ // Now, find the intersection points using the length...
+ Vector lineUnit = Vector(l->p[0], l->p[1]).Unit();
+ Point i1 = p + (lineUnit * perpendicularLength);
+ Point i2 = p - (lineUnit * perpendicularLength);
+
+ // Next we need to see if they are on the line segment...
+ double u = ParameterOfLineAndPoint(l->p[0], l->p[1], i1);
+ double v = ParameterOfLineAndPoint(l->p[0], l->p[1], i2);
+
+ if ((u >= 0.0) && (u <= 1.0))
+ {
+ Global::intersectPoint[Global::numIntersectPoints] = i1;
+ Global::numIntersectPoints++;
+ }
+
+ if ((v >= 0.0) && (v <= 1.0))
+ {
+ Global::intersectPoint[Global::numIntersectPoints] = i2;
+ Global::numIntersectPoints++;
+ }
+}