- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes

- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages

- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation

- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples

# 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.**

- Related Articles
- JAVA Program to Calculate Length of Chord of the Circle
- JAVA Program to Find Length of Longest Chord of a Circle
- Java Program to calculate distance light travels
- Find the length of tangent to a circle from a point at a distance of 5 cm from center of the circle of radius 3 cm.
- C++ Program to find the Shortest Distance to a character
- C++ code to get shortest distance from circular stations
- Program to find distance of shortest bridge between islands in Python
- Program to Find the Shortest Distance Between Two Points in C++
- JAVA Program to Calculate Radius of Circle with Given Width and Height of Arc
- C++ program to find the shortest distance between two nodes in BST
- The length of two parallel chords of a circle are $6\ cm$ and $8\ cm$. If the smaller chord is at a distance of $4\ cm$ from the centre, what is the distance of the other chord from the centre?
- Shortest Distance from All Buildings in C++
- The lengths of two parallel chords of a circle are \( 6 \mathrm{~cm} \) and \( 8 \mathrm{~cm} \). If the smaller chord is at distance \( 4 \mathrm{~cm} \) from the centre, what is the distance of the other chord from the centre?
- Swift Program to Calculate Area of Circle Inscribed in Square
- Find the length of a chord which is at a distance of $4\ cm$ from the centre of the circle of radius $6\ cm$.