Dada una elipse, con ejes mayores de longitud 2a y 2b , la tarea es encontrar el área del rectángulo más grande que se puede inscribir en ella.
Ejemplos:
Input: a = 4, b = 2 Output: 1.25 Input: a = 5, b= 3 Output: 0.604444
Aproximación : Si un cuadrado está inscrito en una elipse, la distancia desde el centro del cuadrado a cualquiera de sus esquinas será igual a la distancia entre el origen y el punto en la esquina superior derecha en el diagrama a continuación, donde x=y
la ecuación de la elipse es x^2/a^2 + y^2/b^2 = 1
Si, x = y
entonces, x^2/a^2 + x^2/b^2 = 1
entonces, x = √(a^2 + b^2)/ab
so, y = √(a^2 + b^2)/ab
So Área, A = 4(a^2 + b^2)/a^2b^2
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to find the biggest square // which can be inscribed within the ellipse #include <bits/stdc++.h> using namespace std; // Function to find the area // of the square float squarearea(float a, float b) { // a and b cannot be negative if (a < 0 || b < 0) return -1; // area of the square float area = 4 * ((pow(a, 2) + pow(b, 2)) / (pow(a, 2) * pow(b, 2))); return area; } // Driver code int main() { float a = 4, b = 2; cout << squarearea(a, b) << endl; return 0; }
Java
// Java Program to find the biggest square // which can be inscribed within the ellipse import java.io.*; class GFG { // Function to find the area // of the square static float squarearea(float a, float b) { // a and b cannot be negative if (a < 0 || b < 0) return -1; // area of the square float area = 4 *(float) ((Math.pow(a, 2) + Math.pow(b, 2)) / (Math.pow(a, 2) * Math.pow(b, 2))); return area; } // Driver code public static void main (String[] args) { float a = 4, b = 2; System.out.println( squarearea(a, b)); } } // This code is contributed by inder_verma.
Python 3
# Python3 Program to find the biggest square # which can be inscribed within the ellipse # Function to find the area # of the square def squarearea( a, b): # a and b cannot be negative if (a < 0 or b < 0): return -1 # area of the square area = 4 * (((pow(a, 2) + pow(b, 2)) / (pow(a, 2) * pow(b, 2)))) return area # Driver code if __name__=='__main__': a = 4 b = 2 print(squarearea(a, b)) # This code is contributed by ash264
C#
// C# Program to find the biggest // square which can be inscribed // within the ellipse using System; class GFG { // Function to find the area // of the square static float squarearea(float a, float b) { // a and b cannot be negative if (a < 0 || b < 0) return -1; // area of the square float area = 4 *(float) ((Math.Pow(a, 2) + Math.Pow(b, 2)) / (Math.Pow(a, 2) * Math.Pow(b, 2))); return area; } // Driver code public static void Main () { float a = 4, b = 2; Console.WriteLine( squarearea(a, b)); } } // This code is contributed by inder_verma
PHP
<?php // PHP Program to find the biggest square // which can be inscribed within the ellipse // Function to find the area // of the square function squarearea( $a, $b) { // a and b cannot be negative if ($a < 0 or $b < 0) return -1; // area of the square $area = 4 * (((pow($a, 2) + pow($b, 2)) / (pow($a, 2) * pow($b, 2)))); return $area; } // Driver code $a = 4; $b = 2; print(squarearea($a, $b)); // This code is contributed by mits ?>
Javascript
<script> // javascript Program to find the biggest square // which can be inscribed within the ellipse // Function to find the area // of the square function squarearea(a , b) { // a and b cannot be negative if (a < 0 || b < 0) return -1; // area of the square var area = 4 *((Math.pow(a, 2) + Math.pow(b, 2)) / (Math.pow(a, 2) * Math.pow(b, 2))); return area; } // Driver code var a = 4, b = 2; document.write( squarearea(a, b)); // This code contributed by shikhasingrajput </script>
1.25
Complejidad del 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