如何在 Swift 中計算四面體的面積?
四面體是一種三維三角錐體,其底面也是一個三角形。通常,四面體包含四個等邊三角形,因此其內角為 60 度。
在 Swift 中,我們可以使用以下公式計算四面體的面積
公式
$$\mathrm{Area=\sqrt{3}*X*X}$$
這裡,x 表示四面體的邊長。
如果您想找到四面體一個面的面積,則可以使用以下公式
公式
$$\mathrm{Area\:of\:one\:side\:of\:tetrahedron =(\sqrt{3}*y*y)/4}$$
這裡,y 表示四面體的邊長。
演算法
步驟 1 − 建立一個函式,該函式以四面體的邊長作為引數並返回面積。
步驟 2 − 在函式內部,我們將使用數學公式來查詢四面體的面積並將結果儲存在一個變數中。
步驟 3 − 返回四面體的面積。
步驟 4 − 定義四面體的邊長,或者可以從使用者那裡獲取。
步驟 5 − 呼叫上面建立的函式並將邊長傳遞給它。
步驟 6 − 顯示結果。
示例 1:四面體的面積
在下面的 Swift 程式中,我們將計算四面體的面積。為此,我們建立了一個名為 areaOfTetrahedron() 的函式。此函式以邊長作為引數,並使用公式查詢面積,其中我們將使用 sqrt() 函式查詢 3 的平方根並返回四面體的最終面積。
import Foundation import Glibc // Function to find the area of Tetrahedron func areaOfTetrahedron(inputSide: Double)-> Double{ let resultantArea = sqrt(3) * inputSide * inputSide return resultantArea } // Test case let side = 4.0 // Calling the function let resultant = areaOfTetrahedron(inputSide:side) // Displaying the result print("Sides:", side) print("Area of Tetrahedron:", resultant)
輸出
Sides: 4.0 Area of Tetrahedron: 27.712812921102035
示例 2:四面體一個面的面積
在下面的 Swift 程式中,我們將計算四面體一個面的面積。為此,我們建立了一個名為 areaOfOneFace() 的函式。此函式以邊長作為引數,並使用給定的公式查詢四面體一個面的面積。返回四面體一個面的最終面積。
import Foundation import Glibc // Function to find the area of one face of the Tetrahedron func areaOfOneFace(inputSide: Double)-> Double{ let resultantArea = (sqrt(3) * inputSide * inputSide) / 4 return resultantArea } // Test case let side = 2.0 // Calling the function let resultant = areaOfOneFace(inputSide:side) // Displaying the result print("Sides:", side) print("Area of one side of Tetrahedron:", resultant)
輸出
Sides: 2.0 Area of one side of Tetrahedron: 1.7320508075688772
現實生活中的應用
四面體面積的現實生活中的應用
它用於維護計算機幾何和計算機圖形學中四面體網格的網格質量。
它有時用於晶體結構。
它通常用於教育部門來說明幾何概念。
結論
這就是我們如何計算四面體的面積。四面體有四個面,因此它通常用於藝術、建築等領域。加沙的金字塔、鹽晶體、帳篷和分子都是四面體的現實生活中的例子。