Side of a regular n-sided polygon circumscribed in a circle

Given two integers r and n where n is the number of sides of a regular polygon and r is the radius of the circle this polygon is circumscribed in. The task is to find the length of the side of polygon.Â
Â
Examples:Â
Â
Input: n = 5, r = 11Â
Output: 12.9256
Input: n = 3, r = 5Â
Output: 8.6576Â
Approach: Consider the image above and let angle AOB be theta then theta = 360 / n.Â
In right angled triangle , angle ACO = 90 degrees and angle AOC = theta / 2.Â
So, AC = OA * sin(theta / 2) = r * sin(theta / 2)Â
Therefore, side of the polygon, AB = 2 * AC i.e. 2 * r * sin(theta / 2).
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to calculate the side of the polygon// circumscribed in a circlefloat 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 Codeint 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 approachimport java.lang.Math;import java.io.*;Â
class GFG {     // Function to calculate the side of the polygon// circumscribed in a circlestatic 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 approachfrom math import sinÂ
# Function to calculate the side of # the polygon circumscribed in a circledef calculateSide(n, r):    theta = 360 / n    theta_in_radians = theta * 3.14 / 180Â
    return 2 * r * sin(theta_in_radians / 2)Â
# Driver Codeif __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
<?php// PHP implementation of the approachÂ
// Function to calculate the side of the // polygon circumscribed in a circlefunction calculateSide($n, $r){Â Â Â Â $theta; $theta_in_radians;Â
    $theta = 360 / $n;    $theta_in_radians = $theta * 3.14 / 180;Â
    return 2 * $r * sin($theta_in_radians / 2);}Â
// Driver CodeÂ
// Total sides of the polygon$n = 3;Â
// Radius of the circumscribing circle$r = 5;Â
echo calculateSide($n, $r);Â
// This code is contributed by inder_verma..?> |
Javascript
<script>Â
// javascript implementation of the approachÂ
    // Function to calculate the side of the polygon// circumscribed in a circlefunction calculateSide( n , r){    var 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Â
// Total sides of the polygonvar n = 3;Â
// Radius of the circumscribing circlevar r = 5;document.write(calculateSide(n, r).toFixed(5));Â
// This code contributed by Princi Singh Â
</script> |
8.6576
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




