Area of the Largest square that can be inscribed in an ellipse
Given an ellipse, with major axis length 2a & 2b, the task is to find the area of the largest rectangle that can be inscribed in it.
Examples:
Input: a = 4, b = 2
Output: 1.25
Input: a = 5, b= 3
Output: 0.604444
Approach: If a square is inscribed in an ellipse, the distance from the centre of the square to any of its corners will be equal to the distance between the origin and the point on the upper right corner in the diagram below, where x=y
the equation of the ellipse is x^2/a^2 + y^2/b^2 = 1
If, x = y
then, x^2/a^2 + x^2/b^2 = 1
therefore, x = ?(a^2 + b^2)/ab
so, y = ?(a^2 + b^2)/ab
So Area, A = 4(a^2 + b^2)/a^2b^2
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float squarearea( float a, float b)
{
if (a < 0 || b < 0)
return -1;
float area = 4 * (( pow (a, 2) + pow (b, 2))
/ ( pow (a, 2) * pow (b, 2)));
return area;
}
int main()
{
float a = 4, b = 2;
cout << squarearea(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float squarearea( float a, float b)
{
if (a < 0 || b < 0 )
return - 1 ;
float area = 4 *( float ) ((Math.pow(a, 2 ) + Math.pow(b, 2 ))
/ (Math.pow(a, 2 ) * Math.pow(b, 2 )));
return area;
}
public static void main (String[] args) {
float a = 4 , b = 2 ;
System.out.println( squarearea(a, b));
}
}
|
Python 3
def squarearea( a, b):
if (a < 0 or b < 0 ):
return - 1
area = 4 * ((( pow (a, 2 ) + pow (b, 2 )) /
( pow (a, 2 ) * pow (b, 2 ))))
return area
if __name__ = = '__main__' :
a = 4
b = 2
print (squarearea(a, b))
|
C#
using System;
class GFG
{
static float squarearea( float a, float b)
{
if (a < 0 || b < 0)
return -1;
float area = 4 *( float ) ((Math.Pow(a, 2) +
Math.Pow(b, 2)) /
(Math.Pow(a, 2) *
Math.Pow(b, 2)));
return area;
}
public static void Main ()
{
float a = 4, b = 2;
Console.WriteLine( squarearea(a, b));
}
}
|
PHP
<?php
function squarearea( $a , $b )
{
if ( $a < 0 or $b < 0)
return -1;
$area = 4 * (((pow( $a , 2) + pow( $b , 2)) /
(pow( $a , 2) * pow( $b , 2))));
return $area ;
}
$a = 4;
$b = 2;
print (squarearea( $a , $b ));
?>
|
Javascript
<script>
function squarearea(a , b)
{
if (a < 0 || b < 0)
return -1;
var area = 4 *((Math.pow(a, 2) + Math.pow(b, 2))
/ (Math.pow(a, 2) * Math.pow(b, 2)));
return area;
}
var a = 4, b = 2;
document.write( squarearea(a, b));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...