Largest triangle that can be inscribed in an ellipse
Last Updated :
23 Jul, 2022
Given an ellipse, with major axis length 2a & 2b, the task is to find the area of the largest triangle that can be inscribed in it.
Examples:
Input: a = 4, b = 2
Output: 10.3923
Input: a = 5, b = 3
Output: 10.8253
Approach: So we know the ellipse is just the scaled shadow of a circle.Let’s find the scaling factor.
x^2/a^2 + y^2/b^2 = 1 is an ellipse. Rewrite this as:
(y*(a/b))^2+x^2 = a^2
This is just a vertically scaled down circle of radius a (think light falls from the top at an angle), and the vertical factor is a/b. The biggest triangle in the ellipse is then a scaled up version of the biggest triangle in the circle. Using a little geometry and taking symmetry into account, we can understand that the biggest such triangle is the equilateral one. It’s sides will be ?3a and the area will be (3?3)a^2/4
Translating this to ellipse terms – we scale the horizontal dimension up by a factor a/b, and the area of the biggest triangle in the ellipse is,
A = (3?3)a^2/4b
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float trianglearea( float a, float b)
{
if (a < 0 || b < 0)
return -1;
float area = (3 * sqrt (3) * pow (a, 2)) / (4 * b);
return area;
}
int main()
{
float a = 4, b = 2;
cout << trianglearea(a, b) << endl;
return 0;
}
|
Java
public class GFG {
static float trianglearea( float a, float b)
{
if (a < 0 || b < 0 )
return - 1 ;
float area = ( float )( 3 * Math.sqrt( 3 ) * Math.pow(a, 2 )) / ( 4 * b);
return area;
}
public static void main(String[] args) {
float a = 4 , b = 2 ;
System.out.println(trianglearea(a, b));
}
}
|
Python3
from math import *
def trianglearea(a, b) :
if a < 0 or b < 0 :
return - 1
area = ( 3 * sqrt( 3 ) * pow (a, 2 )) / ( 4 * b)
return area
if __name__ = = "__main__" :
a, b = 4 , 2
print ( round (trianglearea(a, b), 4 ))
|
C#
using System;
class GFG
{
static float trianglearea( float a, float b)
{
if (a < 0 || b < 0)
return -1;
float area = ( float )(3 * Math.Sqrt(3) *
Math.Pow(a, 2)) / (4 * b);
return area;
}
public static void Main()
{
float a = 4, b = 2;
Console.WriteLine(trianglearea(a, b));
}
}
|
PHP
<?php
function trianglearea( $a , $b )
{
if ( $a < 0 || $b < 0)
return -1;
$area = (3 * sqrt(3) *
pow( $a , 2)) / (4 * $b );
return $area ;
}
$a = 4;
$b = 2;
echo trianglearea( $a , $b );
?>
|
Javascript
<script>
function trianglearea(a , b)
{
if (a < 0 || b < 0)
return -1;
var area = (3 * Math.sqrt(3) * Math.pow(a, 2)) / (4 * b);
return area;
}
var a = 4, b = 2;
document.write(trianglearea(a, b).toFixed(4));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...