From 7addbccee72d2b18e6d095ab6675cbcb290412ce Mon Sep 17 00:00:00 2001 From: Paradact <44441385+Paradact@users.noreply.github.com> Date: Mon, 31 Oct 2022 22:51:45 +0100 Subject: [PATCH] Torus volume (#7905) * Added Torus volume algorithm * Updated Torus volume for simplicity (removed ref to vol_sphere()) * Refactoring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- maths/volume.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/maths/volume.py b/maths/volume.py index da4054646..1da4584c8 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -441,6 +441,34 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa ) +def vol_torus(torus_radius: float, tube_radius: float) -> float: + """Calculate the Volume of a Torus. + Wikipedia reference: https://en.wikipedia.org/wiki/Torus + :return 2pi^2 * torus_radius * tube_radius^2 + >>> vol_torus(1, 1) + 19.739208802178716 + >>> vol_torus(4, 3) + 710.6115168784338 + >>> vol_torus(3, 4) + 947.4820225045784 + >>> vol_torus(1.6, 1.6) + 80.85179925372404 + >>> vol_torus(0, 0) + 0.0 + >>> vol_torus(-1, 1) + Traceback (most recent call last): + ... + ValueError: vol_torus() only accepts non-negative values + >>> vol_torus(1, -1) + Traceback (most recent call last): + ... + ValueError: vol_torus() only accepts non-negative values + """ + if torus_radius < 0 or tube_radius < 0: + raise ValueError("vol_torus() only accepts non-negative values") + return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2) + + def main(): """Print the Results of Various Volume Calculations.""" print("Volumes:") @@ -453,6 +481,7 @@ def main(): print(f"Sphere: {vol_sphere(2) = }") # ~= 33.5 print(f"Hemisphere: {vol_hemisphere(2) = }") # ~= 16.75 print(f"Circular Cylinder: {vol_circular_cylinder(2, 2) = }") # ~= 25.1 + print(f"Torus: {vol_torus(2, 2) = }") # ~= 157.9 print(f"Conical Frustum: {vol_conical_frustum(2, 2, 4) = }") # ~= 58.6 print(f"Spherical cap: {vol_spherical_cap(1, 2) = }") # ~= 5.24 print(f"Spheres intersetion: {vol_spheres_intersect(2, 2, 1) = }") # ~= 21.21