1
1
"""
2
2
Find the volume of various shapes.
3
+
3
4
* https://en.wikipedia.org/wiki/Volume
4
5
* https://en.wikipedia.org/wiki/Spherical_cap
5
6
"""
6
7
7
8
from __future__ import annotations
8
9
9
- from math import pi , pow # noqa: A004
10
+ from math import pi , pow
10
11
11
12
12
13
def vol_cube (side_length : float ) -> float :
13
14
"""
14
15
Calculate the Volume of a Cube.
16
+
15
17
>>> vol_cube(1)
16
18
1.0
17
19
>>> vol_cube(3)
@@ -33,6 +35,7 @@ def vol_cube(side_length: float) -> float:
33
35
def vol_spherical_cap (height : float , radius : float ) -> float :
34
36
"""
35
37
Calculate the volume of the spherical cap.
38
+
36
39
>>> vol_spherical_cap(1, 2)
37
40
5.235987755982988
38
41
>>> vol_spherical_cap(1.6, 2.6)
@@ -57,20 +60,27 @@ def vol_spherical_cap(height: float, radius: float) -> float:
57
60
def vol_spheres_intersect (
58
61
radius_1 : float , radius_2 : float , centers_distance : float
59
62
) -> float :
60
- """
63
+ r """
61
64
Calculate the volume of the intersection of two spheres.
65
+
62
66
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)`
64
68
of the spherical caps, then the two volumes and it returns the sum.
65
69
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
+
74
84
>>> vol_spheres_intersect(2, 2, 1)
75
85
21.205750411731103
76
86
>>> vol_spheres_intersect(2.6, 2.6, 1.6)
@@ -114,12 +124,14 @@ def vol_spheres_union(
114
124
) -> float :
115
125
"""
116
126
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`)
123
135
124
136
>>> vol_spheres_union(2, 2, 1)
125
137
45.814892864851146
@@ -157,7 +169,9 @@ def vol_spheres_union(
157
169
def vol_cuboid (width : float , height : float , length : float ) -> float :
158
170
"""
159
171
Calculate the Volume of a Cuboid.
160
- :return multiple of width, length and height
172
+
173
+ :return: multiple of `width`, `length` and `height`
174
+
161
175
>>> vol_cuboid(1, 1, 1)
162
176
1.0
163
177
>>> vol_cuboid(1, 2, 3)
@@ -185,10 +199,12 @@ def vol_cuboid(width: float, height: float, length: float) -> float:
185
199
186
200
187
201
def 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
+
192
208
>>> vol_cone(10, 3)
193
209
10.0
194
210
>>> vol_cone(1, 1)
@@ -212,10 +228,12 @@ def vol_cone(area_of_base: float, height: float) -> float:
212
228
213
229
214
230
def 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
+
219
237
>>> vol_right_circ_cone(2, 3)
220
238
12.566370614359172
221
239
>>> vol_right_circ_cone(0, 0)
@@ -238,9 +256,11 @@ def vol_right_circ_cone(radius: float, height: float) -> float:
238
256
239
257
def vol_prism (area_of_base : float , height : float ) -> float :
240
258
"""
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
+
244
264
>>> vol_prism(10, 2)
245
265
20.0
246
266
>>> vol_prism(11, 1)
@@ -264,10 +284,12 @@ def vol_prism(area_of_base: float, height: float) -> float:
264
284
265
285
266
286
def 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
+
271
293
>>> vol_pyramid(10, 3)
272
294
10.0
273
295
>>> vol_pyramid(1.5, 3)
@@ -291,10 +313,12 @@ def vol_pyramid(area_of_base: float, height: float) -> float:
291
313
292
314
293
315
def 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
+
298
322
>>> vol_sphere(5)
299
323
523.5987755982989
300
324
>>> vol_sphere(1)
@@ -315,10 +339,13 @@ def vol_sphere(radius: float) -> float:
315
339
316
340
317
341
def 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
+
322
349
>>> vol_hemisphere(1)
323
350
2.0943951023931953
324
351
>>> vol_hemisphere(7)
@@ -339,9 +366,12 @@ def vol_hemisphere(radius: float) -> float:
339
366
340
367
341
368
def 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
+
345
375
>>> vol_circular_cylinder(1, 1)
346
376
3.141592653589793
347
377
>>> vol_circular_cylinder(4, 3)
@@ -368,7 +398,9 @@ def vol_circular_cylinder(radius: float, height: float) -> float:
368
398
def vol_hollow_circular_cylinder (
369
399
inner_radius : float , outer_radius : float , height : float
370
400
) -> float :
371
- """Calculate the Volume of a Hollow Circular Cylinder.
401
+ """
402
+ Calculate the Volume of a Hollow Circular Cylinder.
403
+
372
404
>>> vol_hollow_circular_cylinder(1, 2, 3)
373
405
28.274333882308138
374
406
>>> vol_hollow_circular_cylinder(1.6, 2.6, 3.6)
@@ -405,8 +437,9 @@ def vol_hollow_circular_cylinder(
405
437
406
438
407
439
def 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
410
443
411
444
>>> vol_conical_frustum(45, 7, 28)
412
445
48490.482608158454
@@ -443,9 +476,12 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa
443
476
444
477
445
478
def 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
+
449
485
>>> vol_torus(1, 1)
450
486
19.739208802178716
451
487
>>> vol_torus(4, 3)
@@ -471,8 +507,9 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
471
507
472
508
473
509
def 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
476
513
477
514
>>> from math import isclose
478
515
>>> isclose(vol_icosahedron(2.5), 34.088984228514256)
0 commit comments