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 numberOfSidesnumber-of-sides, or number_of_sides sides inside a circle of radius circleRadiuscircle-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
def area_of_polygon_inside_circle(circle_radius, number_of_sides):
    r = circle_radius
    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
def area_of_polygon_inside_circle(circle_radius, number_of_sides):
    angle = radians(360/(number_of_sides*2))
    reverse = circle_radius*sin(angle)
    adjoining = circle_radius*cos(angle)
    return spherical(adjoining*reverse*number_of_sides,3)

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments