Java Program to Check if Two Given Circles Touch or Intersect Each Other

bookmark

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