Two circles#4

Two circles (Editorial)

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 dist^2 = (x_2 - x_1)^2 + (y_2 - y_1)^2 – the square of the distance between the centers of the circles. Then:

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);