给定六边形对角线的长度d。您的任务是找到该六边形的面积。
例子:
Input : 5
Output : Area of Hexagon: 16.238
Input : 10
Output : Area of Hexagon: 64.9519
六边形
六角形是具有六个相等边和所有相等角度的规则多边形。六角形的内角各为120度,六角形的所有角之和为720度。
设d为六角形的对角线,则公式为:
面积=
以上公式如何运作?
我们知道边长为a =(3√3(a) 2 )/ 2的六边形面积。由于所有边的大小相同且角度为120度,因此d = 2a或a = d / 2。放置此值后,我们得到的面积为(3√3(d) 2 )/ 8。
下面是实现。
C++
// C++ program to find the area of Hexagon with given diagonal
#include
using namespace std;
// Function to calculate area
float hexagonArea(float d)
{
// Formula to find area
return (3 * sqrt(3) * pow(d, 2)) / 8;
}
// Main
int main()
{
float d = 10;
cout << "Area of hexagon: " << hexagonArea(d);
return 0;
}
Java
// Java program to find the area of
// Hexagon with given diagonal
import java.lang.Math;
public class GfG {
// Function to calculate area
public static float hexagonArea(float d)
{
// Formula to find area
return (float)((3 * Math.sqrt(3) * d * d) / 8);
}
public static void main(String []args) {
float d = 10;
System.out.println("Area of hexagon: " + hexagonArea(d));
}
}
// This code is contributed by Rituraj Jain
Python3
# Python3 program to find the area
# of Hexagon with given diagonal
from math import sqrt
# Function to calculate area
def hexagonArea(d) :
# Formula to find area
return (3 * sqrt(3) * pow(d, 2)) / 8
# Driver ode
if __name__ == "__main__" :
d = 10
print("Area of hexagon:",
round(hexagonArea(d), 3))
# This code is contributed by Ryuga
C#
// C# program to find the area of
// Hexagon with given diagonal
using System;
public class GFG{
// Function to calculate area
public static float hexagonArea(float d)
{
// Formula to find area
return (float)((3 * Math.Sqrt(3) * d * d) / 8);
}
//Code driven
static public void Main (){
float d = 10;
Console.WriteLine("Area of hexagon: " + hexagonArea(d));
}
//This code is contributed by Tushil.
}
PHP
Javascript
输出:
Area of Hexagon: 64.952
时间复杂度: O(1)