Dado un polígono regular de N lados con radio (distancia del centro a cualquier vértice) R . La tarea es encontrar el área del polígono.
Ejemplos:
Input : r = 9, N = 6 Output : 210.444 Input : r = 8, N = 7 Output : 232.571
En la figura 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 partes.
Entonces, el ángulo t = 180/N .
Mirando dentro de uno de los triángulos, vemos,
h = rcost a = rsint
Sabemos,
area of the triangle = (base * height)/2 = r2sin(t)cos(t) = r2*sin(2t)/2
Entonces, área del polígono:
A = n * (area of one triangle) = n*r2*sin(2t)/2 = n*r2*sin(360/n)/2
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to find the area // of a regular polygon with given radius #include <bits/stdc++.h> using namespace std; // Function to find the area // of a regular polygon float polyarea(float n, float r) { // Side and radius cannot be negative if (r < 0 && n < 0) return -1; // Area // degree converted to radians float A = ((r * r * n) * sin((360 / n) * 3.14159 / 180)) / 2; return A; } // Driver code int main() { float r = 9, n = 6; cout << polyarea(n, r) << endl; return 0; }
Java
// Java Program to find the area // of a regular polygon with given radius import java.util.*; class GFG { // Function to find the area // of a regular polygon static double polyarea(double n, double r) { // Side and radius cannot be negative if (r < 0 && n < 0) return -1; // Area // degree converted to radians double A = ((r * r * n) * Math.sin((360 / n) * 3.14159 / 180)) / 2; return A; } // Driver code public static void main(String []args) { float r = 9, n = 6; System.out.println(polyarea(n, r)); } } // This code is contributed // By ihritik (Hritik Raj)
Python3
# Python3 Program to find the area # of a regular polygon with given radius # form math lib import sin function from math import sin # Function to find the area # of a regular polygon def polyarea(n, r) : # Side and radius cannot be negative if (r < 0 and n < 0) : return -1 # Area # degree converted to radians A = (((r * r * n) * sin((360 / n) * 3.14159 / 180)) / 2); return round(A, 3) # Driver code if __name__ == "__main__" : r, n = 9, 6 print(polyarea(n, r)) # This code is contributed by Ryuga
C#
// C# Program to find the area // of a regular polygon with given radius using System; class GFG { // Function to find the area // of a regular polygon static double polyarea(double n, double r) { // Side and radius cannot be negative if (r < 0 && n < 0) return -1; // Area // degree converted to radians double A = ((r * r * n) * Math.Sin((360 / n) * 3.14159 / 180)) / 2; return A; } // Driver code public static void Main() { float r = 9, n = 6; Console.WriteLine(polyarea(n, r)); } } // This code is contributed // By ihritik (Hritik Raj)
PHP
<?php // PHP Program to find the area of a // regular polygon with given radius // Function to find the area // of a regular polygon function polyarea($n, $r) { // Side and radius cannot be negative if ($r < 0 && $n < 0) return -1; // Area // degree converted to radians $A = (($r * $r * $n) * sin((360 / $n) * 3.14159 / 180)) / 2; return $A; } // Driver code $r = 9; $n = 6; echo polyarea($n, $r)."\n"; // This code is contributed by ita_c ?>
Javascript
<script> // javascript Program to find the area // of a regular polygon with given radius // Function to find the area // of a regular polygon function polyarea(n , r) { // Side and radius cannot be negative if (r < 0 && n < 0) return -1; // Area // degree converted to radians var A = ((r * r * n) * Math.sin((360 / n) * 3.14159 / 180)) / 2; return A; } // Driver code var r = 9, n = 6; document.write(polyarea(n, r).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