Skip to content

Added spheres union #6879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6ea3b00
Spheres union
matteomessmer Oct 9, 2022
2317ca2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2022
4585796
Update volume.py
matteomessmer Oct 9, 2022
f2d1867
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2022
73337b0
Update volume.py
matteomessmer Oct 9, 2022
fd2f2a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2022
6dccffe
Merge branch 'master' into master
matteomessmer Oct 14, 2022
af41981
Merge branch 'TheAlgorithms:master' into master
matteomessmer Oct 16, 2022
8b2ff50
f-strings
matteomessmer Oct 21, 2022
794b98c
Update maths/volume.py
matteomessmer Oct 27, 2022
bc48147
Merge branch 'TheAlgorithms:master' into master
matteomessmer Oct 27, 2022
4d9c415
more tests
matteomessmer Oct 27, 2022
4cda451
Merge branch 'TheAlgorithms:master' into master
matteomessmer Oct 27, 2022
ae9d9e2
fix non negative
matteomessmer Oct 27, 2022
b7510d0
fix 0 radius
matteomessmer Oct 27, 2022
940fc1c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2022
0cdf596
fix tests
matteomessmer Oct 27, 2022
f295743
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2022
d0d2d2a
fix tests
matteomessmer Oct 27, 2022
39536fd
fix print
matteomessmer Oct 27, 2022
6b8b27b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2022
15c2634
fix comment
matteomessmer Oct 27, 2022
2e1ca03
fix comment
matteomessmer Oct 27, 2022
55a8084
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2022
3893e2f
Update volume.py
matteomessmer Oct 27, 2022
a098e3a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions maths/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,51 @@ def vol_spheres_intersect(
return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1)


def vol_spheres_union(
radius_1: float, radius_2: float, centers_distance: float
) -> float:
"""
Calculate the volume of the union of two spheres that possibly intersect.
It is the sum of sphere A and sphere B minus their intersection.
First, it calculates the volumes (v1, v2) of the spheres,
then the volume of the intersection (i) and it returns the sum v1+v2-i.
If centers_distance is 0 then it returns the volume of the larger sphere
:return vol_sphere(radius_1) + vol_sphere(radius_2)
- vol_spheres_intersect(radius_1, radius_2, centers_distance)

>>> vol_spheres_union(2, 2, 1)
45.814892864851146
>>> vol_spheres_union(1.56, 2.2, 1.4)
48.77802773671288
>>> vol_spheres_union(0, 2, 1)
Traceback (most recent call last):
...
ValueError: vol_spheres_union() only accepts non-negative values, non-zero radius
>>> vol_spheres_union('1.56', '2.2', '1.4')
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'str' and 'int'
>>> vol_spheres_union(1, None, 1)
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'NoneType' and 'int'
"""

if radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0:
raise ValueError(
"vol_spheres_union() only accepts non-negative values, non-zero radius"
)

if centers_distance == 0:
return vol_sphere(max(radius_1, radius_2))

return (
vol_sphere(radius_1)
+ vol_sphere(radius_2)
- vol_spheres_intersect(radius_1, radius_2, centers_distance)
)


def vol_cuboid(width: float, height: float, length: float) -> float:
"""
Calculate the Volume of a Cuboid.
Expand Down Expand Up @@ -408,12 +453,13 @@ 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"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
) # ~= 28.3
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
print(f"Spheres union: {vol_spheres_union(2, 2, 1) = }") # ~= 45.81
print(
f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
) # ~= 28.3


if __name__ == "__main__":
Expand Down