📜  被圆外接的正 n 边多边形的边

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

给定两个整数rn ,其中n是正多边形的边数, r是该多边形外接的圆的半径。任务是找到多边形边的长度。

图像

例子:

方法:考虑上面的图像,让角度 AOBtheta然后theta = 360 / n
在直角三角形中$\Delta AOC$   角度 ACO = 90 度角度 AOC = theta / 2
所以, AC = OA * sin(theta / 2) = r * sin(theta / 2)
因此,多边形的边AB = 2 * AC2 * r * sin(theta / 2)
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to calculate the side of the polygon
// circumscribed in a circle
float calculateSide(float n, float r)
{
    float theta, theta_in_radians;
 
    theta = 360 / n;
    theta_in_radians = theta * 3.14 / 180;
 
    return 2 * r * sin(theta_in_radians / 2);
}
 
// Driver Code
int main()
{
 
    // Total sides of the polygon
    float n = 3;
 
    // Radius of the circumscribing circle
    float r = 5;
 
    cout << calculateSide(n, r);
}


Java
// Java  implementation of the approach
import java.lang.Math;
import java.io.*;
 
class GFG {
     
// Function to calculate the side of the polygon
// circumscribed in a circle
static double calculateSide(double  n, double r)
{
    double theta, theta_in_radians;
 
    theta = 360 / n;
    theta_in_radians = theta * 3.14 / 180;
 
    return 2 * r * Math.sin(theta_in_radians / 2);
}
 
// Driver Code
    public static void main (String[] args) {
 
    // Total sides of the polygon
    double n = 3;
 
    // Radius of the circumscribing circle
    double r = 5;
    System.out.println (calculateSide(n, r));
    }
//This code is contributed by akt_mit   
}


Python3
# Python 3 implementation of the approach
from math import sin
 
# Function to calculate the side of
# the polygon circumscribed in a circle
def calculateSide(n, r):
    theta = 360 / n
    theta_in_radians = theta * 3.14 / 180
 
    return 2 * r * sin(theta_in_radians / 2)
 
# Driver Code
if __name__ == '__main__':
     
    # Total sides of the polygon
    n = 3
 
    # Radius of the circumscribing circle
    r = 5
 
    print('{0:.5}'.format(calculateSide(n, r)))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# implementation of the approach
 
using System;
 
class GFG {
         
    // Function to calculate the side of the polygon
    // circumscribed in a circle
    static double calculateSide(double n, double r)
    {
        double theta, theta_in_radians;
     
        theta = 360 / n;
        theta_in_radians = theta * 3.14 / 180;
     
        return Math.Round(2 * r * Math.Sin(theta_in_radians / 2),4);
    }
 
        // Driver Code
    public static void Main () {
 
    // Total sides of the polygon
    double n = 3;
 
    // Radius of the circumscribing circle
    double r = 5;
     
    Console.WriteLine(calculateSide(n, r));
    }
    // This code is contributed by Ryuga
}


PHP


Javascript


输出:
8.6576