首頁 > Java > java教程 > 主體

使用行列式計算三角形面積的Java程序

王林
發布: 2023-08-31 10:17:08
轉載
977 人瀏覽過

使用行列式計算三角形面積的Java程序

簡介

使用行列式計算三角形面積的 Java 程序是一個簡潔且有效率的程序,可以根據給定三個頂點的座標來計算三角形的面積。

該程式對於學習或使用幾何的任何人都非常有用,因為它演示瞭如何在 Java 中使用基本算術和代數計算,以及如何使用 Scanner 類別讀取使用者輸入。程式提示使用者輸入三角形三個點的座標,然後將其讀入並用於計算座標矩陣的行列式。使用行列式的絕對值來確保面積始終為正,然後使用公式計算三角形的面積並顯示給使用者。該程式可以輕鬆修改以接受不同格式的輸入或執行附加計算,使其成為幾何計算的多功能工具。

決定因素

行列式是一個數學概念,用來決定矩陣的某些屬性。在線性代數中,行列式是可以根據方陣的元素計算出來的標量值。行列式可用於確定矩陣是否有逆矩陣、線性方程組是否有唯一解以及平行四邊形或平行六面體的面積或體積。

文法

area = |determinant|/2
登入後複製
登入後複製
登入後複製

演算法

  • 導入 Scanner 類別。

  • 定義一個名為 TriangleArea 的公共類別。

  • 在類別中定義一個 main 方法。

  • 建立 Scanner 物件來讀取使用者輸入。

  • 提示使用者輸入以空格分隔的三個點的座標。

  • 讀取使用者輸入的座標並將其儲存在六個雙精確度變數(x1、y1、x2、y2、x3、y3)中。

  • 使用公式計算座標矩陣的行列式 -

  • | x1 y1 1 |
    | x2 y2 1 | = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1
    | x3 y3 1 |
    
    登入後複製
    登入後複製
  • 然後我們使用公式計算三角形的面積 -

area = |determinant|/2
登入後複製
登入後複製
登入後複製

範例 1

方法

  • 首先,我們提示使用者輸入三角形三個點的座標。

  • 我們使用 Scanner 類別讀取使用者輸入的座標並將其儲存在六個雙精度變數(x1、y1、x2、y2、x3、y3)中。

  • 接下來,我們使用公式計算座標矩陣的行列式 -

#
| x1 y1 1 |
| x2 y2 1 | = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1
| x3 y3 1 |
登入後複製
登入後複製
  • 然後我們使用公式計算三角形的面積 -

area = |determinant|/2
登入後複製
登入後複製
登入後複製

這是一個使用行列式計算三角形面積的 Java 程式 -

import java.util.Scanner;

public class TriangleArea {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);

      // Prompt the user to enter the coordinates of three points
      System.out.println("Enter the coordinates of three points separated by a space:");
      double x1 = scanner.nextDouble();
      double y1 = scanner.nextDouble();
      double x2 = scanner.nextDouble();
      double y2 = scanner.nextDouble();
      double x3 = scanner.nextDouble();
      double y3 = scanner.nextDouble();

      // Compute the area of the triangle using determinants
      double determinant = x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1;
      double area = Math.abs(determinant / 2);

      // Display the area of the triangle
      System.out.println("The area of the triangle is " + area);
   }
}
登入後複製

說明

請注意,Math.abs() 函數用於確保面積始終為正,因為如果頂點以逆時針順序列出,則行列式可能為負。

輸出

Enter the coordinates of three points separated by a space:
4 3
2 6
7 4
The area of the triangle is 5.5
登入後複製

範例 2

此方法適用於任何三角形,無論其方向或大小為何。程式假設使用者輸入了有效的三個點的數字座標,否則,如果輸入無效,可能會拋出異常。

這是一個使用行列式計算三角形面積的 Java 程式 -

import java.util.Scanner;

public class TriangleArea {

   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the coordinates of the first point: ");
      double x1 = sc.nextDouble();
      double y1 = sc.nextDouble();

      System.out.print("Enter the coordinates of the second point: ");
      double x2 = sc.nextDouble();
      double y2 = sc.nextDouble();

      System.out.print("Enter the coordinates of the third point: ");
      double x3 = sc.nextDouble();
      double y3 = sc.nextDouble();

      double area = calculateTriangleArea(x1, y1, x2, y2, x3, y3);

      System.out.println("The area of the triangle is " + area);
   }

   public static double calculateTriangleArea(double x1, double y1, double x2, double y2, double x3, double y3) {
      double determinant = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
      return Math.abs(determinant) / 2.0;
   }
}
登入後複製

說明

該程式提示使用者輸入形成三角形的三個點的座標,然後使用calculateTriangleArea()方法透過行列式計算三角形的面積。最後,它將計算出的區域列印到控制台。

輸出

Enter the coordinates of the first point: 0 0
Enter the coordinates of the second point: 4 0
Enter the coordinates of the third point: 0 3
The area of the triangle is 6.0
登入後複製

結論

使用行列式計算三角形面積的 Java 程式是在給定座標的情況下計算三角形面積的簡單而有效的方法。程式使用基本算術和代數計算來確定座標矩陣的行列式,然後使用該行列式透過簡單的公式計算三角形的面積。此程式示範如何使用 Scanner 類別進行使用者輸入、使用 Math 類別進行數學運算,以及如何使用程式碼組織和模組化方法。

程式的時間複雜度是常數時間,這意味著無論輸入的大小如何,它都會執行固定數量的操作。這使得它成為計算三角形面積的快速有效的程式。程式的空間複雜度也是恆定的,因為它只使用固定數量的記憶體來儲存變量,不需要任何額外的記憶體分配。

以上是使用行列式計算三角形面積的Java程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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