Program to find the Circumcircle of any regular polygon
Last Updated :
03 Sep, 2024
Given a n-sided polygon with side length a. The task is to find the area of the circumcircle of the polygon.
Examples:
Input: n = 10, a = 3
Output: 1.99737
Input: n = 5, a = 6
Output: 3.02487

Approach: A regular n-gon divides the circle into n pieces, so the central angle of the triangle is a full circle divided by n: 360 deg/n.
Applying the law of cosines for the three side lengths of the triangle, we get
c2 = a2 + b2 - 2ab cos C
or, a2 = r2 + r2 - 2rr cos (360/n)
or, a2 = 2r2 - 2r2 cos (360/n)
or, c2 = r2 (2 - 2 cos (360/n))
so, a=r\sqrt(2-2cos(360/n))
Therefore,
r=a/\sqrt(2-2cos(360/n))
Below is the implementation of the above approach:
C++
// C++ Program to find the radius
// of the circumcircle of the given polygon
#include <bits/stdc++.h>
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
JavaScript
<script>
// javascript Program to find the radius
// of the circumcircle of the given polygon
// Function to find the radius
// of the circumcircle
function findRadiusOfcircumcircle(n , a)
{
// these cannot be negative
if (n < 0 || a < 0)
return -1;
// Radius of the circumcircle
var radius = (a / Math.sqrt(2 - (2 * Math.cos(360 / n))));
// Return the radius
return radius;
}
// Driver code
var n = 5, a = 6;
// Find the radius of the circumcircle
document.write( findRadiusOfcircumcircle(n, a).toFixed(5)) ;
// This code is contributed by shikhasingrajput
</script>
PHP
<?php
// PHP Program to find the radius
// of the circumcircle of the
// given polygon
// Function to find the radius
// of the circumcircle
function findRadiusOfcircumcircle($n, $a)
{
// these cannot be negative
if ($n < 0 || $a < 0)
return -1;
// Radius of the circumcircle
$radius = $a / sqrt(2 - (2 *
cos(360 / $n)));
// Return the radius
return $radius;
}
// Driver code
$n = 5;
$a = 6;
// Find the radius of the circumcircle
echo findRadiusOfcircumcircle($n, $a);
// This code is contributed by Anuj_67..
?>
Time Complexity : O(log(n)) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
Similar Reads
Program to find the Perimeter of a Regular Polygon Given the number of sides 'n' and the length of side 's' of a regular polygon, the task is to find out the Perimeter of this polygon. Examples: Input: n = 7, s = 10 Output: Perimeter : 70 Since the sides are 7, Hence the given polygon is Heptagon. Therefore. Perimeter = 7*10 = 70 Input: n = 5, s = 2
5 min read
Program to find the Interior and Exterior Angle of a Regular Polygon Given the number of sides n of a regular polygon. The task is to find out the Interior angle and Exterior angle of the polygon.Examples: Input : n = 6 Output : Interior angle: 120 Exterior angle: 60 Input : n = 10 Output: Interior angle: 144 Exterior angle: 36 Interior angle: The angle between two a
4 min read
Program to find Perimeter / Circumference of Square and Rectangle The circumference of a figure is the sum of all the side lengths. To calculate the circumference of square, length of one of the side is required as all sides are equal. To calculate the circumference of rectangle, length and breadth of rectangle is required. Circumference of a Square:  The circumf
5 min read
Program to find Area of Triangle inscribed in N-sided Regular Polygon Given the triangle inscribed in an N-sided regular polygon with given side length, formed using any 3 vertices of the polygon, the task is to find the area of this triangle. Examples: Input: N = 6, side = 10 Output: 129.904 Input: N = 8, side = 5 Output: 45.2665 Approach: Consider the 1st example: G
6 min read
Program to find Circumcenter of a Triangle Given 3 non-collinear points in the 2D Plane P, Q and R with their respective x and y coordinates, find the circumcenter of the triangle.Note: Circumcenter of a triangle is the centre of the circle, formed by the three vertices of a triangle. Note that three points can uniquely determine a circle.Ex
13 min read