Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
Last Updated :
09 Jun, 2022
Given here is a semicircle of radius r, which inscribes a rectangle which in turn inscribes an ellipse. The task is to find the area of this largest ellipse.
Examples:
Input: r = 5
Output: 19.625
Input: r = 11
Output: 94.985
Approach:
- Let the, length of the rectangle = l and breadth of the rectangle = b
- Let, the length of the major axis of the ellipse = 2x and, the length of the minor axis of the ellipse = 2y
- As we know, length and breadth of the largest rectangle inside a semicircle are r/√2 and √2r(Please refer here)
- Also, Area of the ellipse within the rectangle = (Ï€*l*b)/4 = (Ï€r^2/4)
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float ellipsearea( float r)
{
if (r < 0)
return -1;
float a = (3.14 * r * r) / 4;
return a;
}
int main()
{
float r = 5;
cout << ellipsearea(r) << endl;
return 0;
}
|
Java
class GFG
{
static float ellipsearea( float r)
{
if (r < 0 )
return - 1 ;
float a = ( float )(( 3 .14f * r * r) / 4 );
return a;
}
public static void main(String[] args)
{
float r = 5 ;
System.out.println(ellipsearea(r));
}
}
|
Python3
def ellipsearea(r) :
if (r < 0 ) :
return - 1 ;
a = ( 3.14 * r * r) / 4 ;
return a;
if __name__ = = "__main__" :
r = 5 ;
print (ellipsearea(r));
|
C#
using System;
class GFG
{
static float ellipsearea( float r)
{
if (r < 0)
return -1;
float a = ( float )((3.14 * r * r) / 4);
return a;
}
public static void Main()
{
float r = 5;
Console.WriteLine(ellipsearea(r));
}
}
|
PHP
<?php
function ellipsearea( $r )
{
if ( $r < 0)
return -1;
$a = (3.14 * $r * $r ) / 4;
return $a ;
}
$r = 5;
echo ellipsearea( $r ) . "\n" ;
?>
|
Javascript
<script>
function ellipsearea(r)
{
if (r < 0)
return -1;
var a = ((3.14 * r * r) / 4);
return a;
}
var r = 5;
document.write(ellipsearea(r));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...