# 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;
// Caclculates the distance between the center and the line
// Prints the result
System.out.println("The line the touches the circle");
System.out.println("The line the intersects the circle");
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();

// Caclculates the distance between the center and the line

// Prints the minimum revloutions
System.out.println("The line the touches the circle");
System.out.println("The line the intersects the circle");
System.out.println("The line is outside the circle");
}
}

Output:

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

Case-2
Enter a, b and c
20 25 30
The line is outside the circle