首頁 > Java > java教程 > 主體

如何在Java中檢查三個點是否共線?

WBOY
發布: 2023-09-05 18:41:05
轉載
1108 人瀏覽過

如何在Java中檢查三個點是否共線?

如果三個點都位於一條直線上,則稱這三個點共線。如果這些點不在同一條直線上,則它們不是共線點。

這表示如果三個點(x1, y1),(x2, y2),(x3, y3)在同一條直線上,則它們是共線的。

其中,x1、y1、x2、y2、x3、y3是x軸和y軸上的點,(x1, y1)、(x2, y2)、(x3, y3)是座標。

數學上,有兩種方法可以確定三個點是否共線。

透過使用點求三角形的面積,如果三角形的面積為零,則三個點共線。

Formula to find area of triangle = 0。5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]
登入後複製

透過找出兩點的斜率相等,可以確定這三個點共線。

Formula to find slope =
Slope of (x1, y1), (x2, y2)
m1 = (y2-y1) / (x2-x1)
Slope of (x2, y2), (x3, y3)
m2 = (y3-y2) / (x3-x2)
登入後複製

在本文中,我們將了解如何使用 Java 程式語言檢查三個點是否共線。

展示一些實例給你看

Instance-1

的翻譯為:

實例-1

#假設給定座標為(1,2), (3,4), (5,6)

所有三個點共線,因為它們位於同一條直線上。

Instance-2

的中文翻譯為:

實例-2

假設給定座標為(1,1), (1,4), (1,6)

所有三個點共線,因為它們位於同一條直線上。

Instance-3

的中文翻譯為:

實例-3

假設給定座標為(1,1), (2,4), (4,6)

所有三個點不共線,因為它們不在同一條直線上。

演算法

  • 第 1 步 - 透過使用者輸入或初始化取得三個點。

  • 步驟2 - 透過使用上述公式中的任何一個,檢查三角形面積是否為零或斜率是否相同,然後列印三個點共線,否則三個點不共線。 < /p>

  • 步驟 3 − 列印結果。

多種方法

我們以不同的方式提供了解決方案。

  • 藉由求三角形面積。

  • 透過找到斜率。

讓我們逐一查看程式及其輸出

方法 1:透過找出三角形面積

在這個方法中,程式將初始化三個點。然後使用公式計算三角形的面積。如果面積為零,則列印三個點共線。

範例

public class Main{
   //main method
   public static void main(String args[]){
	
      //initialized first point
      double x1 = 1;
      double y1 = 2;
      System。out。println("First point: "+x1+", "+y1);
		
      //initialized second point
      double x2 = 3;
      double y2 = 4;
      System。out。println("Second point: "+x2+", "+y2);
		
      //initialized third point
      double x3 = 5;
      double y3 = 6;
      System。out。println("Third point: "+x3+", "+y3);
		
      //find triangle area by using formula
      double triangleArea = 0。5*(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
      System。out。println("Area of triangle using three points ="+triangleArea);
      if (triangleArea == 0)
         System。out。println("Three points are collinear。");
      else
         System。out。println("Three points are not collinear。");
   }
}
登入後複製

輸出

First point: 1。0, 2。0
Second pointe: 3。0, 4。0
Third pointe: 5。0, 6。0
Area of triangle using three points = 0。0
Three points are collinear。
登入後複製

方法二:透過找斜率

In this approach, three points will be initialized in the program。 Then calculate the slope of any pair of points and check if slope is equal with slope of other pair of points by using the slope formula。 If both slopes are equal then print three points are collinear。

範例

public class Main{
   //main method
   public static void main(String args[]){
	
      //initialized first point
      double x1 = 1;
      double y1 = 2;
      System。out。println("First point: "+x1+", "+y1);

      //initialized second point
      double x2 = 3;
      double y2 = 4;
      System。out。println("Second point: "+x2+", "+y2);

      //initialized third point
      double x3 = 5;
      double y3 = 6;
      System。out。println("Third point: "+x3+", "+y3);

      //find slope of (x1, y1) , (x2, y2)
      double m1 = (y2-y1) / (x2-x1);

      //find slope of (x2, y2) , (x3, y3)
      double m2 = (y3-y2) / (x3-x2);
      System。out。println("Slope of first pair= " + m1);
      System。out。println("Slope of second pair= " + m2);
      if (m1 == m2)
         System。out。println("Three points are collinear。");
      else
         System。out。println("Three points are not collinear。");
   }
}
登入後複製

輸出

First point: 1。0, 2。0
Second point: 3。0, 4。0
Third point: 5。0, 6。0
Slope of first pair= 1。0
Slope of second pair= 1。0
Three points are collinear。
登入後複製

In this article, we explored how to check if three points are collinear or not in Java by using different approaches。

以上是如何在Java中檢查三個點是否共線?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!