Skip to content

Volume of Torus #1220

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

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
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
21 changes: 20 additions & 1 deletion Maths/Volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Volume for Triangular Prism
Volume for Pentagonal Prism
Volume for Sphere
Volume for Hemisphere
Volume for Torus
*/

/*
Expand Down Expand Up @@ -112,6 +113,24 @@ const volHemisphere = (radius) => {
return (2.0 * Math.PI * radius ** 3) / 3.0
}

/**
* Calculate the volume for a Torus
* Reference: https://en.wikipedia.org/wiki/Torus
* @param minorRadius - for the radius of torus ring
* @param majorRadius - for the distance between torus center and ring center
* @return {number} - volume of torus = (PI * minorRadius^2) * (2 * PI * majorRadius)
* >>> volTorus(2, 3);
* 236.8705056261446
* */
const volTorus = (minorRadius, majorRadius) => {
isNumber(minorRadius, 'Minor Radius')
isNumber(majorRadius, 'Major Radius')
if (minorRadius >= majorRadius) {
throw new Error('Major Radius must be greater than Minor Radius')
}
return (Math.PI ** 2 * 2 * majorRadius * minorRadius ** 2)
}

const isNumber = (number, noName = 'number') => {
if (typeof number !== 'number') {
throw new TypeError('The ' + noName + ' should be Number type')
Expand All @@ -120,4 +139,4 @@ const isNumber = (number, noName = 'number') => {
}
}

export { volCuboid, volCube, volCone, volPyramid, volCylinder, volTriangularPrism, volPentagonalPrism, volSphere, volHemisphere }
export { volCuboid, volCube, volCone, volPyramid, volCylinder, volTriangularPrism, volPentagonalPrism, volSphere, volHemisphere, volTorus }