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 编程语言检查点是否在圆扇形内的不同方法。

更新于: 2023 年 5 月 4 日

291 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.