Program to find height of a Trapezoid
Given two integers a and b representing the length of the two bases, and the two integers p1 and p2 representing the non-parallel sides of a trapezoid, the task is to find the height of the trapezoid.
A Trapezoid is a convex quadrilateral with at least one pair of parallel sides. The parallel sides are called the bases of the trapezoid and the other two sides which are not parallel are referred to as the legs. There can also be a pair of bases.
In the figure, CD || AB. Therefore, they form the bases, and the other two sides i.e., AD and BC form the legs.
Examples:
Input: a = 14, b = 13, p1 = 25, p2 = 10
Output: 11.2
Input: a = 8, b = 16, p1 = 25, p2 = 10
Output: 7.92401
Approach: To find the height of the trapezium, construct perpendiculars DE and CF on the base AB of the trapezium as shown in the figure given below.
Now, the area of the triangle AED and area of triangle CFB can be calculated by using Heron’s Formula with sides p1, p2, and (b – a). Also, its area is equal to 0.5 * (b – a) * h.
From these two equations, calculate the value of h, which is the height of the trapezoid.
Heron’s Formula: Consider a triangle with sides a, b and c, then
Area of triangle, A = (s (s – a)(s – b)(s – c))1/2, where s = (a + b + c)/2
Also, A = 0.5 * base * height
After rearranging the terms, the height of the trapezoid = (A * 2) / Base
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findHeight( float p1, float p2,
float b, float c)
{
float a = max(p1, p2) - min(p1, p2);
float s = (a + b + c) / 2;
float area = sqrt (s * (s - a)
* (s - b) * (s - c));
float height = (area * 2) / a;
cout << "Height is: " << height;
}
int main()
{
float p1 = 25, p2 = 10;
float a = 14, b = 13;
findHeight(p1, p2, a, b);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findHeight( float p1, float p2,
float b, float c)
{
float a = Math.max(p1, p2) - Math.min(p1, p2);
float s = (a + b + c) / 2 ;
float area = ( int )Math.sqrt(s * (s - a)
* (s - b) * (s - c));
float height = (area * 2 ) / a;
System.out.print( "Height is: " + height);
}
public static void main(String args[])
{
float p1 = 25 , p2 = 10 ;
float a = 14 , b = 13 ;
findHeight(p1, p2, a, b);
}
}
|
Python3
import math
def findHeight(p1, p2, b, c) :
a = max (p1, p2) - min (p1, p2)
s = (a + b + c) / / 2
area = math.sqrt(s * (s - a)
* (s - b) * (s - c))
height = (area * 2 ) / a
print ( "Height is: " , height)
p1 = 25
p2 = 10
a = 14
b = 13
findHeight(p1, p2, a, b)
|
C#
using System;
public class GFG
{
static void findHeight( float p1, float p2,
float b, float c)
{
float a = Math.Max(p1, p2) - Math.Min(p1, p2);
float s = (a + b + c) / 2;
float area = ( int )Math.Sqrt(s * (s - a)
* (s - b) * (s - c));
float height = (area * 2) / a;
Console.Write( "Height is: " + height);
}
public static void Main(String []args)
{
float p1 = 25, p2 = 10;
float a = 14, b = 13;
findHeight(p1, p2, a, b);
}
}
|
Javascript
<script>
function findHeight(p1, p2, b, c)
{
a = Math.max(p1, p2) - Math.min(p1, p2)
let s = Math.floor((a + b + c)/2)
let area = Math.sqrt(s * (s - a)
* (s - b) * (s - c))
height = (area * 2) / a
document.write( "Height is: " , height)
}
let p1 = 25
let p2 = 10
let a = 14
let b = 13
findHeight(p1, p2, a, b)
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Last Updated :
20 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...