find the surface area of a regular polygon circumscribed around a pyramid in javascript

To find the surface area of a regular polygon circumscribed around a pyramid, we need to know the perimeter and height of the pyramid. We can use the formula:

index.tsx
surface area = perimeter * slant height / 2 + base area
56 chars
2 lines

where the base area can be calculated using the formula for the area of a regular polygon:

index.tsx
base area = 0.25 * n * s^2 * cot(π/n)
38 chars
2 lines

where n is the number of sides, s is the length of each side, and cot is the cotangent function.

Here's an implementation in Javascript:

index.tsx
function surfaceAreaOfPyramidWithRegularPolygonBase(n, s, h) {
  const perimeter = n * s;
  const slantHeight = Math.sqrt(Math.pow(h, 2) + Math.pow(s/2, 2)) * 2;
  const baseArea = 0.25 * n * Math.pow(s, 2) * 1/Math.tan(Math.PI/n);

  const surfaceArea = perimeter * slantHeight / 2 + baseArea;

  return surfaceArea;
}
320 chars
10 lines

You can call this function with the number of sides n, the length of each side s, and the height h of the pyramid, and it will return the surface area.

gistlibby LogSnag