find the surface area of a regular polygon circumscribed around a triangular prism in php

To find the surface area of a regular polygon circumscribed around a triangular prism in PHP, we need to follow the below steps:

  1. Determine the apothem length 'a' for the polygon. The apothem is the distance between the center of the polygon to the midpoint of any one of its sides.

  2. Determine the side length 's' of the polygon. This can be found by using the formula: s = (2 * r) * (sin(180/n)) where 'r' is the radius of the circumscribed circle and 'n' is the number of sides of the polygon.

  3. Calculate the perimeter 'P' of the polygon by using the formula: P = n * s where 'n' is the number of sides.

  4. Determine the lateral surface area 'L' of the triangular prism by using the formula: L = ph where 'p' is the perimeter of the base polygon and 'h' is the height of the prism.

  5. Determine the base surface area 'B' of the triangular prism by using the formula: B = 1/2 * p * ap where 'ap' is the apothem of the polygon.

  6. Calculate the total surface area 'T' of the triangular prism by using the formula: T = L + 2B.

Here's the PHP code that implements the above algorithm:

main.php
<?php

// Function to find the surface area of a regular polygon
// circumscribed around a triangular prism
function findSurfaceArea($n, $r, $h)
{
    // Calculate apothem length
    $a = $r * cos(pi() / $n);

    // Calculate side length
    $s = 2 * $r * sin(pi() / $n);

    // Calculate perimeter
    $p = $n * $s;

    // Calculate lateral surface area
    $L = $p * $h;

    // Calculate base surface area
    $B = 0.5 * $p * $a;

    // Calculate total surface area
    $T = $L + 2 * $B;

    return $T;
}

// Test the function
$n = 6;  // Number of sides
$r = 5;  // Radius of the circumscribed circle
$h = 10; // Height of the prism

$T = findSurfaceArea($n, $r, $h);

echo "Surface Area of the Triangular Prism = " . $T . " sq.units";

?>
749 chars
38 lines

gistlibby LogSnag