📜  查找六角形的对角线长度

📅  最后修改于: 2021-04-29 13:27:19             🧑  作者: Mango

给定边长a的正六边形,任务是找到对角线的长度。
例子

Input : a = 4
Output : 8

Input : a = 7
Output : 14

从图中可以清楚地看到,三角形ABC是等边三角形,因此
AB = AC = BC = a
也很明显,对角线= 2 * AC2 * BC
所以六角形的对角线长度= 2 * a
下面是上述方法的实现:

C++
// C++ Program to find the diagonal
// of the hexagon
#include 
using namespace std;
 
// Function to find the diagonal
// of the hexagon
float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
int main()
{
    float a = 4;
 
    cout << hexadiagonal(a) << endl;
 
    return 0;
}


Java
// Java Program to find the diagonal
// of the hexagon
 
import java.io.*;
 
class GFG {
 
// Function to find the diagonal
// of the hexagon
static float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
 
 
    public static void main (String[] args) {
            float a = 4;
 
    System.out.print( hexadiagonal(a));
}
}
 
// This code is contributed
// by shs


Python3
# Python3 Program to find the diagonal
# of the hexagon
 
# Function to find the diagonal
# of the hexagon
def hexadiagonal(a):
 
    # side cannot be negative
    if (a < 0):
        return -1
 
    # diagonal of the hexagon
    return 2 * a
 
 
# Driver code
if __name__=='__main__':
    a = 4
    print(hexadiagonal(a))
 
# This code is contributed by
# Shivi_Aggarwal


C#
// C# Program to find the diagonal
// of the hexagon
using System;
 
class GFG {
 
    // Function to find the diagonal
    // of the hexagon
    static float hexadiagonal(float a)
    {
        // side cannot be negative
        if (a < 0)
            return -1;
         
        // diagonal of the hexagon
        return 2 * a;
    }
 
// Driver code
public static void Main()
{
   float a = 4;
   Console.WriteLine( hexadiagonal(a));
}
}
 
// This code is contributed
// by anuj_67..


PHP


Javascript


输出:
8