r""" 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 :math:`(h_1, h_2)` of the spherical caps, then the two volu
(
radius_1: float, radius_2: float, centers_distance: float
)
| 58 | |
| 59 | |
| 60 | def vol_spheres_intersect( |
| 61 | radius_1: float, radius_2: float, centers_distance: float |
| 62 | ) -> float: |
| 63 | r""" |
| 64 | Calculate the volume of the intersection of two spheres. |
| 65 | |
| 66 | The intersection is composed by two spherical caps and therefore its volume is the |
| 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. |
| 70 | The height formulas are |
| 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 | |
| 86 | >>> vol_spheres_intersect(2, 2, 1) |
| 87 | 21.205750411731103 |
| 88 | >>> vol_spheres_intersect(2.6, 2.6, 1.6) |
| 89 | 40.71504079052372 |
| 90 | >>> vol_spheres_intersect(0, 0, 0) |
| 91 | 0.0 |
| 92 | >>> vol_spheres_intersect(-2, 2, 1) |
| 93 | Traceback (most recent call last): |
| 94 | ... |
| 95 | ValueError: vol_spheres_intersect() only accepts non-negative values |
| 96 | >>> vol_spheres_intersect(2, -2, 1) |
| 97 | Traceback (most recent call last): |
| 98 | ... |
| 99 | ValueError: vol_spheres_intersect() only accepts non-negative values |
| 100 | >>> vol_spheres_intersect(2, 2, -1) |
| 101 | Traceback (most recent call last): |
| 102 | ... |
| 103 | ValueError: vol_spheres_intersect() only accepts non-negative values |
| 104 | """ |
| 105 | if radius_1 < 0 or radius_2 < 0 or centers_distance < 0: |
| 106 | raise ValueError("vol_spheres_intersect() only accepts non-negative values") |
| 107 | if centers_distance == 0: |
| 108 | return vol_sphere(min(radius_1, radius_2)) |
| 109 | |
| 110 | h1 = ( |
| 111 | (radius_1 - radius_2 + centers_distance) |
| 112 | * (radius_1 + radius_2 - centers_distance) |
| 113 | / (2 * centers_distance) |
| 114 | ) |
| 115 | h2 = ( |
| 116 | (radius_2 - radius_1 + centers_distance) |
| 117 | * (radius_2 + radius_1 - centers_distance) |
no test coverage detected