11"""
22Find the volume of various shapes.
3+
34* https://en.wikipedia.org/wiki/Volume
45* https://en.wikipedia.org/wiki/Spherical_cap
56"""
67
78from __future__ import annotations
89
9- from math import pi , pow # noqa: A004
10+ from math import pi , pow
1011
1112
1213def vol_cube (side_length : float ) -> float :
1314 """
1415 Calculate the Volume of a Cube.
16+
1517 >>> vol_cube(1)
1618 1.0
1719 >>> vol_cube(3)
@@ -33,6 +35,7 @@ def vol_cube(side_length: float) -> float:
3335def vol_spherical_cap (height : float , radius : float ) -> float :
3436 """
3537 Calculate the volume of the spherical cap.
38+
3639 >>> vol_spherical_cap(1, 2)
3740 5.235987755982988
3841 >>> vol_spherical_cap(1.6, 2.6)
@@ -57,20 +60,27 @@ def vol_spherical_cap(height: float, radius: float) -> float:
5760def vol_spheres_intersect (
5861 radius_1 : float , radius_2 : float , centers_distance : float
5962) -> float :
60- """
63+ r """
6164 Calculate the volume of the intersection of two spheres.
65+
6266 The intersection is composed by two spherical caps and therefore its volume is the
63- sum of the volumes of the spherical caps. First, it calculates the heights (h1, h2)
67+ sum of the volumes of the spherical caps. First, it calculates the heights :math:`(h_1, h_2)`
6468 of the spherical caps, then the two volumes and it returns the sum.
6569 The height formulas are
66- h1 = (radius_1 - radius_2 + centers_distance)
67- * (radius_1 + radius_2 - centers_distance)
68- / (2 * centers_distance)
69- h2 = (radius_2 - radius_1 + centers_distance)
70- * (radius_2 + radius_1 - centers_distance)
71- / (2 * centers_distance)
72- if centers_distance is 0 then it returns the volume of the smallers sphere
73- :return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1)
70+
71+ .. math::
72+ h_1 = \frac{(radius_1 - radius_2 + centers\_distance)
73+ \cdot (radius_1 + radius_2 - centers\_distance)}
74+ {2 \cdot centers\_distance}
75+
76+ h_2 = \frac{(radius_2 - radius_1 + centers\_distance)
77+ \cdot (radius_2 + radius_1 - centers\_distance)}
78+ {2 \cdot centers\_distance}
79+
80+ if `centers_distance` is 0 then it returns the volume of the smallers sphere
81+
82+ :return: ``vol_spherical_cap`` (:math:`h_1`, :math:`radius_2`) + ``vol_spherical_cap`` (:math:`h_2`, :math:`radius_1`)
83+
7484 >>> vol_spheres_intersect(2, 2, 1)
7585 21.205750411731103
7686 >>> vol_spheres_intersect(2.6, 2.6, 1.6)
@@ -114,12 +124,14 @@ def vol_spheres_union(
114124) -> float :
115125 """
116126 Calculate the volume of the union of two spheres that possibly intersect.
117- It is the sum of sphere A and sphere B minus their intersection.
118- First, it calculates the volumes (v1, v2) of the spheres,
119- then the volume of the intersection (i) and it returns the sum v1+v2-i.
120- If centers_distance is 0 then it returns the volume of the larger sphere
121- :return vol_sphere(radius_1) + vol_sphere(radius_2)
122- - vol_spheres_intersect(radius_1, radius_2, centers_distance)
127+
128+ It is the sum of sphere :math:`A` and sphere :math:`B` minus their intersection.
129+ First, it calculates the volumes :math:`(v_1, v_2)` of the spheres,
130+ then the volume of the intersection :math:`i` and it returns the sum :math:`v_1 + v_2 - i`.
131+ If `centers_distance` is 0 then it returns the volume of the larger sphere
132+
133+ :return: ``vol_sphere`` (:math:`radius_1`) + ``vol_sphere`` (:math:`radius_2`)
134+ - ``vol_spheres_intersect`` (:math:`radius_1`, :math:`radius_2`, :math:`centers\_distance`)
123135
124136 >>> vol_spheres_union(2, 2, 1)
125137 45.814892864851146
@@ -157,7 +169,9 @@ def vol_spheres_union(
157169def vol_cuboid (width : float , height : float , length : float ) -> float :
158170 """
159171 Calculate the Volume of a Cuboid.
160- :return multiple of width, length and height
172+
173+ :return: multiple of `width`, `length` and `height`
174+
161175 >>> vol_cuboid(1, 1, 1)
162176 1.0
163177 >>> vol_cuboid(1, 2, 3)
@@ -185,10 +199,12 @@ def vol_cuboid(width: float, height: float, length: float) -> float:
185199
186200
187201def vol_cone (area_of_base : float , height : float ) -> float :
188- """
189- Calculate the Volume of a Cone.
190- Wikipedia reference: https://en.wikipedia.org/wiki/Cone
191- :return (1/3) * area_of_base * height
202+ r"""
203+ | Calculate the Volume of a Cone.
204+ | Wikipedia reference: https://en.wikipedia.org/wiki/Cone
205+
206+ :return: :math:`\frac{1}{3} \cdot area\_of\_base \cdot height`
207+
192208 >>> vol_cone(10, 3)
193209 10.0
194210 >>> vol_cone(1, 1)
@@ -212,10 +228,12 @@ def vol_cone(area_of_base: float, height: float) -> float:
212228
213229
214230def vol_right_circ_cone (radius : float , height : float ) -> float :
215- """
216- Calculate the Volume of a Right Circular Cone.
217- Wikipedia reference: https://en.wikipedia.org/wiki/Cone
218- :return (1/3) * pi * radius^2 * height
231+ r"""
232+ | Calculate the Volume of a Right Circular Cone.
233+ | Wikipedia reference: https://en.wikipedia.org/wiki/Cone
234+
235+ :return: :math:`\frac{1}{3} \cdot \pi \cdot radius^2 \cdot height`
236+
219237 >>> vol_right_circ_cone(2, 3)
220238 12.566370614359172
221239 >>> vol_right_circ_cone(0, 0)
@@ -238,9 +256,11 @@ def vol_right_circ_cone(radius: float, height: float) -> float:
238256
239257def vol_prism (area_of_base : float , height : float ) -> float :
240258 """
241- Calculate the Volume of a Prism.
242- Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
243- :return V = Bh
259+ | Calculate the Volume of a Prism.
260+ | Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
261+
262+ :return: :math:`V = B \cdot h`
263+
244264 >>> vol_prism(10, 2)
245265 20.0
246266 >>> vol_prism(11, 1)
@@ -264,10 +284,12 @@ def vol_prism(area_of_base: float, height: float) -> float:
264284
265285
266286def vol_pyramid (area_of_base : float , height : float ) -> float :
267- """
268- Calculate the Volume of a Pyramid.
269- Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)
270- :return (1/3) * Bh
287+ r"""
288+ | Calculate the Volume of a Pyramid.
289+ | Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)
290+
291+ :return: :math:`\frac{1}{3} \cdot B \cdot h`
292+
271293 >>> vol_pyramid(10, 3)
272294 10.0
273295 >>> vol_pyramid(1.5, 3)
@@ -291,10 +313,12 @@ def vol_pyramid(area_of_base: float, height: float) -> float:
291313
292314
293315def vol_sphere (radius : float ) -> float :
294- """
295- Calculate the Volume of a Sphere.
296- Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
297- :return (4/3) * pi * r^3
316+ r"""
317+ | Calculate the Volume of a Sphere.
318+ | Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
319+
320+ :return: :math:`\frac{4}{3} \cdot \pi \cdot r^3`
321+
298322 >>> vol_sphere(5)
299323 523.5987755982989
300324 >>> vol_sphere(1)
@@ -315,10 +339,13 @@ def vol_sphere(radius: float) -> float:
315339
316340
317341def vol_hemisphere (radius : float ) -> float :
318- """Calculate the volume of a hemisphere
319- Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere
320- Other references: https://www.cuemath.com/geometry/hemisphere
321- :return 2/3 * pi * radius^3
342+ r"""
343+ | Calculate the volume of a hemisphere
344+ | Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere
345+ | Other references: https://www.cuemath.com/geometry/hemisphere
346+
347+ :return: :math:`\frac{2}{3} \cdot \pi \cdot radius^3`
348+
322349 >>> vol_hemisphere(1)
323350 2.0943951023931953
324351 >>> vol_hemisphere(7)
@@ -339,9 +366,12 @@ def vol_hemisphere(radius: float) -> float:
339366
340367
341368def vol_circular_cylinder (radius : float , height : float ) -> float :
342- """Calculate the Volume of a Circular Cylinder.
343- Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
344- :return pi * radius^2 * height
369+ """
370+ | Calculate the Volume of a Circular Cylinder.
371+ | Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
372+
373+ :return: :math:`\pi \cdot radius^2 \cdot height`
374+
345375 >>> vol_circular_cylinder(1, 1)
346376 3.141592653589793
347377 >>> vol_circular_cylinder(4, 3)
@@ -368,7 +398,9 @@ def vol_circular_cylinder(radius: float, height: float) -> float:
368398def vol_hollow_circular_cylinder (
369399 inner_radius : float , outer_radius : float , height : float
370400) -> float :
371- """Calculate the Volume of a Hollow Circular Cylinder.
401+ """
402+ Calculate the Volume of a Hollow Circular Cylinder.
403+
372404 >>> vol_hollow_circular_cylinder(1, 2, 3)
373405 28.274333882308138
374406 >>> vol_hollow_circular_cylinder(1.6, 2.6, 3.6)
@@ -405,8 +437,9 @@ def vol_hollow_circular_cylinder(
405437
406438
407439def vol_conical_frustum (height : float , radius_1 : float , radius_2 : float ) -> float :
408- """Calculate the Volume of a Conical Frustum.
409- Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
440+ """
441+ | Calculate the Volume of a Conical Frustum.
442+ | Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
410443
411444 >>> vol_conical_frustum(45, 7, 28)
412445 48490.482608158454
@@ -443,9 +476,12 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa
443476
444477
445478def vol_torus (torus_radius : float , tube_radius : float ) -> float :
446- """Calculate the Volume of a Torus.
447- Wikipedia reference: https://en.wikipedia.org/wiki/Torus
448- :return 2pi^2 * torus_radius * tube_radius^2
479+ """
480+ | Calculate the Volume of a Torus.
481+ | Wikipedia reference: https://en.wikipedia.org/wiki/Torus
482+
483+ :return: :math:`2 \pi^2 \cdot torus\_radius \cdot tube\_radius^2`
484+
449485 >>> vol_torus(1, 1)
450486 19.739208802178716
451487 >>> vol_torus(4, 3)
@@ -471,8 +507,9 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
471507
472508
473509def vol_icosahedron (tri_side : float ) -> float :
474- """Calculate the Volume of an Icosahedron.
475- Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
510+ """
511+ | Calculate the Volume of an Icosahedron.
512+ | Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
476513
477514 >>> from math import isclose
478515 >>> isclose(vol_icosahedron(2.5), 34.088984228514256)
0 commit comments