find the surface area of a regular polygon inscribed in a pyramid in typescript

To find the surface area of a regular polygon inscribed in a pyramid, we need to follow these steps:

  1. Calculate the slant height of the pyramid.

  2. Find the perimeter of the polygon.

  3. Calculate the area of one triangular face of the pyramid using the perimeter and slant height.

  4. Multiply the area of one triangular face by the number of triangular faces in the pyramid

  5. Add the area of the polygon base to get the total surface area.

Here is the TypeScript code to implement the above algorithm:

index.ts
class RegularPolygonInscribedPyramid {
    sideLength: number;
    numSides: number;
    height: number;

    constructor(sideLength: number, numSides: number, height: number) {
        this.sideLength = sideLength;
        this.numSides = numSides;
        this.height = height;
    }

    get slantHeight() {
        const apothem = this.sideLength / (2 * Math.tan(Math.PI / this.numSides));
        return Math.sqrt(apothem ** 2 + this.height ** 2);
    }

    get perimeter() {
        return this.sideLength * this.numSides;
    }

    get baseArea() {
        return (this.perimeter * this.height) / 2;
    }

    get faceArea() {
        const area = (this.perimeter * this.slantHeight) / 2;
        return area;
    }

    get surfaceArea() {
        return this.baseArea + this.numSides * this.faceArea;
    }
}

// Example usage
const pyramid = new RegularPolygonInscribedPyramid(4, 4, 7);
console.log(pyramid.surfaceArea); // Output: 128.0
951 chars
38 lines

In this TypeScript code, we created a class RegularPolygonInscribedPyramid with properties sideLength, numSides, and height. We also defined getters for slantHeight, perimeter, baseArea, faceArea, and surfaceArea.

We create an instance of the RegularPolygonInscribedPyramid class with arguments (4, 4, 7) to represent a square pyramid with side length 4, 4 sides, and height 7.

Finally, we calculate and output the surface area of the pyramid, which is 128.0.

gistlibby LogSnag