Wednesday, February 21, 2024
HomeSoftware EngineeringHow you can Calculate the realm of a daily N sides polygon...

# How you can Calculate the realm of a daily N sides polygon inside a circle of radius R in Python

## The problem#

Write the next perform:

``````def area_of_polygon_inside_circle(circle_radius, number_of_sides):
``````

It ought to calculate the realm of a daily polygon of `numberOfSides``number-of-sides`, or `number_of_sides` sides inside a circle of radius `circleRadius``circle-radius`, or `circle_radius` which passes via all of the vertices of the polygon (such a circle known as circumscribed circle or circumcircle). The reply ought to be a quantity rounded to three decimal locations.

Enter/Output Examples

``````area_of_polygon_inside_circle(3, 3) # returns 11.691
area_of_polygon_inside_circle(5.8, 7) # returns 92.053
area_of_polygon_inside_circle(4, 5) # returns 38.042
``````

## The answer in Python code#

Choice 1:

``````from math import sin, pi

def area_of_polygon_inside_circle(r, n):
return spherical(0.5 * n * r ** 2 * sin(2 * pi / n), 3)
``````

Choice 2:

``````import math
s = number_of_sides
a = (s * (r ** 2) * math.sin(2*math.pi/s))/2
return spherical(a, 3)
``````

Choice 3:

``````from math import sin,cos,radians
``````

## Take a look at instances to validate our resolution#

``````check.describe('Instance Assessments')
check.it('ex1')
check.assert_equals(area_of_polygon_inside_circle(3, 3), 11.691)
check.it('ex2')
check.assert_equals(area_of_polygon_inside_circle(2, 4), 8)
check.it('ex3')
check.assert_equals(area_of_polygon_inside_circle(2.5, 5), 14.86)
``````
RELATED ARTICLES