Here's a Ruby function that takes the number of sides of a regular polygon and the radius of its inscribing circle as arguments, and returns the area of the polygon:
main.rb210 chars6 lines
The formula for the area of a regular polygon is:
main.rb50 chars2 lines
where num_sides
is the number of sides of the polygon, length_of_side
is the length of each side, and apothem
is the distance from the center of the polygon to the midpoint of a side.
To calculate apothem
and length_of_side
, we can use the following formulas:
main.rb90 chars4 lines
where radius
is the radius of the inscribed circle of the polygon, and pi
is the mathematical constant.
Thus, the function uses the above formulas to compute apothem
and length_of_side
, plugs them into the formula for the area, and returns the result.
gistlibby LogSnag