計算圓的弦長的JAVA程式
圓是一個沒有角的二維圓形圖形。每個圓都有一個原點,圓上的每個點到原點的距離都相等。原點和圓上一點之間的距離稱為圓的半徑。同樣,如果我們從圓的一端到另一端畫一條線,並且原點位於該線段的中間,則該線段稱為圓的直徑。基本上,直徑是半徑長度的兩倍。
圓的弦是指連線圓上兩點的一條線段。或者簡單地說,弦是指端點位於圓上的線段。弦將圓分成兩部分。
根據題意,我們必須在已知圓的半徑和絃在圓心處所對的角的情況下,求弦長。
求弦長的公式:
Length = 2 * r * sin(a/2)
其中,“r”表示圓的半徑,“a”表示弦在圓心處所對的角。
讓我們開始探索。
為了向您展示一些示例:
示例1
Let say radius (r) of circle is 3. And angle (a) subtended at centre by the chord is 60 Then by using the formula, length of the chord is 5.19.
示例2
Let say radius (r) of circle is 4. And angle (a) subtended at centre by the chord is 50 Then by using the formula, length of the chord is 6.12.
示例3
Let say radius (r) of circle is 4. And angle (a) subtended at centre by the chord is 40. Then by using the formula, length of the chord is 5.1.
演算法
步驟1 - 透過靜態輸入或使用者輸入獲取圓的半徑和絃在圓心處所對的角。
步驟2 - 使用公式求弦長。
步驟3 - 列印結果。
多種方法
我們提供了多種不同的方法來解決這個問題。
使用靜態輸入值。
使用使用者輸入值。
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入值
在這種方法中,我們在程式中初始化半徑和角度值。然後,使用演算法可以找到弦長。
示例
public class Main {
//main method
public static void main(String[] args) {
//radius of circle
double r = 4;
//angle subtended by chord at center
double a = 40;
//find length of chord by using formula
double length = 2 * r * Math.sin(a * (3.14 / 180));
System.out.println("Length of Chord: "+length);
}
}
輸出
Length of Chord: 5.140131589369607
方法2:使用帶靜態輸入值的自定義方法。
在這種方法中,我們獲取程式中半徑和角度值的用輸入。然後,透過將這些值作為引數傳遞來呼叫使用者定義的方法,並在方法內部使用演算法來找到弦長。
示例
public class Main {
//main method
public static void main(String[] args) {
//radius of circle
double r = 4;
//angle subtended by chord at center
double a = 40;
length_of_chord(r, a);
}
//user defined method to find the length of the chord
static void length_of_chord(double r, double a) {
double length = 2 * r * Math.sin(a * (3.14 / 180));
System.out.println("Length of Chord: "+length);
}
}
輸出
Length of Chord: 5.140131589369607
在本文中,我們探討了如何在Java中使用不同的方法來求解已知圓的半徑和絃在圓心處所對的角的情況下求弦長的問題。
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP