Algorithm Analysis
It is necessary to consider the options of external and internal tangency, as well as the possibility of one circle being inside another. Circles have an infinite number of intersection points only in the case of their coincidence.
Let – the square of the distance between the centers of the circles. Then:
Circles touch externally if .
Circles have a point of internal tangency if .
Circles do not have common points and do not contain one inside the other if .
Circles do not intersect but are contained one inside the other if .
Algorithm Implementation
First, let's assume we have two intersection points.
int res = 2;
Read the input data.
scanf("%lf %lf %lf %lf %lf %lf", &xx1, &yy1, &r1, &xx2, &yy2, &r2);
Calculate the square of the distance between the centers of the circles.
dist2 = (xx2-xx1)*(xx2-xx1) + (yy2-yy1)*(yy2-yy1);
Check if the circles do not coincide.
if ((xx1 == xx2) && (yy1 == yy2) && (r1 == r2)) res = -1; else
Check the condition for the external tangency of two circles.
if (fabs((r1 + r2) * (r1 + r2) - dist2) < EPS) res = 1; else
Check the condition for the internal tangency of two circles.
if (fabs((r1 - r2) * (r1 - r2) - dist2) < EPS) res = 1; else
Consider cases when circles do not have common points.
if ((r1 + r2)*(r1 + r2) < dist2 - EPS) res = 0; else if ((r1 - r2)*(r1 - r2) > dist2 + EPS) res = 0;
Output the answer. If none of the above conditions are met, then we have two points of intersection.
printf("%d\n", res);