📜  刻在正六边形上的圆的面积

📅  最后修改于: 2021-04-29 01:28:26             🧑  作者: Mango

给定一个边长a的正六边形,任务是找到内切圆的面积,因为该圆与六个边的每一个相切。
例子:

Input: a = 4
Output: 37.68

Input: a = 10
Output: 235.5

方法
从图中可以清楚地看到,我们可以将正六边形划分为6个相同的等边三角形。
我们采用一个三角形OAB ,其中O为六边形或圆形的中心, AB为六边形的一侧。
MAB的中点,OMAB的垂直平分线,角AOM = 30度
然后在直角三角形OAM中,

下面是该方法的实现

C++
// C++ Program to find the area of the circle
// which can be inscribed within the hexagon
#include 
using namespace std;
 
// Function to find the area
// of the inscribed circle
float circlearea(float a)
{
 
    // the side cannot be negative
    if (a < 0)
        return -1;
 
    // area of the circle
    float A = (3.14 * 3 * pow(a, 2)) / 4;
 
    return A;
}
 
// Driver code
int main()
{
    float a = 4;
    cout << circlearea(a) << endl;
 
    return 0;
}


Java
//Java program to find the
//area of the circle
//which can be inscribed within the hexagon
 
import java.util.*;
 
class solution
{
static double circlearea(double a)
{
 
// the side cannot be negative
    if (a < 0)
    return -1;
 
// area of the circle
    double A = (3.14 * 3 * Math.pow(a,2) ) / 4;
 
    return A;
}
public static void main(String arr[])
{
    double a = 4;
    System.out.println(circlearea(a));
}
}


Python 3
# Python 3 program to find the
# area of the circle
# which can be inscribed within the hexagon
 
# Function to find the area
# of the inscribed circle
def circlearea(a) :
 
    # the side cannot be negative
    if a < 0 :
        return -1
 
    #  area of the circle
    A = (3.14 * 3 * pow(a,2)) / 4
 
    return A
 
 
# Driver code    
if __name__ == "__main__" :
 
    a = 4
    print(circlearea(a))
 
 
# This code is contributed by ANKITRAI1


C#
// C# program to find 
// the area of the circle
// which can be inscribed
// within the hexagon
using System;
 
class GFG
{
static double circlearea(double a)
{
 
    // the side cannot be negative
    if (a < 0)
    return -1;
 
    // area of the circle
    double A = (3.14 * 3 *
                Math.Pow(a, 2)) / 4;
 
    return A;
}
 
// Driver Code
public static void Main()
{
    double a = 4;
    Console.WriteLine(circlearea(a));
}
}
 
// This code is contributed
// by inder_verma


PHP


Javascript


输出:
37.68