📜  N边正多边形中最大的圆刻面积

📅  最后修改于: 2021-04-22 05:55:02             🧑  作者: Mango

给定N边的规则多边形,边长为a 。任务是找到刻在多边形上的圆的区域。
注意:此问题是This和This的混合版本
例子:

Input: N = 6, a = 4
Output: 37.6801
Explanataion:

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
Explanataion:

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

方法:在上图中,我们可以看到多边形可以分为N个相等的三角形。查看三角形中的一个,我们可以看到中心的整个角度可以分为= 360 / N
因此,角度x = 180 / n
现在, tan(x)=(a / 2)* r
因此, r = a /(2 * tan(x))
因此,内切圆的面积为

A = Πr² = Π * (a / (2 * tan(x))) * (a / (2*tan(x)))

下面是上述方法的实现:

C++
// C++ Program to find the area of a circle in
// inscribed in polygon
 
#include 
using namespace std;
 
// Function to find the area
// of a circle
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 / (2 * tan((180 / n) * 3.14159 / 180));
 
    // area of circle
    float Area = (3.14) * (r) * (r);
 
    return Area;
}
 
// Driver code
int main()
{
 
    // no.  of sides
    float n = 6;
 
    // side length
    float a = 4;
 
    cout << InscribedCircleArea(n, a) << endl;
 
    return 0;
}


Java
// Java Program to find the area of a circle
// inscribed in a polygon
import 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));
    }
}


Python3
# Python 3 Program to find the area
# of a circle inscribed
# in a polygon
from math import tan
 
# Function to find the area of a
# circle
def 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 code
if __name__ == '__main__':
    a = 4
    n = 6
 
    print('{0:.6}'.format(InscribedCircleArea(n, a)))
 
# This code is contributed by
# Chandan Agrawal


C#
// C# Program to find the area of a circle
// inscribed in a polygon
using System;
 
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()
{
 
    // no. of sides
    float n = 6;
 
    // side length
    float a = 4;
 
    Console.WriteLine(InscribedCircleArea(n, a));
}
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
37.6801