Area of the biggest ellipse inscribed within a rectangle
Given here is a rectangle of length l & breadth b, the task is to find the area of the biggest ellipse that can be inscribed within it.
Examples:
Input: l = 5, b = 3
Output: 11.775
Input: 7, b = 4
Output: 21.98
Approach:
- Let, the length of the major axis of the ellipse = 2x and the length of the minor axis of the ellipse = 2y
- From the diagram, it is very clear that,
2x = l
2y = b
- So, Area of the ellipse = (? * x * y) = (? * l * b) / 4
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float ellipse( float l, float b)
{
if (l < 0 || b < 0)
return -1;
float x = (3.14 * l * b) / 4;
return x;
}
int main()
{
float l = 5, b = 3;
cout << ellipse(l, b) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static float ellipse( float l, float b)
{
if (l < 0 || b < 0 )
return - 1 ;
float x = ( float )( 3.14 * l * b) / 4 ;
return x;
}
public static void main(String args[])
{
float a = 5 , b = 3 ;
System.out.println(ellipse(a, b));
}
}
|
Python3
def ellipse(l, b):
if l < 0 or b < 0 :
return - 1
x = ( 3.14 * l * b) / 4
return x
if __name__ = = "__main__" :
l, b = 5 , 3
print (ellipse(l, b))
|
C#
using System;
class GFG
{
static float ellipse( float l, float b)
{
if (l < 0 || b < 0)
return -1;
float x = ( float )(3.14 * l * b) / 4;
return x;
}
public static void Main()
{
float a = 5, b = 3;
Console.WriteLine(ellipse(a, b));
}
}
|
PHP
<?php
function ellipse( $l , $b )
{
if ( $l < 0 || $b < 0)
return -1;
$x = (3.14 * $l * $b ) / 4;
return $x ;
}
$l = 5; $b = 3;
echo ellipse( $l , $b ) . "\n" ;
?>
|
Javascript
<script>
function ellipse(l , b)
{
if (l < 0 || b < 0)
return -1;
var x = (3.14 * l * b) / 4;
return x;
}
var a = 5, b = 3;
document.write(ellipse(a, b));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...