Apothem of a n-sided regular polygon - GeeksforGeeks

17 Mar, 2021
Given here the side length a of a regular n-sided polygon, the task is to find the length of its Apothem. Apothem is the line drawn from the center of the polygon that is perpendicular to one of its sides. Examples:  
Input a = 9, n = 6
Output: 7.79424

Input: a = 8, n = 7
Output: 8.30609
 
 
Approach:  
In the figure, we see the polygon can be divided into n equal triangles. Looking into one of the triangles, we see the whole angle at the centre can be divided into = 360/n So, angle t = 180/n now, tan t = a/2h So, h = a/(2*tan t) here, h is the apothem, so, apothem = a/(2*tan(180/n))
Below is the implementation of the above approach. 
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the apothem// of a regular polygon with given side length#include <bits/stdc++.h>using namespace std; // Function to find the apothem// of a regular polygonfloat 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 codeint main(){    float a = 9, n = 6;    cout << polyapothem(n, a) << endl;     return 0;}
// Java Program to find the apothem of a// regular polygon with given side lengthimport 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 codepublic 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
# Python 3 Program to find the apothem# of a regular polygon with given side# lengthfrom math import tan # Function to find the apothem# of a regular polygondef 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 codeif __name__ == '__main__':    a = 9    n = 6    print('{0:.6}'.format(polyapothem(n, a)))     # This code is contributed by# Sahil_Shelangia
// C# Program to find the apothem of a// regular polygon with given side lengthusing System; class GFG{ // Function to find the apothem// of a regular polygonstatic 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 codepublic static void Main(){    double a = 9, n = 6;    Console.WriteLine(Math.Round(polyapothem(n, a), 4));}} // This code is contributed by Ryuga
<?php// PHP Program to find the apothem of a// regular polygon with given side length // Function to find the apothem// of a regular polygonfunction polyapothem($n, $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$a = 9; $n = 6;echo polyapothem($n, $a) . "\n"; // This code is contributed// by Akanksha Rai?>
<script>// javascript Program to find the apothem of a// regular polygon with given side length // Function to find the apothem// of a regular polygonfunction polyapothem(n , 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 var a = 9, n = 6; document.write(polyapothem(n, a).toFixed(5));  // This code contributed by Princi Singh</script>
7.79424
 
sahilshelangia
Akanksha_Rai
Shivi_Aggarwal
ankthon
amit143katiyar
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull | Set 2 (Graham Scan)
Check whether a given point lies inside a triangle or not
Convex Hull using Divide and Conquer Algorithm
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7