Java 计算圆心到弦的最短距离
圆是一个没有拐角的二维图形。每个圆都有一个原点,圆上每个点到原点的距离都相等。原点到圆上某点的距离被称为圆的半径。同样地,如果我们从圆的一边画一条线到另一边,原点位于中间,那条线被称为圆的直径。基本上,直径的长度是半径长度的两倍。
圆的弦指的是从圆的一端点到另一端点的线。或者简单地说,弦指的是端点都在圆上的线。弦将圆分为两部分。
根据问题描述,我们需要找到圆心到圆的弦的最短距离。如我们所知,垂直距离是最短距离。
因此,从圆心画两条线到弦的两个端点,我们将得到一个三角形。垂直线将三角形等分为两部分。
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
如果我们在其中一个三角形中应用勾股定理,则有
\mathrm{P^{2}+(1/2)^{2}=r^{2}}
\mathrm{P=\sqrt{r^{2}}-(1^{2}/4)}
因此,我们继续查看如何使用Java编程语言找到从圆心到弦的最短距离。
举例说明−
示例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.
示例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.
示例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.
语法
要获得一个数字的平方根,我们可以使用java.lang包中Math类中的内置 sqrt() 方法。
以下是使用该方法获取任意数字的平方根的语法。
double squareRoot = Math.sqrt(input_vale)
同样,在Java中获取一个数的另一个数的幂的方法是使用内置的java.lang.Math.pow()方法。 下面是使用该方法获取2的幂的语法-
double power = Math.pow (inputValue,2)
步骤
步骤-1 - 通过静态输入或用户输入获取圆的半径和弦的长度。
步骤-2 - 通过使用公式找到从圆心到弦的最短距离。
步骤-3 - 打印结果。
多种方法
我们提供了不同的解决方案。
- 通过使用静态输入值。
-
通过使用用户定义的方法。
让我们逐一查看程序及其输出。
方法1:通过使用静态输入值
在这种方法中,我们在程序中初始化半径和弦长的值。然后通过使用算法,我们可以找到从圆心到弦的最短距离。
示例
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);
}
}
输出
The shortest distance: 5.656854249492381
方法2:通过使用具有静态输入值的用户定义方法
在这种方法中,我们在程序中将半径和弦长度的值作为用户输入。然后通过使用算法,我们可以找到从圆心到弦的最短距离。
示例
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);
}
}
输出
The shortest distance: 5.656854249492381
在这篇文章中,我们探讨了如何使用不同的方法在Java中找到从圆心到弦的最短距离。