Java Program to Check if a Line Touches or Intersects a Circle

bookmark

import java.awt.Point; 
import static java.lang.Math.*;
public class Main
{
    public static void main(String[] args)
    {
        //Static initialization of the line, center coordinates and the radius
        int a = 1, b = 1, c = -16;
        Point rad = new Point(0,0);
        double radius = 5;
        // Caclculates the distance between the center and the line
        double distance = (abs(a*rad.x+b*rad.y+c))/sqrt(a*a+b*b);
        // Prints the result
        if(radius==distance)
            System.out.println("The line the touches the circle");
        else if(radius>distance)
            System.out.println("The line the intersects the circle");
        else if(radius<distance)
            System.out.println("The line is outside the circle");
    }

 


Output:

The line is outside the circle