Java Program to Find the Shortest Distance from the Centre of a Circle to a Chord

In this article we are going to see how to calculate the shortest distance from the center of a circle to a chord using Java programming language.

Java Program to Find the Shortest Distance from the Centre of a Circle to a Chord

Before Jumping into the program directly let’s see how we can calculate the Shortest distance from the centre of a circle to a chord.

Explanation:

Let the length of the radius and the length of the cord of a given circle is “r” and “c” respectively.

We know, the line segment drawn from the center of the circle bisects the chord. The line is the perpendicular bisector of the chord. And we also know, the perpendicular distance is the shortest distance,

Radius of the circle = r

Length of the chord = c

Let the line segment of the chord = AC

The perpendicular bisector bisects the chord at point B i.e. AB=BC

And the center of the circle = O

In triangle OBC, using Pythagoras theorem:
H2 = P2 + B2
OC2 = OB2 + BC2

OB = √(OC2 – BC2)

OB = √(r2 – (c/2)2)

OB =  √(r2 – (c2/4))

Example:

r = 5

c = 8

Shortest distance =  √(r2 – (c2/4)) = 3

Let’s see different ways to find the shortest distance from the center of a circle to a chord.

Method-1: Java Program to Find the Shortest Distance from the Centre of a Circle to a Chord By Using Static Input Value

Approach:

• Declare a double variable say ‘r’ and assign the value to it, which holds the radius value of the circle.
• Declare a double variable say ‘c’ and assign the value to it, which holds the length of the chord of the circle.
• Find the shortest distance using the formula √(r2 - (c2/4))
• Print the result.

Program:

import java.io.*;
class Main
{
public static void main(String [] args)
{
double r = 5;
double c = 8;
double p =  Math.sqrt((r*r)-((c*c)/4)); // formula to find shortest distance
System.out.println("The shortest distance from the center to the chord of a the given circle is" + p);
}
}
Output:

The shortest distance from the center to the chord of a the given circle is3.0

Methord-2: Java Program to Find the Shortest Distance from the Centre of a Circle to a Chord By Using User Input Value

Approach:

• Declare an double variable say ‘r’ which holds the radius value of the circle.
• Declare an double variable say ‘c’ which holds the length of the chord of the circle.
• Then we will take the value of “r”, “c” as user input using scanner class.
• Find the shortest distance using the formula √(r2 - (c2/4))
• Print the result.

Program:

import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
// scanner class obj ref
System.out.println("Enter the length of radius of the circle");
// to take user input value
double r = s.nextDouble();
System.out.println("Enter the length of the chord of the circle");
double c =  s.nextDouble();
// formula to find shortest distance
double p =  Math.sqrt((r*r)-((c*c)/4));
System.out.println("The shortest distance from the center to the chord of a the given circle is" + p);
}
}
Output:

Enter the length of radius of the circle
7
Enter the length of the chord of the circle
10
The shortest distance from the center to the chord of a the given circle is4.898979485566356

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.