Dado un polígono regular de n lados de longitud de lado a . La tarea es encontrar la longitud de su diagonal.
Ejemplos:
Input: a = 9, n = 10 Output: 17.119 Input: a = 4, n = 5 Output: 6.47213
Enfoque :
Sabemos que la suma de los ángulos interiores de un polígono = (n – 2) * 180 donde, n es el no. de lados en el polígono.
Entonces, cada ángulo interior = (n – 2) * 180/n
Ahora, tenemos que encontrar BC = 2 * x . Si dibujamos una perpendicular AO en BC , veremos que la perpendicular biseca a BC en BO y OC , ya que los triángulos AOB y AOC son congruentes entre sí.
Ahora, t = (n – 2) * 180/2n
Entonces, sint = x/a
Por lo tanto, x = asint
Por lo tanto, diagonal=2x = 2asint = 2asen((n – 2) * 180/2n)
C++
// C++ Program to find the diagonal // of a regular polygon with given side length #include <bits/stdc++.h> using namespace std; // Function to find the diagonal // of a regular polygon float polydiagonal(float n, float a) { // Side and side length cannot be negative if (a < 0 && n < 0) return -1; // diagonal // degree converted to radians return 2 * a * sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180); } // Driver code int main() { float a = 9, n = 10; cout << polydiagonal(n, a) << endl; return 0; }
Java
// Java Program to find the diagonal // of a regular polygon with given side length class GFG { // Function to find the diagonal // of a regular polygon static float polydiagonal(float n, float a) { // Side and side length cannot be negative if (a < 0 && n < 0) { return -1; } // diagonal // degree converted to radians return (float) (2 * a * Math.sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180)); } // Driver code public static void main(String[] args) { float a = 9, n = 10; System.out.printf("%.3f",polydiagonal(n, a)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 Program to find the diagonal # of a regular polygon with given side length import math as mt # Function to find the diagonal # of a regular polygon def polydiagonal(n, a): # Side and side length cannot # be negative if (a < 0 and n < 0): return -1 # diagonal degree converted to radians return (2 * a * mt.sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180)) # Driver code a, n = 9, 10 print(polydiagonal(n, a)) # This code is contributed # by Mohit kumar 29
C#
// C# Program to find the diagonal // of a regular polygon with given side length using System; public class GFG{ // Function to find the diagonal // of a regular polygon static float polydiagonal(float n, float a) { // Side and side length cannot be negative if (a < 0 && n < 0) { return -1; } // diagonal // degree converted to radians return (float) (2 * a * Math.Sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180)); } // Driver code static public void Main (){ float a = 9, n = 10; Console.WriteLine(polydiagonal(n, a)); } } // This code is contributed by @Sachin...
PHP
<?php // PHP Program to find the diagonal of a // regular polygon with given side length // Function to find the diagonal // of a regular polygon function polydiagonal ($n, $a) { // Side and side length cannot // be negative if ($a < 0 && $n < 0) return -1; // diagonal // degree converted to radians return 2 * $a * sin(((($n - 2) * 180) / (2 * $n)) * 3.14159 / 180); } // Driver code $a = 9; $n = 10; echo polydiagonal($n, $a); // This code is contributed // by Sach_Code ?>
Javascript
<script> // javascript Program to find the diagonal // of a regular polygon with given side length // Function to find the diagonal // of a regular polygon function polydiagonal(n , a) { // Side and side length cannot be negative if (a < 0 && n < 0) { return -1; } // diagonal // degree converted to radians return (2 * a * Math.sin((((n - 2) * 180) / (2 * n)) * 3.14159 / 180)); } // Driver code var a = 9, n = 10; document.write(polydiagonal(n, a).toFixed(3)); // This code contributed by Princi Singh </script>
17.119
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