68.8k views
5 votes
Write a method that prints on the screen a message stating whether 2 circles touch each other, do not touch each other or intersect. The method accepts the coordinates of the center of the first circle and its radius, and the coordinates of the center of the second circle and its radius.

The header of the method is as follows:

public static void checkIntersection(double x1, double y1, double r1, double x2, double y2, double r2)


Hint:

Distance between centers C1 and C2 is calculated as follows:
d = Math.sqrt((x1 - x2)2 + (y1 - y2)2).

There are three conditions that arise:

1. If d == r1 + r2
Circles touch each other.
2. If d > r1 + r2
Circles do not touch each other.
3. If d < r1 + r2
Circles intersect.


1 Answer

4 votes

Answer:

The method is as follows:

public static void checkIntersection(double x1, double y1, double r1, double x2, double y2, double r2){

double d = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));

if(d == r1 + r2){

System.out.print("The circles touch each other"); }

else if(d > r1 + r2){

System.out.print("The circles do not touch each other"); }

else{

System.out.print("The circles intersect"); }

}

Step-by-step explanation:

This defines the method

public static void checkIntersection(double x1, double y1, double r1, double x2, double y2, double r2){

This calculate the distance

double d = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));

If the distance equals the sum of both radii, then the circles touch one another

if(d == r1 + r2){

System.out.print("The circles touch each other"); }

If the distance is greater than the sum of both radii, then the circles do not touch one another

else if(d > r1 + r2){

System.out.print("The circles do not touch each other"); }

If the distance is less than the sum of both radii, then the circles intersect

else{

System.out.print("The circles intersect"); }

}

User Mar Cnu
by
3.8k points