Program to find the Circumcircle of any regular polygon
Last Updated :
27 Aug, 2022
Given a n-sided polygon with side length a. The task is to find the area of the circumcircle of the polygon.
Examples:
Input: n = 10, a = 3
Output: 1.99737
Input: n = 5, a = 6
Output: 3.02487
Approach: A regular n-gon divides the circle into n pieces, so the central angle of the triangle is a full circle divided by n: 360 deg/n.
Applying the law of cosines for the three side lengths of the triangle, we get
c2 = a2 + b2 - 2ab cos C
or, a2 = r2 + r2 - 2rr cos (360/n)
or, a2 = 2r2 - 2r2 cos (360/n)
or, c2 = r2 (2 - 2 cos (360/n))
so, a=r?(2-2cos(360/n))
Therefore,
r=a/?(2-2cos(360/n))
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float findRadiusOfcircumcircle( float n, float a)
{
if (n < 0 || a < 0)
return -1;
float radius = a / sqrt (2 - (2 * cos (360 / n)));
return radius;
}
int main()
{
float n = 5, a = 6;
cout << findRadiusOfcircumcircle(n, a) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float findRadiusOfcircumcircle( float n, float a)
{
if (n < 0 || a < 0 )
return - 1 ;
float radius = ( float )(a / Math.sqrt( 2 - ( 2 * Math.cos( 360 / n))));
return radius;
}
public static void main (String[] args) {
float n = 5 , a = 6 ;
System.out.println( findRadiusOfcircumcircle(n, a)) ;
}
}
|
Python 3
from math import *
def findRadiusOfcircumcircle(n, a) :
if n < 0 or a < 0 :
return - 1
radius = a / sqrt( 2 - ( 2 * cos( 360 / n)))
return radius
if __name__ = = "__main__" :
n , a = 5 , 6
print ( round (findRadiusOfcircumcircle(n, a), 5 ))
|
C#
using System;
class GFG
{
static float findRadiusOfcircumcircle( float n,
float a)
{
if (n < 0 || a < 0)
return -1;
float radius = ( float )(a / Math.Sqrt(2 -
(2 * Math.Cos(360 / n))));
return radius;
}
public static void Main ()
{
float n = 5, a = 6;
Console.WriteLine(findRadiusOfcircumcircle(n, a));
}
}
|
PHP
<?php
function findRadiusOfcircumcircle( $n , $a )
{
if ( $n < 0 || $a < 0)
return -1;
$radius = $a / sqrt(2 - (2 *
cos (360 / $n )));
return $radius ;
}
$n = 5;
$a = 6;
echo findRadiusOfcircumcircle( $n , $a );
?>
|
Javascript
<script>
function findRadiusOfcircumcircle(n , a)
{
if (n < 0 || a < 0)
return -1;
var radius = (a / Math.sqrt(2 - (2 * Math.cos(360 / n))));
return radius;
}
var n = 5, a = 6;
document.write( findRadiusOfcircumcircle(n, a).toFixed(5)) ;
</script>
|
Time Complexity : O(log(n)) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...