📜  n边正多边形的对角线长度

📅  最后修改于: 2021-05-04 10:48:45             🧑  作者: Mango

给定一个边长a的n边规则多边形,任务是找到对角线的长度。
例子:

Input:  a = 9, n = 10
Output:  17.119

Input: a = 4, n = 5
Output: 6.47213

方法

C++
// C++ Program to find the diagonal
// of a regular polygon with given side length
#include 
using namespace std;
 
// Function to find the diagonal
// of a regular polygon
float polydiagonal(float n, float a)
{
 
    // Side and side length cannot be negative
    if (a < 0 && n < 0)
        return -1;
 
    // diagonal
    // degree converted to radians
    return 2 * a * sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180);
}
 
// Driver code
int main()
{
    float a = 9, n = 10;
    cout << polydiagonal(n, a) << endl;
 
    return 0;
}


Java
// Java Program to find the diagonal
// of a regular polygon with given side length
 
class GFG {
 
// Function to find the diagonal
// of a regular polygon
    static float polydiagonal(float n, float a) {
 
        // Side and side length cannot be negative
        if (a < 0 && n < 0) {
            return -1;
        }
 
        // diagonal
        // degree converted to radians
        return (float) (2 * a * Math.sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180));
    }
 
// Driver code
    public static void main(String[] args) {
        float a = 9, n = 10;
        System.out.printf("%.3f",polydiagonal(n, a));
 
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 Program to find the diagonal
# of a regular polygon with given side length
import math as mt
 
# Function to find the diagonal
# of a regular polygon
def polydiagonal(n, a):
 
    # Side and side length cannot
    # be negative
    if (a < 0 and n < 0):
        return -1
 
    # diagonal degree converted to radians
    return (2 * a * mt.sin((((n - 2) * 180) /
           (2 * n)) * 3.14159 / 180))
 
# Driver code
a, n = 9, 10
print(polydiagonal(n, a))
 
# This code is contributed
# by Mohit kumar 29


C#
// C#  Program to find the diagonal
// of a regular polygon with given side length
using System;
 
public class GFG{
     
// Function to find the diagonal
// of a regular polygon
    static float polydiagonal(float n, float a) {
 
        // Side and side length cannot be negative
        if (a < 0 && n < 0) {
            return -1;
        }
 
        // diagonal
        // degree converted to radians
        return (float) (2 * a * Math.Sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180));
    }
 
// Driver code
    static public void Main (){
            float a = 9, n = 10;
        Console.WriteLine(polydiagonal(n, a));
 
    }
}
 
// This code is contributed by  @Sachin...


PHP


Javascript


输出:
17.119