Área de un polígono regular de n lados con longitud de lado dada

Dado un polígono regular de N lados con longitud de lado a . La tarea es encontrar el área del polígono. 
Ejemplos: 
 

Input : N = 6, a = 9
Output : 210.444

Input : N = 7, a = 8
Output : 232.571

Planteamiento : En la figura de arriba, vemos que el polígono se puede dividir en N triángulos iguales. Mirando uno de los triángulos, vemos que todo el ángulo en el centro se puede dividir en = 360/N
Entonces, el ángulo t = 180/n 
Ahora, tan(t) = a/2*h
Entonces, h = a/ (2*tan(t))
Área de cada triángulo = (base * altura)/2 = a * a/(4*tan(t)) 
Entonces, área del polígono, 
 

A = n * (area of one triangle) = a2 * n/(4tan t)

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ Program to find the area of a regular
// polygon with given side length
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the area
// of a regular polygon
float polyarea(float n, float a)
{
    // Side and side length cannot be negative
    if (a < 0 && n < 0)
        return -1;
 
    // Area
    // degree converted to radians
    float A = (a * a * n) / (4 * tan((180 / n) * 3.14159 / 180));
 
    return A;
}
 
// Driver code
int main()
{
    float a = 9, n = 6;
 
    cout << polyarea(n, a) << endl;
 
    return 0;
}

Java

// Java  Program to find the area of a regular
// polygon with given side length
 
import java.io.*;
 
class GFG {
   
 
// Function to find the area
// of a regular polygon
static float polyarea(float n, float a)
{
    // Side and side length cannot be negative
    if (a < 0 && n < 0)
        return -1;
 
    // Area
    // degree converted to radians
    float A = (a * a * n) /(float) (4 * Math.tan((180 / n) * 3.14159 / 180));
 
    return A;
}
 
// Driver code
 
    public static void main (String[] args) {
    float a = 9, n = 6;
 
    System.out.println( polyarea(n, a));
    }
}
// This code is contributed by inder_verma..

Python3

# Python 3 Program to find the area
# of a regular polygon with given
# side length
from math import tan
 
# Function to find the area of a
# regular polygon
def polyarea(n, a):
     
    # Side and side length cannot
    # be negative
    if (a < 0 and n < 0):
        return -1
 
    # Area degree converted to radians
    A = (a * a * n) / (4 * tan((180 / n) *
                      3.14159 / 180))
 
    return A
 
# Driver code
if __name__ == '__main__':
    a = 9
    n = 6
 
    print('{0:.6}'.format(polyarea(n, a)))
 
# This code is contributed by
# Shashank_sharma

C#

// C# Program to find the area of a regular
// polygon with given side length
using System;
 
class GFG
{
 
// Function to find the area
// of a regular polygon
static float polyarea(float n, float a)
{
    // Side and side length cannot be negative
    if (a < 0 && n < 0)
        return -1;
 
    // Area
    // degree converted to radians
    float A = (a * a * n) / (float)(4 * Math.Tan((180 / n) *
                                           3.14159 / 180));
 
    return A;
}
 
// Driver code
public static void Main ()
{
    float a = 9, n = 6;
     
    Console.WriteLine(polyarea(n, a));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP Program to find the area of a regular
// polygon with given side length
 
// Function to find the area
// of a regular polygon
function polyarea($n, $a)
{
    // Side and side length cannot
    // be negative
    if ($a < 0 && $n < 0)
        return -1;
 
    // Area
    // degree converted to radians
    $A = ($a * $a * $n) / (4 * tan((180 / $n) *
                              3.14159 / 180));
 
    return $A;
}
 
// Driver code
$a = 9 ;
$n = 6 ;
 
echo round(polyarea($n, $a), 3);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
// javascript  Program to find the area of a regular
// polygon with given side length
 
// Function to find the area
// of a regular polygon
function polyarea(n , a)
{
 
    // Side and side length cannot be negative
    if (a < 0 && n < 0)
        return -1;
 
    // Area
    // degree converted to radians
    var A = (a * a * n) / (4 * Math.tan((180 / n) * 3.14159 / 180));
 
    return A;
}
 
// Driver code
var a = 9, n = 6;
document.write( polyarea(n, a).toFixed(5));
 
// This code contributed by Princi Singh
</script>
Producción: 

210.444

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *