Java 检查一个点是否存在于圆扇形中
一个圆是一个闭合的形状,由一个点在平面上移动形成,使得它到给定的点的距离是恒定的。在本文中,我们将检查如何从给定的半径和圆心形成圆的方程。
我们将得到一个圆,一个点即(x,y),半径r和圆心即(x1,y1)。我们需要从给定的半径和圆心形成圆的方程。圆的方程由以下公式给出:
(x-x1)^2+(y-y1)^2 = r^2
其中,
- (x,y)是一个点。
- (x1,y1)是圆心。
- r是半径。
现在,要检查一个点是否存在于圆扇形中,该点必须满足以下方程:
(x-x1)^2+(y-y1)^2
让我们将圆的中心取为(0,0)。
让我们看看如何使用Java编程语言来检查一个点是否存在于圆扇形中。
为了给您展示一些实例
实例1
- 给定的点和半径的输入为-
- 点 = (2, 6),半径=3
- 检查条件后,结果将是-
- 点不存在于圆扇形中。
实例2
- 给定的点和半径的输入为-
- 点=(8, 5),半径=11
- 检查条件后,结果将是-
- 点存在于圆扇形中。
步骤
- 步骤1 - 声明并初始化变量。
- 步骤2 - 将值放入公式中。
- 步骤3 - 检查条件。
- 步骤4 - 打印结果。
多种方法
我们已经提供了不同的方法来解决问题。
- 使用静态输入
- 使用用户定义的方法
让我们逐个查看程序及其输出。
方法1:通过使用静态输入
在这种方法中,点(point)和半径(radius)的值将在程序中进行初始化。然后按照算法,我们将查找点是否存在于圆的扇形区域内。
示例
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编程语言探索了不同的方法来检查一个点是否存在于一个圆形扇区中。