# How to Calculate the area of a regular N sides polygon inside a circle of radius R in Python

## The challenge

Write the following function:

`.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}`def area_of_polygon_inside_circle(circle_radius, number_of_sides):`Code language: Python (python)`

It should calculate the area of a regular polygon of `numberOfSides``number-of-sides`, or `number_of_sides` sides inside a circle of radius `circleRadius``circle-radius`, or `circle_radius` which passes through all the vertices of the polygon (such a circle is called circumscribed circle or circumcircle). The answer should be a number rounded to 3 decimal places.

Input/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```Code language: Python (python)```

## The solution in Python code

Option 1:

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

def area_of_polygon_inside_circle(r, n):
return round(0.5 * n * r ** 2 * sin(2 * pi / n), 3)
```Code language: Python (python)```

Option 2:

``````import math
s = number_of_sides
a = (s * (r ** 2) * math.sin(2*math.pi/s))/2
return round(a, 3)
```Code language: Python (python)```

Option 3:

``````from math import sin,cos,radians
```Code language: Python (python)```

## Test cases to validate our solution

``````test.describe('Example Tests')
test.it('ex1')
test.assert_equals(area_of_polygon_inside_circle(3, 3), 11.691)
test.it('ex2')
test.assert_equals(area_of_polygon_inside_circle(2, 4), 8)
test.it('ex3')
test.assert_equals(area_of_polygon_inside_circle(2.5, 5), 14.86)
```Code language: Python (python)```
Tags:
Subscribe
Notify of