find the volume of a regular polygon circumscribed around a triangular prism in javascript

To find the volume of a regular polygon circumscribed around a triangular prism, we first need to find some measures of the polygon.

Let 'n' be the number of sides of the regular polygon, 'a' be the length of each side, 'h' be the height of the triangular prism and 'r' be the radius of the circumcircle of the polygon.

The formula to find 'a' is a = (2 * h) / (n * √3).

The formula to find 'r' is r = (a / 2) / sin(π/n).

Once we have 'a' and 'r', we can find the area of the polygon using the formula:

Area = (n * a^2) / (4 * tan(π/n)).

Now, the volume of the prism can be calculated using the formula:

Volume = Area * h.

Here is the code to implement the above formulas in JavaScript:

// function to find the volume of regular polygon circumscribed around a triangular prism
function findVolume(n, h) {
  const a = (2 * h) / (n * Math.sqrt(3));
  const r = (a / 2) / Math.sin(Math.PI / n);
  const area = (n * Math.pow(a, 2)) / (4 * Math.tan(Math.PI / n));
  const volume = area * h;
  return volume;
}

// example usage
const sides = 6; // hexagon
const height = 10;
const volume = findVolume(sides, height);
console.log("Volume:", volume);
457 chars
15 lines

gistlibby LogSnag