From 817006f2667d1dfb74f75ad74d6a18186efdf401 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 24 Oct 2021 14:34:43 +0200 Subject: [PATCH 1/3] sphere intersection + spherical cap volume formulas --- maths/volume.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/maths/volume.py b/maths/volume.py index 51b2b9fc0334..a2439fe1e42e 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -19,6 +19,37 @@ def vol_cube(side_length: int | float) -> float: """ return pow(side_length, 3) +def vol_spherical_cap(height: float, radius: float) -> float: + """ + Calculate the Volume of the spherical cap. + :return 1/3 pi * height ^ 2 * (3 * radius - height) + + >>> vol_spherical_cap(1, 2) + 5.235987755982988 + """ + return 1/3*pi * pow(height, 2) * (3*radius - height) + +def vol_spheres_intersect(radius_1: float, radius_2: float, centers_distance: float) -> float: + """ + Calculate the Volume of the intersection of two spheres. + The intersection is composed by two spherical caps and therefore its volume is the sum of the volumes of the spherical caps. + First it calculates the heights (h1, h2) of the the spherical caps, then the two volumes and it returns the sum. + The height formulas are + h1 = (radius_1-radius_2+centers_distance)*(radius_1+radius_2-centers_distance)/(2*centers_distance) + h2 = (radius_2-radius_1+centers_distance)*(radius_2+radius_1-centers_distance)/(2*centers_distance) + if centers_distance is 0 it returns the volume of the smallers sphere + :return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1) + + >>> vol_spheres_intersect(2, 2, 1) + 21.205750411731103 + """ + if centers_distance == 0: + return vol_sphere(min(radius_1, radius_2)) + + h1 = (radius_1-radius_2+centers_distance)*(radius_1+radius_2-centers_distance)/(2*centers_distance) + h2 = (radius_2-radius_1+centers_distance)*(radius_2+radius_1-centers_distance)/(2*centers_distance) + + return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1) def vol_cuboid(width: float, height: float, length: float) -> float: """ @@ -127,6 +158,8 @@ def main(): print("Pyramid: " + str(vol_pyramid(2, 2))) # ~= 1.33 print("Sphere: " + str(vol_sphere(2))) # ~= 33.5 print("Circular Cylinder: " + str(vol_circular_cylinder(2, 2))) # ~= 25.1 + print("Spherical cap: " + str(vol_spherical_cap(1,2))) # ~= 5.24 + print("Spheres intersetion: " + str(vol_spheres_intersect(2, 2, 1))) # ~= 21.21 if __name__ == "__main__": From 14225e70bf63991392cef6620863deb4eb2ff1b8 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 26 Oct 2021 20:55:00 +0200 Subject: [PATCH 2/3] reformatted --- maths/volume.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index a2439fe1e42e..dfcc768d0a08 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -19,6 +19,7 @@ def vol_cube(side_length: int | float) -> float: """ return pow(side_length, 3) + def vol_spherical_cap(height: float, radius: float) -> float: """ Calculate the Volume of the spherical cap. @@ -27,14 +28,17 @@ def vol_spherical_cap(height: float, radius: float) -> float: >>> vol_spherical_cap(1, 2) 5.235987755982988 """ - return 1/3*pi * pow(height, 2) * (3*radius - height) + return 1 / 3 * pi * pow(height, 2) * (3 * radius - height) + -def vol_spheres_intersect(radius_1: float, radius_2: float, centers_distance: float) -> float: +def vol_spheres_intersect( + radius_1: float, radius_2: float, centers_distance: float +) -> float: """ Calculate the Volume of the intersection of two spheres. The intersection is composed by two spherical caps and therefore its volume is the sum of the volumes of the spherical caps. First it calculates the heights (h1, h2) of the the spherical caps, then the two volumes and it returns the sum. - The height formulas are + The height formulas are h1 = (radius_1-radius_2+centers_distance)*(radius_1+radius_2-centers_distance)/(2*centers_distance) h2 = (radius_2-radius_1+centers_distance)*(radius_2+radius_1-centers_distance)/(2*centers_distance) if centers_distance is 0 it returns the volume of the smallers sphere @@ -45,12 +49,21 @@ def vol_spheres_intersect(radius_1: float, radius_2: float, centers_distance: fl """ if centers_distance == 0: return vol_sphere(min(radius_1, radius_2)) - - h1 = (radius_1-radius_2+centers_distance)*(radius_1+radius_2-centers_distance)/(2*centers_distance) - h2 = (radius_2-radius_1+centers_distance)*(radius_2+radius_1-centers_distance)/(2*centers_distance) + + h1 = ( + (radius_1 - radius_2 + centers_distance) + * (radius_1 + radius_2 - centers_distance) + / (2 * centers_distance) + ) + h2 = ( + (radius_2 - radius_1 + centers_distance) + * (radius_2 + radius_1 - centers_distance) + / (2 * centers_distance) + ) return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1) + def vol_cuboid(width: float, height: float, length: float) -> float: """ Calculate the Volume of a Cuboid. @@ -158,8 +171,8 @@ def main(): print("Pyramid: " + str(vol_pyramid(2, 2))) # ~= 1.33 print("Sphere: " + str(vol_sphere(2))) # ~= 33.5 print("Circular Cylinder: " + str(vol_circular_cylinder(2, 2))) # ~= 25.1 - print("Spherical cap: " + str(vol_spherical_cap(1,2))) # ~= 5.24 - print("Spheres intersetion: " + str(vol_spheres_intersect(2, 2, 1))) # ~= 21.21 + print("Spherical cap: " + str(vol_spherical_cap(1, 2))) # ~= 5.24 + print("Spheres intersetion: " + str(vol_spheres_intersect(2, 2, 1))) # ~= 21.21 if __name__ == "__main__": From 9a82c49429738f9e627bf139b896bc6c05d71b42 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 27 Oct 2021 00:25:05 +0200 Subject: [PATCH 3/3] Update volume.py --- maths/volume.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index dfcc768d0a08..fd24aa9eef54 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -35,13 +35,19 @@ def vol_spheres_intersect( radius_1: float, radius_2: float, centers_distance: float ) -> float: """ - Calculate the Volume of the intersection of two spheres. - The intersection is composed by two spherical caps and therefore its volume is the sum of the volumes of the spherical caps. - First it calculates the heights (h1, h2) of the the spherical caps, then the two volumes and it returns the sum. + Calculate the volume of the intersection of two spheres. + + The intersection is composed by two spherical caps and therefore its volume is the + sum of the volumes of the spherical caps. First it calculates the heights (h1, h2) + of the the spherical caps, then the two volumes and it returns the sum. The height formulas are - h1 = (radius_1-radius_2+centers_distance)*(radius_1+radius_2-centers_distance)/(2*centers_distance) - h2 = (radius_2-radius_1+centers_distance)*(radius_2+radius_1-centers_distance)/(2*centers_distance) - if centers_distance is 0 it returns the volume of the smallers sphere + h1 = (radius_1 - radius_2 + centers_distance) + * (radius_1 + radius_2 - centers_distance) + / (2 * centers_distance) + h2 = (radius_2 - radius_1 + centers_distance) + * (radius_2 + radius_1 - centers_distance) + / (2 * centers_distance) + if centers_distance is 0 then it returns the volume of the smallers sphere :return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1) >>> vol_spheres_intersect(2, 2, 1)