📜  正六边形内接圆的面积

📅  最后修改于: 2021-10-23 08:32:46             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程