-// Finds the intersection(s) between a circle and a circle (if any)
-// There can be 0, 1, or 2 intersections.
-// Returns the angles of the points of intersection in tp thru wp, with the
-// angles returned as c1, c2, c1, c2 (if applicable--in the 1 intersection case,
-// only the first two angles are returned: c1, c2).
-int Geometry::Intersects(Circle * c1, Circle * c2, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/, Point * p1/*= 0*/, Point * p2/*= 0*/)
-{
- // Get the distance between centers. If the distance plus the radius of the
- // smaller circle is less than the radius of the larger circle, there is no
- // intersection. If the distance is greater than the sum of the radii,
- // there is no intersection. If the distance is equal to the sum of the
- // radii, they are tangent and intersect at one point. Otherwise, they
- // intersect at two points.
- Vector centerLine(c1->position, c2->position);
- double d = centerLine.Magnitude();
-//printf("Circle #1: pos=<%lf, %lf>, r=%lf\n", c1->position.x, c1->position.y, c1->radius);
-//printf("Circle #2: pos=<%lf, %lf>, r=%lf\n", c2->position.x, c2->position.y, c2->radius);
-//printf("Distance between #1 & #2: %lf\n", d);
-
- // Check to see if we actually have an intersection, and return failure if not
- if ((fabs(c1->radius - c2->radius) > d) || ((c1->radius + c2->radius) < d))
- return 0;
-
- // There are *two* tangent cases!
- if (((c1->radius + c2->radius) == d) || (fabs(c1->radius - c2->radius) == d))
- {
- // Need to return something in tp & up!! !!! FIX !!! [DONE]
- if (tp)
- *tp = centerLine.Angle();
-
- if (up)
- *up = centerLine.Angle() + PI;
-
- return 1;
- }
-
- // 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
-// Not needed...!
- double a = sqrt((-d + c2->radius - c1->radius) * (-d - c2->radius + c1->radius) * (-d + c2->radius + c1->radius) * (d + c2->radius + c1->radius)) / d;
-
- // Now, you can use pythagorus to find the length of the hypotenuse, but we
- // already know that length, it's the radius! :-P
- // What's needed is the angle of the center line and the radial line. Since
- // there's two intersection points, there's also four angles (two for each
- // circle)!
- // We can use the arccos to find the angle using just the radius and the
- // distance to the perpendicular chord...!
- double angleC1 = acos(x / c1->radius);
- double angleC2 = acos((d - x) / c2->radius);
-
- if (tp)
- *tp = centerLine.Angle() - angleC1;
-
- if (up)
- *up = (centerLine.Angle() + PI) - angleC2;
-
- if (vp)
- *vp = centerLine.Angle() + angleC1;
-
- if (wp)
- *wp = (centerLine.Angle() + PI) + angleC2;
-
- if (p1)
- *p1 = c1->position + (centerLine.Unit() * x) + (Vector::Normal(Vector(), centerLine) * (a / 2.0));
-
- if (p2)
- *p2 = c1->position + (centerLine.Unit() * x) - (Vector::Normal(Vector(), centerLine) * (a / 2.0));
-
- return 2;
-}
-#endif
-