JAVA Program to Calculate Shortest Distance from Center of the Circle to Chord


A circle is a round shape two-dimensional diagram which has no corners. Every circle has an origin point and every point on the circle maintains equal distance from the origin. The distance between the origin and a point in a circle is known as Radius of the circle. And similarly, if we draw a line from one edge to another edge of the circle and the origin is held in the middle of it, that line is known as diameter of the circle. Basically, the diameter is double of the length of the radius.

Chord of the circle refers to a line touching from one endpoint of circle to another end point of circle. Or simply we can say a chord refers to the line whose endpoints lie on the circle. A chord divides the circle into two parts.

As per the problem statement we have to find the shortest distance from the centre of a circle to a chord of the circle. As we know, the perpendicular distance is the shortest distance.

So, from the centre of the circle if we draw two lines to the both end points of the chord then we will get a triangle. The perpendicular line will divide the triangle in two equal parts.

Let the radius of circle = r
Length of the chord = l
So, if perpendicular line bisects the chords in two equal parts, then length of one equal part = l/2
Perpendicular line (bisector line) = p

If we will apply Pythagoras theorem in one of the triangles then

$$\mathrm{P^{2}+(1/2)^{2}=r^{2}}$$

$$\mathrm{P=\sqrt{r^{2}}-(1^{2}/4)}$$

So, let's proceed to see how we can find the shortest distance from the centre of a circle to a chord by using Java programming language.

To show you some instances −

Instance-1

Given radius (r) of the circle is 5
Length of the chord (l) is 4
Then the shortest distance from centre of the circle to the chord = sqrt((r * r) - ((l * l) / 4)) = sqrt((5 * 5) - ((4 * 4) / 4)) = 4.58.

Instance-2

Given radius (r) of the circle is 5.5.
Length of the chord (l) is 3.5
Then the shortest distance from centre of the circle to the chord = sqrt((r * r) - ((l * l) / 4)) = sqrt((4.5 * 4.5) - ((3.5 * 3.5) / 4)) = 4.14.

Instance-3

Given radius (r) of the circle is 6.
Length of the chord (l) is 4.
Then the shortest distance from centre of the circle to the chord = sqrt((r * r) - ((l * l) / 4)) = sqrt((6 * 6) - ((4 * 4) / 4)) = 5.65.

Syntax

To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.

Following is the syntax to get square root of any number by using the method.

double squareRoot = Math.sqrt(input_vale)

Similarly, to get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method −

double power = Math.pow (inputValue,2)

Algorithm

Step-1 − Get the radius of the circle and length of the chord either by static input or by user input.

Step-2 − Find the shortest distance from the centre of the circle to the chord by using the formula.

Step-3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value.

  • By Using User-Defined Method.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we initialize the radius and chord length value in the program. Then by using the algorithm we can find the shortest distance from the centre of the circle to the chord.

Example

public class Main {
   //main method
   public static void main(String[] args) {
      //radius of circle
      double r = 6;
      //length of chord
      double l = 4;
      //find shortest distance by using formula
      double distance = Math.sqrt((r * r) - ((l * l) / 4));
      System.out.println("The shortest distance: "+distance);
   }
}

Output

The shortest distance: 5.656854249492381

Approach-2: By Using User Defined Method with Static Input Value.

In this approach, we take the value of the radius and chord length as user input in the program. Then by using the algorithm we can find the shortest distance from the centre of the circle to the chord.

Example

public class Main {
   //main method
   public static void main(String[] args) {
      double r = 6, l = 4;
      findShortestDistance(r, l);
   }
   //user defined method to find the shortest distance
   static void findShortestDistance(double r, double l) {
      //find shortest distance by using formula
      double distance = Math.sqrt((r * r) - ((l * l) / 4));
      System.out.println("The shortest distance: "+distance);
   }
}

Output

The shortest distance: 5.656854249492381

In this article, we explored how to find the shortest distance from the centre of a circle to a chord in Java by using different approaches.

Updated on: 27-Dec-2022

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements