Area of a n-sided regular polygon with given Radius
Last Updated :
25 Jun, 2022
Given a regular polygon of N sides with radius(distance from the center to any vertex) R. The task is to find the area of the polygon.
Examples:
Input : r = 9, N = 6
Output : 210.444
Input : r = 8, N = 7
Output : 232.571
In the figure, we see that the polygon can be divided into N equal triangles.
Looking into one of the triangles, we see that the whole angle at the centre can be divided into = 360/N parts.
So, angle t = 180/N.
Looking into one of the triangles, we see,
h = rcost
a = rsint
We know,
area of the triangle = (base * height)/2
= r2sin(t)cos(t)
= r2*sin(2t)/2
So, area of the polygon:
A = n * (area of one triangle)
= n*r2*sin(2t)/2
= n*r2*sin(360/n)/2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float polyarea( float n, float r)
{
if (r < 0 && n < 0)
return -1;
float A = ((r * r * n) * sin ((360 / n) * 3.14159 / 180)) / 2;
return A;
}
int main()
{
float r = 9, n = 6;
cout << polyarea(n, r) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static double polyarea( double n, double r)
{
if (r < 0 && n < 0 )
return - 1 ;
double A = ((r * r * n) * Math.sin(( 360 / n) * 3.14159 / 180 )) / 2 ;
return A;
}
public static void main(String []args)
{
float r = 9 , n = 6 ;
System.out.println(polyarea(n, r));
}
}
|
Python3
from math import sin
def polyarea(n, r) :
if (r < 0 and n < 0 ) :
return - 1
A = (((r * r * n) * sin(( 360 / n) *
3.14159 / 180 )) / 2 );
return round (A, 3 )
if __name__ = = "__main__" :
r, n = 9 , 6
print (polyarea(n, r))
|
C#
using System;
class GFG
{
static double polyarea( double n, double r)
{
if (r < 0 && n < 0)
return -1;
double A = ((r * r * n) * Math.Sin((360 / n) * 3.14159 / 180)) / 2;
return A;
}
public static void Main()
{
float r = 9, n = 6;
Console.WriteLine(polyarea(n, r));
}
}
|
PHP
<?php
function polyarea( $n , $r )
{
if ( $r < 0 && $n < 0)
return -1;
$A = (( $r * $r * $n ) * sin((360 / $n ) *
3.14159 / 180)) / 2;
return $A ;
}
$r = 9;
$n = 6;
echo polyarea( $n , $r ). "\n" ;
?>
|
Javascript
<script>
function polyarea(n , r)
{
if (r < 0 && n < 0)
return -1;
var A = ((r * r * n) * Math.sin((360 / n) * 3.14159 / 180)) / 2;
return A;
}
var r = 9, n = 6;
document.write(polyarea(n, r).toFixed(5));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...