Java程式檢查點在直線的左側還是右側


一條直線由無限多個點組成。在二維座標系中,我們可以用兩個值(即XY)來定義每個點。一個點基本上位於左側或右側,或者它可以位於直線上本身,只有當我們擁有它的座標時才能定義這一點。

在這個程式中,我們將使用叉積方法來找到點的方向。叉積方法用於透過對兩個向量進行叉乘來找到第三個向量。在我們的例子中,如果我們對直線的原點和用於查詢方向的給定點進行叉乘。那麼我們將得到一個正值、一個負值或零。

如果我們得到一個正值,那麼我們可以確定該點位於直線的右側。如果我們得到一個負值,那麼我們可以確定該點位於直線的左側。如果為零,則該點位於直線上的某個位置。

計算兩個點A(x, y)B(x, y)的叉積的公式−

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

在這篇文章中,我們將瞭解如何使用Java來查詢點是在直線的左側還是右側。

問題陳述

用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

輸出1

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

輸出2

As the output is negative, hence the point is situated on the left side of the line.

多種方法

我們提供了不同方法的解決方案−

使用靜態輸入值

在這種方法中,我們透過靜態輸入方法宣告直線的點和點的座標,並將這些值作為引數傳遞給我們的使用者定義方法。然後,透過使用方法內的演算法,我們可以找到點相對於直線的方向。

  • 首先建立Main類並定義main方法。
  • 建立三個點物件:first、second和point。
  • 為直線的端點first和second分配靜態值。
  • 為要檢查的點point分配靜態值。
  • 使用三個點物件呼叫dirOfPoint方法來計算方向。
  • 如果返回值為1,則列印該點位於直線的右側;如果返回值為-1,則列印該點位於直線的左側;如果返回值為0,則列印該點位於直線上。
  • 定義一個靜態內部類pnt,其中包含int x和int y。
  • 實現dirOfPoint方法來計算叉積並返回方向。

示例

以下是檢查點是否在直線的左側或右側的示例−

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.

使用使用者輸入值

在這種方法中,我們透過使用者輸入方法宣告直線的點和點的座標,並將這些值作為引數傳遞給我們的使用者定義方法。然後,透過使用方法內的演算法,我們可以找到點相對於直線的方向。

  • 首先,從java.util包匯入所有必要的類,並將建立Main類並定義main方法。
  • 之後,我們將建立一個Scanner物件用於使用者輸入,並建立三個點物件:first、second和point。
  • 提示使用者輸入直線的第一個點的座標,並在first.x和first.y中儲存它們,然後輸入直線的第二個點的座標,並在second.xsecond.y中儲存它們。
  • 提示使用者輸入要檢查的點的座標,並在point.xpoint.y中儲存它們。
  • 使用三個點物件呼叫dirOfPoint方法來計算方向。
  • 我們將使用if-else條件語句。如果返回值為1,則列印該點位於直線的右側;如果返回值為-1,則列印該點位於直線的左側;如果返回值為0,則列印該點位於直線上。
  • 定義一個靜態內部類pnt,其中包含int xint y
  • 實現dirOfPoint方法來計算叉積並返回方向。

示例

以下是檢查點是否在直線的左側或右側的示例−

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中檢查點是否在直線的左側或右側。

更新於:2024年10月23日

648次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.