Area of largest Circle inscribe in N-sided Regular polygon - GeeksforGeeks

19 Jan, 2022
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/NSo, angle x = 180/n Now, tan(x) = (a / 2) * rSo, r = a / ( 2 * tan(x))So, Area of the Inscribed Circle is,  
 A = Πr2 = Π * (a / (2 * tan(x))) * (a / (2*tan(x)))
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the area of a circle in// inscribed in polygon #include <bits/stdc++.h>using namespace std; // Function to find the area// of a circlefloat InscribedCircleArea(float n, float a){    // Side and side length cannot be negative    if (a < 0 && n < 0)        return -1;     // degree converted to radians    float r = a / (2 * tan((180 / n) * 3.14159 / 180));     // area of circle    float Area = (3.14) * (r) * (r);     return Area;} // Driver codeint main(){     // no.  of sides    float n = 6;     // side length    float a = 4;     cout << InscribedCircleArea(n, a) << endl;     return 0;}
// Java Program to find the area of a circle// inscribed in a polygonimport java.io.*; class GFG {     // Function to find the area    // of a regular polygon    static float InscribedCircleArea(float n, float a)    {        // Side and side length cannot be negative        if (a < 0 && n < 0)            return -1;         // degree converted to radians        float r = a / (float)(2 * Math.tan((180 / n) * 3.14159 / 180));         // area of circle        float Area = (float)(3.14) * (r) * (r);         return Area;    }     // Driver code     public static void main(String[] args)    {         // no.  of sides        float n = 6;         // side length        float a = 4;         System.out.println(InscribedCircleArea(n, a));    }}
# Python 3 Program to find the area# of a circle inscribed# in a polygonfrom math import tan # Function to find the area of a# circledef InscribedCircleArea(n, a):    # Side and side length cannot    # be negative    if (a < 0 and n < 0):        return -1     # degree converted to radians    r = a/(2 * tan((180 / n) * 3.14159 / 180));     # area of circle    Area = 3.14 * r * r     return Area # Driver codeif __name__ == '__main__':    a = 4    n = 6     print('{0:.6}'.format(InscribedCircleArea(n, a))) # This code is contributed by# Chandan Agrawal
// C# Program to find the area of a circle// inscribed in a polygonusing System; class GFG{ // Function to find the area// of a regular polygonstatic float InscribedCircleArea(float n, float a){    // Side and side length cannot be negative    if (a < 0 && n < 0)        return -1;     // degree converted to radians    float r = a / (float)(2 * Math.Tan((180 / n) *                                 3.14159 / 180));     // area of circle    float Area = (float)(3.14) * (r) * (r);     return Area;} // Driver codepublic static void Main(){     // no. of sides    float n = 6;     // side length    float a = 4;     Console.WriteLine(InscribedCircleArea(n, a));}} // This code is contributed by Ryuga
<?php// PHP Program to find the area// of a circle inscribed// in a polygon // Function to find the area of a// circlefunction InscribedCircleArea($n, $a){    // Side and side length cannot    // be negative    if ($a < 0 && $n < 0)        return -1;     // degree converted to radians    $r = $a / (2 * tan((180 / $n) * 3.14159 / 180));     // area of circle    $Area = 3.14 * $r * $r;     return $Area;} // Driver code$a = 4;$n = 6;echo(InscribedCircleArea($n, $a)); // This code contributed by PrinciRaj1992?>
<script>// Javascript Program to find the area of a circle// inscribed in a polygon     // Function to find the area    // of a regular polygon    function InscribedCircleArea( n ,a)    {             // Side and side length cannot be negative        if (a < 0 && n < 0)            return -1;         // degree converted to radians        let r = a /  (2 * Math.tan((180 / n) * 3.14159 / 180));         // area of circle        let Area =  (3.14) * (r) * (r);        return Area;    }     // Driver code     // no. of sides    let n = 6;     // side length    let a = 4;     document.write(InscribedCircleArea(n, a).toFixed(4));     // This code is contributed by 29AjayKumar</script>
37.6801
 
ankthon
princiraj1992
29AjayKumar
sumitgumber28
area-volume-programs
math
school-programming
Geometric
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull | Set 2 (Graham Scan)
Given n line segments, find if any two segments intersect
Closest Pair of Points | O(nlogn) Implementation
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Check whether a given point lies inside a triangle or not
Polygon Clipping | Sutherland–Hodgman Algorithm
Program To Check whether a Triangle is Equilateral, Isosceles or Scalene
Check if two given circles touch or intersect each other
Window to Viewport Transformation in Computer Graphics with Implementation
Sum of Manhattan distances between all pairs of points