Java 检查一个点是在线的左侧还是右侧

Java 检查一个点是在线的左侧还是右侧

一条线由无数个点构成。在二维坐标系中,我们可以用两个值(即X和Y)来定义每个点。一个点基本上位于左侧或右侧,或者可以位于线上自身,只有在我们有它的坐标时才能定义这一点。

在这个程序中,我们将使用叉积法来找出点的方向。叉积法是通过对两个向量进行叉乘来找到第三个向量的方法。在我们的情况下,如果我们对线的起始点和用于查找方向的点进行叉积运算,结果可能是正值、负值或零。

如果我们得到一个正值,那么我们可以确定这个点位于线的右侧。如果我们得到一个负值,那么我们可以确定这个点位于线的左侧。如果是零,则该点位于线上的某个位置。

计算两个点A(x, y)和B(x, y)的叉积的公式:

Cross-Product = [A(x) X B(y)] – [B(x) X A(y)]

在这篇文章中,我们将看到如何使用Java来判断一个点是在一条直线的左侧还是右侧。

为了给你展示一些实例

实例1

The input point of line:
A(x, y) = (20, -20)
B(x, y) = (-30, -23)
The coordinate of the point:
P(x, y) = (4, 5) 
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (-50,-3)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (-16,-25)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] – [O2(x) X O1(y)]
                                        =[-50 X -25] – [-16 X -3]
                                        = 1250 – 48 = 1202

As the output is positive, hence the point is situated on the right side of the line

实例2

The input point of line:
A(x, y) = (-2, 4)
B(x, y) = (7, 8)
The coordinate of the point:
P(x, y) = (10,-20)
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (9, 4)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (12, -24)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] – [O2(x) X O1(y)]
                                       = [9 X -24] – [12 X 4]
                                       = -216 – 48 = -264
As the output is negative, hence the point is situated on the left side of the line.

步骤

步骤-1 - 通过用户输入或静态输入获取线段的两个点的坐标以及点的坐标。

步骤-2 - 找到线段的两个点之间的原点,以及线段上的一个点和该点之间的原点。

步骤-3 - 然后计算两个原点之间的叉乘。

步骤-4 - 如果上述计算的结果为正数、负数或零,则打印出点位于右侧、左侧或在线上。

多种方法

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

  • 使用静态输入值的用户定义方法。

  • 使用用户输入值的用户定义方法。

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

方法1:使用用户定义方法和静态输入值

在这种方法中,我们通过静态输入方法声明线段的点和点的坐标,并将这些值作为参数传递给我们的用户定义方法。然后通过在方法内部使用算法,我们可以找到点相对于线段的方向。

示例

public class Main{
   public static void main(String[] args) {
      pnt first = new pnt();
      pnt second = new pnt();
      pnt point = new pnt();
      first.x = -20;
      first.y = 15; 
      // first(-20, 15)
      second.x = 31;
      second.y = -18; 
      // second(31, -18)
      point.x = 32;
      point.y = 45; 
      // point(32, 45)
      int dir = dirOfPoint(first, second, point);
      if (dir == 1)
         System.out.println("The point is on Right Direction of the line.");
      else if (dir == -1)
         System.out.println("The point is on Left Direction of the line.");
      else
         System.out.println("Point is somewhere on the Line.");
   }
   static class pnt{
      int x, y;
   };
   static int R = 1, L = -1, Z = 0;
   static int dirOfPoint(pnt first,pnt second, pnt point) {
      second.x -= first.x;
      second.y -= first.y;
      point.x -= first.x;
      point.y -= first.y;
      int crs_prod = second.x * point.y - second.y * point.x;
      if (crs_prod > 0)
         return R;
      if (crs_prod < 0)
         return L;
         return Z;
   }
}

输出

The point is on Right Direction of the line.

方法2:使用自定义方法和用户输入值

在这种方法中,我们声明线的点和用户输入方法确定的点的坐标,并将这些值作为参数传递给我们的用户定义的方法。然后通过使用方法内的算法,我们可以找到点相对于线的方向。

例如

import java.util.*;
public class Main{
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      pnt first = new pnt();
      pnt second = new pnt();
      pnt point = new pnt();
      System.out.println("Enter the coordinates of first point of the line: ");
      System.out.println("--------------------------------------------------");
      System.out.print("X- value: ");
      first.x = sc.nextInt();
      System.out.print("Y- value: ");
      first.y = sc.nextInt();
      System.out.println("Enter the coordinates of second point of the line:");
      System.out.println("--------------------------------------------------");
      System.out.print("X value: ");
      second.x = sc.nextInt();
      System.out.print("Y value: ");
      second.y = -sc.nextInt();
      System.out.println("Enter the coordinates of the point which you want to navigate: ");
      System.out.println("--------------------------------------------------");
      System.out.print("X value: ");
      point.x = sc.nextInt();
      System.out.print("Y value: ");
      point.y = sc.nextInt();
      int dir = dirOfPoint(first, second, point);
      if (dir == 1)
         System.out.println("The point is on Right Direction of the line.");
      else if (dir == -1)
         System.out.println("The point is on Left Direction of the line.");
      else
         System.out.println("Point is somewhere on the Line.");
   }
   static class pnt{
      int x, y;
   };
   static int R = 1, L = -1, Z = 0;
   static int dirOfPoint(pnt first,pnt second, pnt point){
      second.x -= first.x;
      second.y -= first.y;
      point.x -= first.x;
      point.y -= first.y;
      int crs_prod = second.x * point.y - second.y * point.x;
      if (crs_prod > 0)
         return R;
      if (crs_prod < 0)
         return L;
         return Z;
   }
}

输出

Enter the coordinates of first point of the line:
---------------------------------------------------
X- value: 10
Y- value: 20
Enter the coordinates of second point of the line:
----------------------------------------------------
X value: -11
Y value: 12
Enter the coordinates of the point which you want to navigate:
----------------------------------------------------------------
X value: 4
Y value: 6
The point is on Right Direction of the line.

在本文中,我们探讨了如何使用不同的方法在Java中检查一个点是在一条线的左侧还是右侧。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程