find the volume of a regular polygon circumscribed around a cylinder in typescript

To find the volume of a regular polygon circumscribed around a cylinder, we need to first find the height of the cylinder. We can do this by dividing the perimeter of the polygon by the number of sides and then using this value to find the radius of the cylinder. Once we have the radius and height of the cylinder, we can use the formula for volume of a cylinder to compute the volume of the regular polygon.

Here's the code:

index.ts
function getVolumeOfRegularPolygonCircumscribedAroundCylinder(numOfSides: number, sideLength: number, height: number): number {
  const perimeter = numOfSides * sideLength;
  const radius = perimeter / (2 * Math.PI);
  const volume = Math.PI * radius ** 2 * height;
  return volume;
}
285 chars
7 lines

This function takes in the number of sides numOfSides, the length of each side sideLength, and the height of the cylinder height. It returns the volume of the regular polygon circumscribed around the cylinder.

Note that we assume that the base of the cylinder is a regular polygon. If it is not, the formula will not be correct.

gistlibby LogSnag