Java 中判斷點是否在圓扇形內
圓是由一個在平面上運動的點所形成的封閉圖形,該點與一個給定點的距離保持恆定。在本文中,我們將瞭解如何根據給定的半徑和圓心來確定圓的方程。
我們將得到一個圓,其上有一個點,即 (x, y),半徑為 r,圓心為 (x1, y1)。我們需要根據給定的半徑和圓心來確定圓的方程。圓的方程由以下公式給出:
$$\mathrm{(x\:-\:x1)^2\:+\:(y\:-\:y1)^2\:=\:r^2}$$
其中,
(x, y) 是一個點。
(x1, y1) 是圓心。
r 是半徑。
現在,要檢查一個點是否在圓扇形內,該點必須滿足以下方程:
$$\mathrm{(x\:-\:x1)^2\:+\:(y\:-\:y1)^2\:<\:r^2}$$
讓我們將圓心設為 (0, 0)。
讓我們看看如何使用 Java 程式語言來檢查一個點是否在圓扇形內。
舉幾個例子
示例 1
給定點的輸入和半徑為:
點 = (2, 6),半徑 = 3
檢查條件後,結果將是:
點不在圓扇形內。
示例 2
給定點的輸入和半徑為:
點 = (8, 5),半徑 = 11
檢查條件後,結果將是:
點在圓扇形內。
演算法
步驟 1 - 宣告並初始化變數。
步驟 2 - 將值代入公式。
步驟 3 - 檢查條件。
步驟 4 - 列印結果。
多種方法
我們提供了多種方法來解決這個問題。
使用靜態輸入
使用使用者自定義方法
讓我們逐一檢視程式及其輸出。
方法 1:使用靜態輸入
在這種方法中,點和半徑的值將在程式中初始化。然後根據演算法,我們將找到點是否在圓扇形內。
示例
public class Main{
//main method
public static void main(String arr[]){
//declaring variables
double x = 2, y = 6, r = 3;
//applying logic
double m = x * x;
double n = y * y;
double o = r * r;
double p = m + n;
//checking the condition
if (p < o) {
//print if point lie inside circle
System.out.println("Point exist in circle sector.");
} else {
//print if point does not lie inside circle
System.out.println("Point does not exist in circle sector.");
}
}
}
輸出
Point does not exist in circle sector.
方法 2:使用使用者自定義方法
在這種方法中,點和半徑的值將在程式中初始化。然後透過傳遞給定值呼叫使用者自定義方法,並在方法內部根據演算法找到點是否在圓扇形內。
示例
public class Main{
//main method
public static void main(String arr[]){
//declaring variables
double x = 8, y = 5, r = 11;
//calling user defined method
sector_circle(x, y, r);
}
//user defined method
static void sector_circle(double x, double y, double r){
//applying logic
double m = x * x;
double n = y * y;
double o = r * r;
double p = m + n;
//checking the condition
if (p < o) {
//print if point lie inside circle
System.out.println("Point exists in circle sector.");
} else {
//print if point does not lie inside circle
System.out.println("Point does not exist in circle sector.");
}
}
}
輸出
Point exists in circle sector.
在本文中,我們探討了使用 Java 程式語言檢查點是否在圓扇形內的不同方法。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP