📜  正六边形的对角线

📅  最后修改于: 2021-10-23 08:05:52             🧑  作者: Mango

给定一个正六边形边的整数a ,任务是找到并打印其对角线的长度。
例子:

方法:我们知道多边形的内角和 = (n – 2) * 180其中, n是多边形的边数。
因此,六边形的内角和 = 4 * 180 = 720并且每个内角将为120
现在,我们必须找到BC = 2 * x 。如果我们在BC上画一条垂直的AO ,我们将看到BOOC 中的BC的垂直平分线,因为三角形AOBAOC彼此全等。
所以,在三角形AOB 中sin(60) = x / ax = 0.866 * a
因此,对角线长度将为2 * x1.73 * a
下面是上述方法的实现:

C++
// C++ Program to find the diagonal
// of a regular hexagon
#include 
using namespace std;
 
// Function to find the diagonal
// of a regular hexagon
float hexDiagonal(float a)
{
 
    // Side cannot be negative
    if (a < 0)
        return -1;
 
    // Length of the diagonal
    float d = 1.73 * a;
    return d;
}
 
// Driver code
int main()
{
    float a = 9;
    cout << hexDiagonal(a) << endl;
    return 0;
}


Java
// Java Program to find the diagonal
// of a regular hexagon
 
public class GFG
{
 
    // Function to find the diagonal
    // of a regular hexagon
    static double hexDiagonal(float a)
    {
     
        // Side cannot be negative
        if (a < 0)
            return -1;
     
        // Length of the diagonal
        double d = (double)1.73 * a;
        return d;
    }
     
    // Driver code
    public static void main(String []args)
    {
        float a = 9;
        System.out.println(hexDiagonal(a)) ;
    }
    // This code is contributed by Ryuga
}


Python3
# Python3 Program to find the diagonal
# of a regular hexagon
 
# Function to find the diagonal
# of a regular hexagon
def hexDiagonal(a):
 
    # Side cannot be negative
    if (a < 0):
        return -1;
 
    # Length of the diagonal
    d = 1.73 * a;
    return d;
 
 
# Driver code
a = 9;
print(hexDiagonal(a));
 
# This code is contributed
# by Akanksha Rai


C#
// C# Program to find the diagonal 
// of a regular hexagon
using System ;
public class GFG
{
   
    // Function to find the diagonal 
    // of a regular hexagon 
    static double hexDiagonal(float a) 
    { 
       
        // Side cannot be negative 
        if (a < 0) 
            return -1; 
       
        // Length of the diagonal 
        double d = (double)1.73 * a; 
        return d; 
    } 
       
    // Driver code 
    public static void Main()
    { 
        float a = 9; 
        Console.WriteLine(hexDiagonal(a)) ; 
    } 
    // This code is contributed by Subhadeep
}


PHP


Javascript


输出:
15.57

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