Java 如何检查3个有序点的方向

Java 如何检查3个有序点的方向

在本文中,我们将找出3个有序点的方向。这里的方向指的是给定的点在空间中形成顺时针、逆时针或共线形状。

在上图中,a、b、c是检查空间中形状方向的三个点。通过计算斜率,我们可以找到三个给定点的方向。

计算斜率和计算3个有序点的方向。

线段的斜率

线段(a,b)的斜率:\theta=\left ( y_{b}-y_{a} \right )/\left ( x_{b} -x_{a}\right )

线段(b,c)的斜率:\phi=\left ( y_{c} -y_{b}\right )/(x_{c}-x_{b})

因此,方向取决于以下表达式:

(y_{b}-y_{a})(x_{c}-x_{b})-(y_{c}-y_{b})(x_{b}-x_{a}):Or:(y2-y1)*(x3-x2)-(y3-y2)*(x2-x1)

即,它是正的、负的还是

  • 如果表达式为零,则\theta=\phi。因此方向是共线的。

  • 如果表达式为负,则\theta<\phi。因此方向是逆时针的。

  • 如果表达式为正,则\theta>\phi。因此方向是顺时针的。

开始吧!

举例说明

实例-1

假设有序点为(0,3), (4,2), (3,1)

检查有序点的方向后,结果将是:

给定的3个点形成:顺时针

实例-2

假设有序点为(0,3), (1,2), (9,5)

检查有序点的方向后,结果将是:

给定的3个点形成:逆时针

实例-3

假设有序点为(2,2), (3,3), (4,4)

检查有序点的方向后,结果将是:

给定的3个点形成:共线

步骤

步骤1 - 声明3个有序点。

步骤2 - 将三个给定点传递给表达式,即(b.y – a.y) * (c.x – b.x) – (b.x – a.x) * (c.y – b.y)。

步骤3 - 检查共线、顺时针和逆时针的条件。

步骤4 - 打印结果。

多种方法

我们以不同的方法提供了解决方案。

  • 通过使用静态输入

  • 通过使用用户定义的方法

让我们逐个查看程序及其输出。

方法1:通过使用静态输入

在这个方法中,首先将三个点传递给表达式,以检查线性、顺时针和逆时针的条件。然后将结果打印到输出。

示例

public class Main{
   //main method
   public static void main(String[] args){
      //Declaring variables
      int x1=0, y1=1;
      int x2=4, y2=3;
      int x3=3, y3=2;

      //expression to check for 3 ordered point
      int val = (y2 - y1) * (x3 - x2) - (x2 - x1) * (y3 - y2);

      // check for collinear
      if (val == 0){
         //printing collinear orientation
         System.out.print("The given 3 points form : Linear");
      }

      //check for clockwise
      else if(val > 0){

         //printing clockwise orientation
         System.out.print("The given 3 points form: Clockwise");
      } else {

         //printig counter clockwise orientation
         System.out.print("The given 3 points form: CounterClockwise");
      }
   }
}

输出

The given 3 points form: Clockwise

方法2:使用用户定义的方法

在这种方法中,首先将3个点通过一个用户定义的方法传递给表达式,以检查线性、顺时针和逆时针的条件。然后将结果打印到输出。

示例

public class Main {
   public static void main(String[] args){
      Point a = new Point(2, 2);
      Point b = new Point(3, 3);
      Point c = new Point(4, 4);

      //calling user defined method
      int o = orientation(a, b, c);

      //check for Linear orientation
      if (o==0) 

         //printing Linear orientation
         System.out.print("The given 3 points form : Linear");

      //check for Clockwise orientation
      else if (o == 1)

         //printing clockwise orientation
         System.out.print("The given 3 points form : Clockwise");
      else

         //printing counter clockwise orientation
         System.out.print("The given 3 points form : CounterClockwise");
   }

   // user defined method
   public static int orientation(Point a, Point b,  Point c){

     //expression to check for 3 ordered point
      int val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);

      // check for collinear
      if (val == 0) return 0; 

      // check for clock or counterclock wise
      return (val > 0)? 1: 2;
   }
}
class Point{
   int x, y;
   Point(int x,int y){
      this.x=x;
      this.y=y;
   }
}

输出

The given 3 points form : Linear

在本文中,我们探讨了如何使用Java编程语言检查3个有序点的方向。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程