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
"""
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,29 @@ 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)
64
- of the spherical caps, then the two volumes and it returns the sum.
67
+ sum of the volumes of the spherical caps.
68
+ First, it calculates the heights :math:`(h_1, h_2)` of the spherical caps,
69
+ then the two volumes and it returns the sum.
65
70
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)
71
+
72
+ .. math::
73
+ h_1 = \frac{(radius_1 - radius_2 + centers\_distance)
74
+ \cdot (radius_1 + radius_2 - centers\_distance)}
75
+ {2 \cdot centers\_distance}
76
+
77
+ h_2 = \frac{(radius_2 - radius_1 + centers\_distance)
78
+ \cdot (radius_2 + radius_1 - centers\_distance)}
79
+ {2 \cdot centers\_distance}
80
+
81
+ if `centers_distance` is 0 then it returns the volume of the smallers sphere
82
+
83
+ :return: ``vol_spherical_cap`` (:math:`h_1`, :math:`radius_2`)
84
+ + ``vol_spherical_cap`` (:math:`h_2`, :math:`radius_1`)
85
+
74
86
>>> vol_spheres_intersect(2, 2, 1)
75
87
21.205750411731103
76
88
>>> vol_spheres_intersect(2.6, 2.6, 1.6)
@@ -112,14 +124,18 @@ def vol_spheres_intersect(
112
124
def vol_spheres_union (
113
125
radius_1 : float , radius_2 : float , centers_distance : float
114
126
) -> float :
115
- """
127
+ r """
116
128
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)
129
+
130
+ It is the sum of sphere :math:`A` and sphere :math:`B` minus their intersection.
131
+ First, it calculates the volumes :math:`(v_1, v_2)` of the spheres,
132
+ then the volume of the intersection :math:`i` and
133
+ it returns the sum :math:`v_1 + v_2 - i`.
134
+ If `centers_distance` is 0 then it returns the volume of the larger sphere
135
+
136
+ :return: ``vol_sphere`` (:math:`radius_1`) + ``vol_sphere`` (:math:`radius_2`)
137
+ - ``vol_spheres_intersect``
138
+ (:math:`radius_1`, :math:`radius_2`, :math:`centers\_distance`)
123
139
124
140
>>> vol_spheres_union(2, 2, 1)
125
141
45.814892864851146
@@ -157,7 +173,9 @@ def vol_spheres_union(
157
173
def vol_cuboid (width : float , height : float , length : float ) -> float :
158
174
"""
159
175
Calculate the Volume of a Cuboid.
160
- :return multiple of width, length and height
176
+
177
+ :return: multiple of `width`, `length` and `height`
178
+
161
179
>>> vol_cuboid(1, 1, 1)
162
180
1.0
163
181
>>> vol_cuboid(1, 2, 3)
@@ -185,10 +203,12 @@ def vol_cuboid(width: float, height: float, length: float) -> float:
185
203
186
204
187
205
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
206
+ r"""
207
+ | Calculate the Volume of a Cone.
208
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Cone
209
+
210
+ :return: :math:`\frac{1}{3} \cdot area\_of\_base \cdot height`
211
+
192
212
>>> vol_cone(10, 3)
193
213
10.0
194
214
>>> vol_cone(1, 1)
@@ -212,10 +232,12 @@ def vol_cone(area_of_base: float, height: float) -> float:
212
232
213
233
214
234
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
235
+ r"""
236
+ | Calculate the Volume of a Right Circular Cone.
237
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Cone
238
+
239
+ :return: :math:`\frac{1}{3} \cdot \pi \cdot radius^2 \cdot height`
240
+
219
241
>>> vol_right_circ_cone(2, 3)
220
242
12.566370614359172
221
243
>>> vol_right_circ_cone(0, 0)
@@ -237,10 +259,12 @@ def vol_right_circ_cone(radius: float, height: float) -> float:
237
259
238
260
239
261
def vol_prism (area_of_base : float , height : float ) -> float :
240
- """
241
- Calculate the Volume of a Prism.
242
- Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
243
- :return V = Bh
262
+ r"""
263
+ | Calculate the Volume of a Prism.
264
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
265
+
266
+ :return: :math:`V = B \cdot h`
267
+
244
268
>>> vol_prism(10, 2)
245
269
20.0
246
270
>>> vol_prism(11, 1)
@@ -264,10 +288,12 @@ def vol_prism(area_of_base: float, height: float) -> float:
264
288
265
289
266
290
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
291
+ r"""
292
+ | Calculate the Volume of a Pyramid.
293
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)
294
+
295
+ :return: :math:`\frac{1}{3} \cdot B \cdot h`
296
+
271
297
>>> vol_pyramid(10, 3)
272
298
10.0
273
299
>>> vol_pyramid(1.5, 3)
@@ -291,10 +317,12 @@ def vol_pyramid(area_of_base: float, height: float) -> float:
291
317
292
318
293
319
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
320
+ r"""
321
+ | Calculate the Volume of a Sphere.
322
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
323
+
324
+ :return: :math:`\frac{4}{3} \cdot \pi \cdot r^3`
325
+
298
326
>>> vol_sphere(5)
299
327
523.5987755982989
300
328
>>> vol_sphere(1)
@@ -315,10 +343,13 @@ def vol_sphere(radius: float) -> float:
315
343
316
344
317
345
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
346
+ r"""
347
+ | Calculate the volume of a hemisphere
348
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere
349
+ | Other references: https://www.cuemath.com/geometry/hemisphere
350
+
351
+ :return: :math:`\frac{2}{3} \cdot \pi \cdot radius^3`
352
+
322
353
>>> vol_hemisphere(1)
323
354
2.0943951023931953
324
355
>>> vol_hemisphere(7)
@@ -339,9 +370,12 @@ def vol_hemisphere(radius: float) -> float:
339
370
340
371
341
372
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
373
+ r"""
374
+ | Calculate the Volume of a Circular Cylinder.
375
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
376
+
377
+ :return: :math:`\pi \cdot radius^2 \cdot height`
378
+
345
379
>>> vol_circular_cylinder(1, 1)
346
380
3.141592653589793
347
381
>>> vol_circular_cylinder(4, 3)
@@ -368,7 +402,9 @@ def vol_circular_cylinder(radius: float, height: float) -> float:
368
402
def vol_hollow_circular_cylinder (
369
403
inner_radius : float , outer_radius : float , height : float
370
404
) -> float :
371
- """Calculate the Volume of a Hollow Circular Cylinder.
405
+ """
406
+ Calculate the Volume of a Hollow Circular Cylinder.
407
+
372
408
>>> vol_hollow_circular_cylinder(1, 2, 3)
373
409
28.274333882308138
374
410
>>> vol_hollow_circular_cylinder(1.6, 2.6, 3.6)
@@ -405,8 +441,9 @@ def vol_hollow_circular_cylinder(
405
441
406
442
407
443
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
444
+ """
445
+ | Calculate the Volume of a Conical Frustum.
446
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
410
447
411
448
>>> vol_conical_frustum(45, 7, 28)
412
449
48490.482608158454
@@ -443,9 +480,12 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa
443
480
444
481
445
482
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
483
+ r"""
484
+ | Calculate the Volume of a Torus.
485
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Torus
486
+
487
+ :return: :math:`2 \pi^2 \cdot torus\_radius \cdot tube\_radius^2`
488
+
449
489
>>> vol_torus(1, 1)
450
490
19.739208802178716
451
491
>>> vol_torus(4, 3)
@@ -471,8 +511,9 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
471
511
472
512
473
513
def vol_icosahedron (tri_side : float ) -> float :
474
- """Calculate the Volume of an Icosahedron.
475
- Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
514
+ """
515
+ | Calculate the Volume of an Icosahedron.
516
+ | Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
476
517
477
518
>>> from math import isclose
478
519
>>> isclose(vol_icosahedron(2.5), 34.088984228514256)
0 commit comments