使用行列式計算三角形面積的 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 類進行數學運算以及如何使用方法進行程式碼組織和模組化。

程式的時間複雜度為常數時間,這意味著它執行固定數量的操作,而與輸入的大小無關。這使其成為計算三角形面積的快速高效的程式。程式的空間複雜度也是常數,因為它僅使用固定數量的記憶體來儲存其變數,並且不需要任何額外的記憶體分配。

更新於: 2023年4月10日

220 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.