In this article we are going to see how to check if two given circles intersect each other or not using Java programming language.
Java Program to Check if Two Given Circles Touch or Intersect Each Other
Before Jumping into the program directly let’s see how we can check if two given circles intersect each other or not.
Explanation:
Let us assume there are 2 circles namely A & B. The center of circle A is C1(x1,y1) and the radius is r1 The center of circle B is C2(x2,y2) and the radius is r2 Now the distance between the 2 centers of the circle can be written as : D = C1-C2 = sqrt(((x1 - x2)*2) + ((y1 - y2)*2)). Now, If D == R1 + R2, then Circle A and B touch to each other. If D > R1 + R2, then Circle A and B do not touch to each other. If D < R1 + R2, then Circle A and B intersects each other.
Example:
x1= 1 y1= 2 x2= 3 y2= 4 r1= 5 r2= 6 D = sqrt(((x1 - x2)*2) + ((y1 - y2)*2)) => sqrt(4+4) => sqrt(8) = 2.82 Now, r1 + r2 = 11 Since, D < r1 + r2, i.e 2.82<11 Therefore Circle A and B intersects each other.
Let’s see different ways to check if two given circles intersect each other or not.
Method-1: Java Program to Check if Two Given Circles Touch or Intersect Each Other By Using Static Value
Approach:
- Declare an integer variable say ‘
x1
’ & ‘y1
’ and assign the value to it, which holds the coordinate value of the center of circleA (Circle-1)
. - Declare a double variable say ‘
r1
’ and assign the value to it, which holds the value of radius of circle A. - Declare an integer variable say ‘
x2
’ & ‘y2
’ and assign the value to it, which holds the coordinate value of the center of circleB (Circle-2)
. - Declare a double variable say ‘
r2
’ and assign the value to which it holds the value of radius of circle B. - Declare a double variable say
D
which will hold the value of distance between the 2 centers of the circles using the formulasqrt(((x1 - x2)*2) + ((y1 - y2)*2))
. - If the value of
D == (R1 + R2)
, then print Circle A and B are touch to each other. - If the value of
D > (R1 + R2)
, then print Circle A and B are not touch to each other. - If the value of
D < (R1 + R2)
, then print Circle A and B intersects each other.
Program:
import java.io.*; class Main { public static void main(String [] args) { int x1 = 1; int y1 = 2; int x2 = 3; int y2 = 4; double r1 = 5; double r2 = 6; // formula to find distance between the centers double D = Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); // checking condition if(D == r1+r2) System.out.println("Two Circles touch Each Other"); else if(D > r1+r2) System.out.println("Two Circles do not touch Each Other"); else System.out.println("Two Circles intersect Each Other"); } }
Output: Two Circles intersect Each Other
Method-2: Java Program to Check if Two Given Circles Touch or Intersect Each Other By Using User Input Value
Approach:
- Declare an integer variable say ‘
x1
’ & ‘y1
’ which holds the coordinate value of the center of circleA(Circle-1)
. - Declare a double variable say ‘
r1
’ which holds the value of radius of circle A. - Declare an integer variable say ‘
x2
’ & ‘y2
’ which holds the coordinate value of the center of circleB(Circle-2)
. - Declare a double variable say ‘
r2
’ which it holds the value of radius of circle B. - Take the input of values of x1, y1, x2, y2, r1and r2 using Scanner class.
- Declare a double variable say
D
which will hold the value of distance between the 2 centers of the circles using the formulasqrt(((x1 - x2)*2) + ((y1 - y2)*2))
. - If the value of
D == (R1 + R2)
, then print Circle A and B are touch to each other. - If the value of
D > (R1 + R2)
, then print Circle A and B are not touch to each other. - If the value of
D < (R1 + R2)
, then print Circle A and B intersects each other.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object created Scanner s = new Scanner(System.in); // to take user input value of x1 System.out.println("Enter the value of x1 coordinate:"); int x1 = s.nextInt(); // to take user input value y1 System.out.println("Enter the value of y1 coordinate:"); int y1 = s.nextInt(); // to take user input value x2 System.out.println("Enter the value of x2 coordinate:"); int x2 = s.nextInt(); // to take user input value y2 System.out.println("Enter the value of y2 coordinate:"); int y2 = s.nextInt(); // to take user input value r1 i.e circle-1 radius System.out.println("Enter the value of radius of circle A:"); double r1 = s.nextDouble(); // to take user input value r2 i.e circle-2 radius System.out.println("Enter the value of radius of circle B:"); double r2 = s.nextDouble(); // formula to find distance between the centers double D = Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); // checking condition if(D == r1+r2) System.out.println("Two Circles touch Each Other"); else if(D > r1+r2) System.out.println("Two Circles do not touch Each Other"); else System.out.println("Two Circles intersect Each Other"); } }
Output: Enter the value of x1 coordinate: 1 Enter the value of y1 coordinate: 2 Enter the value of x2 coordinate: 3 Enter the value of y2 coordinate: 4 Enter the value of radius of circle A: 5 Enter the value of radius of circle B: 6 Two Circles intersect Each Other
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.