Added volume of a torus (#7294)

* Added volume of a torus

Added function calculation the volume of a torus according to the formula:
V = 2 * pi^2 * R * r^2
where R is the major radius and r is the minor radius of the torus.

* Added test for torus volume
This commit is contained in:
kvadrik
2026-02-28 21:08:50 +02:00
committed by GitHub
parent ba286b24d6
commit 0d2a98e9f8
2 changed files with 14 additions and 0 deletions

View File

@@ -114,4 +114,15 @@ public final class Volume {
public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBaseArea, double height) {
return (upperBaseArea + lowerBaseArea + Math.sqrt(upperBaseArea * lowerBaseArea)) * height / 3;
}
/**
* Calculate the volume of a torus.
*
* @param majorRadius major radius of a torus
* @param minorRadius minor radius of a torus
* @return volume of the torus
*/
public static double volumeTorus(double majorRadius, double minorRadius) {
return 2 * Math.PI * Math.PI * majorRadius * minorRadius * minorRadius;
}
}

View File

@@ -38,5 +38,8 @@ public class VolumeTest {
/* test pyramid frustum */
assertEquals(140.0, Volume.volumeFrustumOfPyramid(6, 24, 10));
/* test torus */
assertEquals(39.47841760435743, Volume.volumeTorus(2, 1));
}
}