find the area of a regular polygon circumscribed around a circle in php

To find the area of a regular polygon circumscribed around a circle, we need to know the length of each side and the number of sides. We can use some basic trigonometry to calculate these values.

First, we need to find the radius of the circle. Let's say the diameter of the circle is $d$. The radius $r$ is then $d/2$.

main.php
$radius = $diameter / 2;
25 chars
2 lines

Next, we need to find the length of each side of the polygon. Let's say the number of sides is $n$. The angle of each sector of the circle is $360/n$ degrees. We can use this angle to find the length of each side using the formula $s = 2r\sin(\frac{\pi}{n})$.

main.php
$angle = 360 / $n;
$length = 2 * $radius * sin(pi() / $n);
59 chars
3 lines

Now that we have the length of each side, we can use the formula for the area of a regular polygon: $A = \frac{1}{4}ns^2\tan(\frac{\pi}{n})$.

main.php
$area = (1/4) * $n * pow($length, 2) * tan(pi() / $n);
55 chars
2 lines

Here's the complete code:

main.php
<?php
$diameter = 10;
$n = 6;

$radius = $diameter / 2;
$angle = 360 / $n;
$length = 2 * $radius * sin(pi() / $n);
$area = (1/4) * $n * pow($length, 2) * tan(pi() / $n);

echo "The area of a regular polygon with $n sides circumscribed around a circle with diameter $diameter is: $area";
?>
290 chars
12 lines

gistlibby LogSnag