How to Calculate the Volume of a Tetrahedron in Swift?


Tetrahedron is a triangular base pyramid. It is a platonic solid with four triangular faces, six straight edges and four vertex corners. Where each vertex is connected with every other vertex and each face is of an equilateral triangle.

In Swift, we can calculate the volume of a tetrahedron using the following formula:

Formula

$$\mathrm{Area=(x*x*x*\sqrt{2})/12}$$

Here, x represents the sides of the tetrahedron.

Algorithm

Step 1 − Create a function which takes the side of the tetrahedron as a parameter and returns the volume.

Step 2 − Inside the function, we use the mathematical formula to find the volume of a tetrahedron and store the result in a variable.

Step 3 − Return the volume of a tetrahedron.

Step 4 − Define the side of the tetrahedron, or can take from the user.

Step 5 − Call the above create function and pass the side into it.

Step 6 − Display the result.

Example:Volume of Tetrahedron using Function

In the following Swift program, we will calculate the volume of a tetrahedron. So for that, we create a function named volumeOfTetrahedron(). This function takes the side as a parameter and finds the volume using the formula, where we will use sqrt() function to find the square root of the 2 and return the final volume of a tetrahedron.

import Foundation
import Glibc

// Function to find the volume of Tetrahedron
func volumeOfTetrahedron(inputSide: Double)-> Double{
    let resultantVolume = (sqrt(2) * inputSide * inputSide * inputSide) / 12
    return resultantVolume
}

// Test case
let side = 4.0

// Calling the function
let resultant = volumeOfTetrahedron(inputSide:side)

// Displaying the result
print("Sides:", side)
print("Volume of Tetrahedron:", resultant)

Output

Sides: 4.0
Volume of Tetrahedron: 7.542472332656508

Real−life Usage

The real−life usage of the volume of Tetrahedron:

  • It is used in computer graphics and CAD software for modelling purposes.

  • It is used to demonstrate geometric calculations to the students.

  • It can also be used in architecture to create different types of shapes.

  • It can also be used in the manufacturing industry for designing containers, boxes, models, etc.

Conclusion

So this is how we can calculate the volume of a tetrahedron. A tetrahedron has four faces so it is generally used in various fields like arts, architecture, etc. Pyramids of Gaza, salt crystals, tents, and molecules are some real−life examples of tetrahedrons.

Updated on: 08-Sep-2023

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements