📜  程序来查找任何正多边形的外接圆

📅  最后修改于: 2021-04-21 23:07:15             🧑  作者: Mango

给定一个n边的多边形,边长为a 。任务是找到多边形的cicumcircle的面积。
例子:

Input: n = 10, a = 3
Output: 1.99737

Input: n = 5, a = 6
Output: 3.02487

方法:正则n形将圆分成n个部分,因此三角形的中心角是一个完整的圆,除以n360度/ n
余弦定律应用于三角形的三个边长,我们得到

下面是上述方法的实现:

C++
// C++ Program to find the radius
// of the circumcircle of the given polygon
 
#include 
using namespace std;
 
// Function to find the radius
// of the circumcircle
float findRadiusOfcircumcircle(float n, float a)
{
 
    // these cannot be negative
    if (n < 0 || a < 0)
        return -1;
 
    // Radius of the circumcircle
    float radius = a / sqrt(2 - (2 * cos(360 / n)));
 
    // Return the radius
    return radius;
}
 
// Driver code
int main()
{
 
    float n = 5, a = 6;
 
    // Find the radius of the circumcircle
    cout << findRadiusOfcircumcircle(n, a) << endl;
 
    return 0;
}


Java
// Java Program to find the radius
// of the circumcircle of the given polygon
 
import java.io.*;
 
class GFG {
   
// Function to find the radius
// of the circumcircle
static float findRadiusOfcircumcircle(float n, float a)
{
 
    // these cannot be negative
    if (n < 0 || a < 0)
        return -1;
 
    // Radius of the circumcircle
    float radius = (float)(a / Math.sqrt(2 - (2 * Math.cos(360 / n))));
 
    // Return the radius
    return radius;
}
 
// Driver code
 
    public static void main (String[] args) {
        float n = 5, a = 6;
 
    // Find the radius of the circumcircle
    System.out.println( findRadiusOfcircumcircle(n, a)) ;
 
    }
}
// This code is contributed
// by anuj_67..


Python 3
# Python3 Program to find the
# radius of the circumcircle
# of the given polygon
 
# from math import all methods
from math import *
 
# Function to find the radius
# of the circumcircle
def findRadiusOfcircumcircle(n, a) :
 
    # these cannot be negative
    if n < 0 or a < 0 :
        return -1
 
    # Radius of the circumcircle
    radius = a / sqrt(2 - (2 * cos(360 / n)))
 
    # Return the radius
    return radius
 
# Driver code
if __name__ == "__main__" :
 
    n , a = 5, 6
 
    # Find the radius of the circumcircle
    print(round(findRadiusOfcircumcircle(n, a), 5))
 
# This code is contributed
# by ANKITRAI1


C#
// C# Program to find the radius
// of the circumcircle of the given polygon
using System;
 
class GFG
{
 
// Function to find the radius
// of the circumcircle
static float findRadiusOfcircumcircle(float n,
                                      float a)
{
 
    // these cannot be negative
    if (n < 0 || a < 0)
        return -1;
 
    // Radius of the circumcircle
    float radius = (float)(a / Math.Sqrt(2 -
                   (2 * Math.Cos(360 / n))));
 
    // Return the radius
    return radius;
}
 
// Driver code
public static void Main ()
{
    float n = 5, a = 6;
 
    // Find the radius of the circumcircle
    Console.WriteLine(findRadiusOfcircumcircle(n, a));
}
}
 
// This code is contributed
// by anuj_67


PHP


Javascript


输出:
3.02487