Area of largest Circle inscribe in N-sided Regular polygon
Given a regular polygon of N sides with side length a. The task is to find the area of the Circle which inscribed in the polygon.
Note : This problem is mixed version of This and This
Examples:
Input: N = 6, a = 4
Output: 37.6801
Explanation:
In this, the polygon have 6 faces
and as we see in fig.1 we clearly see
that the angle x is 30 degree
so the radius of circle will be ( a / (2 * tan(30)))
Therefore, r = a?3/2
Input: N = 8, a = 8
Output: 292.81
Explanation:
In this, the polygon have 8 faces
and as we see in fig.2 we clearly see
that the angle x is 22.5 degree
so the radius of circle will be ( a / (2 * tan(22.5)))
Therefore, r = a/0.828
Approach: In the figure above, we see the polygon can be divided into N equal triangles. Looking into one of the triangles, we see that the whole angle at the center can be divided into = 360/N
So, angle x = 180/n
Now, tan(x) = (a / 2) * r
So, r = a / ( 2 * tan(x))
So, Area of the Inscribed Circle is,
A = ?r² = ? * (a / (2 * tan(x))) * (a / (2*tan(x)))
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float InscribedCircleArea( float n, float a)
{
if (a < 0 && n < 0)
return -1;
float r = a / (2 * tan ((180 / n) * 3.14159 / 180));
float Area = (3.14) * (r) * (r);
return Area;
}
int main()
{
float n = 6;
float a = 4;
cout << InscribedCircleArea(n, a) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float InscribedCircleArea( float n, float a)
{
if (a < 0 && n < 0 )
return - 1 ;
float r = a / ( float )( 2 * Math.tan(( 180 / n) * 3.14159 / 180 ));
float Area = ( float )( 3.14 ) * (r) * (r);
return Area;
}
public static void main(String[] args)
{
float n = 6 ;
float a = 4 ;
System.out.println(InscribedCircleArea(n, a));
}
}
|
Python3
from math import tan
def InscribedCircleArea(n, a):
if (a < 0 and n < 0 ):
return - 1
r = a / ( 2 * tan(( 180 / n) * 3.14159 / 180 ));
Area = 3.14 * r * r
return Area
if __name__ = = '__main__' :
a = 4
n = 6
print ( '{0:.6}' . format (InscribedCircleArea(n, a)))
|
C#
using System;
class GFG
{
static float InscribedCircleArea( float n, float a)
{
if (a < 0 && n < 0)
return -1;
float r = a / ( float )(2 * Math.Tan((180 / n) *
3.14159 / 180));
float Area = ( float )(3.14) * (r) * (r);
return Area;
}
public static void Main()
{
float n = 6;
float a = 4;
Console.WriteLine(InscribedCircleArea(n, a));
}
}
|
PHP
<?php
function InscribedCircleArea( $n , $a )
{
if ( $a < 0 && $n < 0)
return -1;
$r = $a / (2 * tan((180 / $n ) * 3.14159 / 180));
$Area = 3.14 * $r * $r ;
return $Area ;
}
$a = 4;
$n = 6;
echo (InscribedCircleArea( $n , $a ));
?>
|
Javascript
<script>
function InscribedCircleArea( n ,a)
{
if (a < 0 && n < 0)
return -1;
let r = a / (2 * Math.tan((180 / n) * 3.14159 / 180));
let Area = (3.14) * (r) * (r);
return Area;
}
let n = 6;
let a = 4;
document.write(InscribedCircleArea(n, a).toFixed(4));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...