Horje
Largest trapezoid that can be inscribed in a semicircle

Given a semicircle of radius r, the task is to find the largest trapezoid that can be inscribed in the semicircle, with base lying on the diameter.
Examples: 
 

Input: r = 5
Output: 32.476

Input: r = 8
Output: 83.1384

 

 

Approach: Let r be the radius of the semicircle, x be the lower edge of the trapezoid, and y the upper edge, & h be the height of the trapezoid. 
Now from the figure,
 

r^2 = h^2 + (y/2)^2
or, 4r^2 = 4h^2 + y^2
y^2 = 4r^2 – 4h^2
y = 2?(r^2 – h^2)
We know, Area of Trapezoid, A = (x + y)*h/2
So, A = hr + h?(r^2 – h^2)
taking the derivative of this area function with respect to h, (noting that r is a constant since we are given the semicircle of radius r to start with)
dA/dh = r + ?(r^2 – h^2) – h^2/?(r^2 – h^2)
To find the critical points we set the derivative equal to zero and solve for h, we get
h = ?3/2 * r
So, x = 2 * r & y = r 
So, A = (3 * ?3 * r^2)/4 
 

Below is the implementation of above approach
 

C++

<?php
// PHP Program to find the biggest
// trapezoid which can be inscribed
// within the semicircle
 
// Function to find the area
// of the biggest trapezoid
function trapezoidarea($r)
{
 
    // the radius cannot be negative
    if ($r < 0)
        return -1;
 
    // area of the trapezoid
    $a = (3 * sqrt(3) * pow($r, 2)) / 4;
 
    return $a;
}
 
// Driver code
$r = 5;
echo trapezoidarea($r)."\n";
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// javascript Program to find the biggest trapezoid
// which can be inscribed within the semicircle
 
// Function to find the area
// of the biggest trapezoid
function trapezoidarea(r)
{
 
    // the radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the trapezoid
    var a = (3 * Math.sqrt(3)
            * Math.pow(r, 2)) / 4;
 
    return a;
}
 
// Driver code
 
var r = 5;
document.write(trapezoidarea(r).toFixed(3));
 
// This code contributed by Princi Singh
 
</script>

Output: 

32.476

 

Time complexity: O(1)

Auxiliary space: O(1)




Reffered: https://www.geeksforgeeks.org


DSA

Related
Minimum Players required to win the game Minimum Players required to win the game
Area of a largest square fit in a right angle triangle Area of a largest square fit in a right angle triangle
Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
Mathematics | Renewal processes in probability Mathematics | Renewal processes in probability
Find a point such that sum of the Manhattan distances is minimized Find a point such that sum of the Manhattan distances is minimized

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9