在Java中根據給定半徑和圓心推導圓的方程
圓是由在平面上運動的點所形成的封閉圖形,該點到給定點的距離保持不變。在本文中,我們將學習如何根據給定的半徑和圓心推匯出圓的方程。
我們將得到一個圓心為 (x1, y1),半徑為 r 的圓。我們需要根據給定的半徑和圓心推匯出圓的方程。圓的方程由以下公式給出:
$$\mathrm{(x-x1)^2\:+\:(y-y1)^2\:=\:r^2}$$
展開並整理方程後,我們得到:
$$\mathrm{(x)^2\:–\:(2*x1*x)\:+\:(y)^2\:-\:(2*y1*y)\:=\:(r)^2\:-\:(x1)^2\:-\:(y1)^2}$$
讓我們開始吧!
舉幾個例子
示例1
給定的圓心和半徑為:
圓心 = (5, -2), 半徑 = 7
求出圓的方程後,結果將為:
圓的方程為:
$$\mathrm{x^2\:+\:(-10.0\:x)\:+\:y^2\:+\:(4.0\:y)\:=\:20.0.}$$
示例2
給定的圓心和半徑為:
圓心 = (9, 3), 半徑 = 7
求出圓的方程後,結果將為:
圓的方程為:
$$\mathrm{x^2\:+\:(-18.0\:x)\:+\:y^2\:+\:(-6.0\:y)\:=\:-41.0.}$$
演算法
步驟1 - 宣告並初始化變數。
步驟2 - 將值代入公式。
步驟3 - 獲取結果值。
步驟4 - 列印結果。
多種方法
我們提供了多種不同的方法來解決這個問題。
使用靜態輸入
使用使用者自定義方法
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入
在這種方法中,圓心和半徑的值將在程式中初始化。然後,根據演算法,我們將找到根據給定半徑和圓心推匯出的圓的方程。
示例
public class Main{
public static void main(String arg[]){
//declaring variables
double x1 = 9, y1 = 3, r = 7;
//applying logic
double m = -2 * x1;
double n = -2 * y1;
double o = (r * r) - (x1 * x1) - (y1 * y1);
// Printing the result
System.out.println("Equation of the circle is:");
System.out.println("x^2 + (" + m + " x) + " + "y^2 + ("+ n + " y) = " + o +".");
}
}
輸出
Equation of the circle is: x^2 + (-18.0 x) + y^2 + (-6.0 y) = -41.0.
方法2:使用使用者自定義方法
在這種方法中,圓心和半徑的值將在程式中初始化。然後,將呼叫使用者自定義方法,並將這些值作為引數傳遞。在方法內部,根據演算法,我們將找到根據給定半徑和圓心推匯出的圓的方程。
示例
public class Main{
//main method
public static void main(String arg[]){
//declaring variables
double x1 = 5, y1 = -2, r = 7;
//calling user defined method
equation_circle(x1, y1, r);
}
//user defined method
static void equation_circle(double x1, double y1, double r){
//applying logic
double m = -2 * x1;
double n = -2 * y1;
double o = (r * r) - (x1 * x1) - (y1 * y1);
// Printing the result
System.out.println("Equation of the circle is:");
System.out.println("x^2 + (" + m + " x) + " + "y^2 + ("+ n + " y) = " + o +".");
}
}
輸出
Equation of the circle is: x^2 + (-10.0 x) + y^2 + (4.0 y) = 20.0.
在本文中,我們探討了如何使用 Java 程式語言根據給定的半徑和圓心找到圓的方程。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP