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

In the previous article, we have seen Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

In this article we will discuss about how to Check if a Line Touches or Intersects a Circle using Java programming language.

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

Before jumping into the program directly, let’s first know how can we Check if a Line Touches or Intersects a Circle .

Explanation:

To Check if a Line Touches or Intersects a Circle:

  1. Find the perpendicular distance between center of circle and given line.
  2. Compare this distance with the radius.
    1. If perpendicular > radius, then line lie outside the circle.
    2. If perpendicular = radius, then line touches the circle.
    3. If perpendicular < radius, then line intersect the circle.

Let’s see different ways to to check if a line touches or intersects a circle.

Method-1: Java Program to Check if a Line Touches or Intersects a Circle By Using Static Value

Approach:

  • Declare the value for ‘a’, ‘b’ and ‘c’, coordinates of the center and size of the radius.
  • Then calculate the perpendicular distance between the center of the circle and the line.
  • Compare the perpendicular distance with radius and print the result.

Program:

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

Method-2: Java Program to Check if a Line Touches or Intersects a Circle By User Input Value

Approach:

  • Take user input of value for ‘a’, ‘b’ and ‘c’, coordinates of the center and size of the radius.
  • Then calculate the perpendicular distance between the center of the circle and the line.
  • Compare the perpendicular distance with radius and print the result.

Program:

import java.awt.Point; 
import java.util.*;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        //Asking the user to input the line, center coordinates and the radius
        System.out.println("Enter a, b and c");
        int a = scan.nextInt(), b = scan.nextInt(), c= scan.nextInt();
        System.out.println("Enter coordinates of the radius");
        Point rad = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter the radius");
        double radius = scan.nextDouble();
        
        // 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 minimum revloutions
        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:

Case-1
Enter a, b and c
1 -1 0
Enter coordinates of the radius
0 0
Enter the radius
5
The line the intersects the circle

Case-2
Enter a, b and c
20 25 30
Enter coordinates of the radius
10 11
Enter the radius
9
The line is outside the circle

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Articles: