📜  n边正多边形的上心点

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

这里给定一个规则的 n 边多边形的边长a ,任务是找到它的 Apothem 的长度。
Apothem是从多边形中心绘制的垂直于其一侧的线。
例子:

Input a = 9, n = 6
Output: 7.79424

Input: a = 8, n = 7
Output: 8.30609

方法

下面是上述方法的实现。

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


Java
// Java Program to find the apothem of a
// regular polygon with given side length
import java.util.*;
 
class GFG
{
 
    // Function to find the apothem
    // of a regular polygon
    double polyapothem(double n, double a)
    {
 
        // Side and side length cannot be negative
        if (a < 0 && n < 0)
            return -1;
 
        // Degree converted to radians
        return (a / (2 * java.lang.Math.tan((180 / n)
                * 3.14159 / 180)));
    }
 
// Driver code
public static void main(String args[])
{
    double a = 9, n = 6;
    GFG g=new GFG();
    System.out.println(g.polyapothem(n, a));
}
 
}
//This code is contributed by Shivi_Aggarwal


Python3
# Python 3 Program to find the apothem
# of a regular polygon with given side
# length
from math import tan
 
# Function to find the apothem
# of a regular polygon
def polyapothem(n, a):
     
    # Side and side length cannot be negative
    if (a < 0 and n < 0):
        return -1
 
    # Degree converted to radians
    return a / (2 * tan((180 / n) *
                   3.14159 / 180))
 
# Driver code
if __name__ == '__main__':
    a = 9
    n = 6
    print('{0:.6}'.format(polyapothem(n, a)))
     
# This code is contributed by
# Sahil_Shelangia


C#
// C# Program to find the apothem of a
// regular polygon with given side length
using System;
 
class GFG
{
 
// Function to find the apothem
// of a regular polygon
static double polyapothem(double n,
                          double a)
{
 
    // Side and side length cannot
    // be negative
    if (a < 0 && n < 0)
        return -1;
 
    // Degree converted to radians
    return (a / (2 * Math.Tan((180 / n) *
                       3.14159 / 180)));
}
 
// Driver code
public static void Main()
{
    double a = 9, n = 6;
    Console.WriteLine(Math.Round(polyapothem(n, a), 4));
}
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
7.79424

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