Skip to content

Commit 7390777

Browse files
authored
Added 2 shaped in volume.py (TheAlgorithms#5560)
1 parent 729aaf6 commit 7390777

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

maths/volume.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ def vol_sphere(radius: float) -> float:
153153
return 4 / 3 * pi * pow(radius, 3)
154154

155155

156+
def vol_hemisphere(radius: float):
157+
"""Calculate the volume of a hemisphere
158+
Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere
159+
Other references: https://www.cuemath.com/geometry/hemisphere
160+
:return 2/3 * pi * radius^3
161+
162+
>>> vol_hemisphere(1)
163+
2.0943951023931953
164+
165+
>>> vol_hemisphere(7)
166+
718.3775201208659
167+
"""
168+
return 2 / 3 * pi * pow(radius, 3)
169+
170+
156171
def vol_circular_cylinder(radius: float, height: float) -> float:
157172
"""Calculate the Volume of a Circular Cylinder.
158173
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
@@ -166,6 +181,26 @@ def vol_circular_cylinder(radius: float, height: float) -> float:
166181
return pi * pow(radius, 2) * height
167182

168183

184+
def vol_conical_frustum(height: float, radius_1: float, radius_2: float):
185+
"""Calculate the Volume of a Conical Frustum.
186+
Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
187+
:return 1/3 * pi * height * (radius_1^2 + radius_top^2 + radius_1 * radius_2)
188+
189+
>>> vol_conical_frustum(45, 7, 28)
190+
48490.482608158454
191+
192+
>>> vol_conical_frustum(1, 1, 2)
193+
7.330382858376184
194+
"""
195+
return (
196+
1
197+
/ 3
198+
* pi
199+
* height
200+
* (pow(radius_1, 2) + pow(radius_2, 2) + radius_1 * radius_2)
201+
)
202+
203+
169204
def main():
170205
"""Print the Results of Various Volume Calculations."""
171206
print("Volumes:")
@@ -176,7 +211,9 @@ def main():
176211
print("Prism: " + str(vol_prism(2, 2))) # = 4
177212
print("Pyramid: " + str(vol_pyramid(2, 2))) # ~= 1.33
178213
print("Sphere: " + str(vol_sphere(2))) # ~= 33.5
214+
print("Hemisphere: " + str(vol_hemisphere(2))) # ~= 16.75
179215
print("Circular Cylinder: " + str(vol_circular_cylinder(2, 2))) # ~= 25.1
216+
print("Conical Frustum: " + str(vol_conical_frustum(2, 2, 4))) # ~= 58.6
180217
print("Spherical cap: " + str(vol_spherical_cap(1, 2))) # ~= 5.24
181218
print("Spheres intersetion: " + str(vol_spheres_intersect(2, 2, 1))) # ~= 21.21
182219

0 commit comments

Comments
 (0)