Area of Largest rectangle that can be inscribed in an Ellipse - GeeksforGeeks

16 Mar, 2021
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 = 3
Output: 24

Input: a = 10, b = 8
Output: 160
 
 
Approach: Let the upper right corner of the rectangle has co-ordinates (x, y), Then the area of rectangle, A = 4*x*y.Now,  
Equation of ellipse, (x2/a2) + (y2/b2) = 1 Thinking of the area as a function of x, we have dA/dx = 4xdy/dx + 4yDifferentiating equation of ellipse with respect to x, we have 2x/a2 + (2y/b2)dy/dx = 0,so, dy/dx = -b2x/a2y, and dAdx = 4y – (4b2x2/a2y)Setting this to 0 and simplifying, we have y2 = b2x2/a2.From equation of ellipse we know that, y2=b2 – b2x2/a2Thus, y2=b2 – y2, 2y2=b2, and y2b2 = 1/2. Clearly, then, x2a2 = 1/2 as well, and the area is maximized when x= a/√2 and y=b/√2So the maximum area Area, Amax = 2ab  
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find the biggest rectangle// which can be inscribed within the ellipse#include <bits/stdc++.h>using namespace std; // Function to find the area// of the rectanglefloat rectanglearea(float a, float b){     // a and b cannot be negative    if (a < 0 || b < 0)        return -1;     // area of the rectangle    return 2 * a * b;} // Driver codeint main(){    float a = 10, b = 8;    cout << rectanglearea(a, b) << endl;    return 0;}
// Java Program to find the biggest rectangle// which can be inscribed within the ellipse import java.util.*;import java.lang.*;import java.io.*; class GFG{// Function to find the area// of the rectanglestatic float rectanglearea(float a, float b){      // a and b cannot be negative    if (a < 0 || b < 0)        return -1;      // area of the rectangle    return 2 * a * b;}  // Driver codepublic static void main(String args[]){    float a = 10, b = 8;    System.out.println(rectanglearea(a, b));}}
# Python 3 Program to find the biggest rectangle# which can be inscribed within the ellipse #  Function to find the area# of the rectangledef rectanglearea(a, b) :     # a and b cannot be negative    if a < 0 or b < 0 :        return -1     # area of the rectangle    return 2 * a * b   # Driver code    if __name__ == "__main__" :     a, b = 10, 8    print(rectanglearea(a, b))  # This code is contributed by ANKITRAI1
// C# Program to find the// biggest rectangle which// can be inscribed within// the ellipseusing System; class GFG{// Function to find the area// of the rectanglestatic float rectanglearea(float a,                           float b){     // a and b cannot be negative    if (a < 0 || b < 0)        return -1;     // area of the rectangle    return 2 * a * b;} // Driver codepublic static void Main(){    float a = 10, b = 8;    Console.WriteLine(rectanglearea(a, b));}} // This code is contributed// by inder_verma
<?php// PHP Program to find the biggest// rectangle which can be inscribed// within the ellipse // Function to find the area// of the rectanglefunction rectanglearea($a, $b){     // a and b cannot be negative    if ($a < 0 or $b < 0)        return -1;     // area of the rectangle    return 2 * $a * $b;} // Driver code$a = 10; $b = 8;echo rectanglearea($a, $b); // This code is contributed// by inder_verma?>
<script> // javascript Program to find the biggest rectangle// which can be inscribed within the ellipse // Function to find the area// of the rectanglefunction rectanglearea(a , b){      // a and b cannot be negative    if (a < 0 || b < 0)        return -1;      // area of the rectangle    return 2 * a * b;}  // Driver code var a = 10, b = 8;document.write(rectanglearea(a, b)); // This code contributed by Princi Singh </script>
160
 
tufan_gupta2000
ankthon
inderDuMCA
princi singh
square-rectangle
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Program for distance between two points on earth
Closest Pair of Points | O(nlogn) Implementation
Equation of circle when three points on the circle are given
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7