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.2Input: 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)/2Also, 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate height // of the trapezoid void findHeight( float p1, float p2, float b, float c) { float a = max(p1, p2) - min(p1, p2); // Apply Heron's formula float s = (a + b + c) / 2; // Calculate the area float area = sqrt (s * (s - a) * (s - b) * (s - c)); // Calculate height of trapezoid float height = (area * 2) / a; // Print the height cout << "Height is: " << height; } // Driver Code int main() { // Given a, b, p1 and p2 float p1 = 25, p2 = 10; float a = 14, b = 13; findHeight(p1, p2, a, b); return 0; } |
Java
// Java program to implement // the above approach import java.util.*; class GFG { // Function to calculate height // of the trapezoid static void findHeight( float p1, float p2, float b, float c) { float a = Math.max(p1, p2) - Math.min(p1, p2); // Apply Heron's formula float s = (a + b + c) / 2 ; // Calculate the area float area = ( int )Math.sqrt(s * (s - a) * (s - b) * (s - c)); // Calculate height of trapezoid float height = (area * 2 ) / a; // Print the height System.out.print( "Height is: " + height); } // Driver Code public static void main(String args[]) { // Given a, b, p1 and p2 float p1 = 25 , p2 = 10 ; float a = 14 , b = 13 ; findHeight(p1, p2, a, b); } } // This code is contributed by splevel62. |
Python3
# Python3 program to implement # the above approach import math # Function to calculate height # of the trapezoid def findHeight(p1, p2, b, c) : a = max (p1, p2) - min (p1, p2) # Apply Heron's formula s = (a + b + c) / / 2 # Calculate the area area = math.sqrt(s * (s - a) * (s - b) * (s - c)) # Calculate height of trapezoid height = (area * 2 ) / a # Print the height print ( "Height is: " , height) # Driver Code # Given a, b, p1 and p2 p1 = 25 p2 = 10 a = 14 b = 13 findHeight(p1, p2, a, b) # this code is contributed by sanjoy_62. |
C#
// C# program to implement // the above approach using System; public class GFG { // Function to calculate height // of the trapezoid static void findHeight( float p1, float p2, float b, float c) { float a = Math.Max(p1, p2) - Math.Min(p1, p2); // Apply Heron's formula float s = (a + b + c) / 2; // Calculate the area float area = ( int )Math.Sqrt(s * (s - a) * (s - b) * (s - c)); // Calculate height of trapezoid float height = (area * 2) / a; // Print the height Console.Write( "Height is: " + height); } // Driver Code public static void Main(String []args) { // Given a, b, p1 and p2 float p1 = 25, p2 = 10; float a = 14, b = 13; findHeight(p1, p2, a, b); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript program to implement // the above approach // Function to calculate height // of the trapezoid function findHeight(p1, p2, b, c) { a = Math.max(p1, p2) - Math.min(p1, p2) // Apply Heron's formula let s = Math.floor((a + b + c)/2) // Calculate the area let area = Math.sqrt(s * (s - a) * (s - b) * (s - c)) // Calculate height of trapezoid height = (area * 2) / a // Print the height document.write( "Height is: " , height) } // Driver Code // Given a, b, p1 and p2 let p1 = 25 let p2 = 10 let a = 14 let b = 13 findHeight(p1, p2, a, b) // This code is contributed by shinjanpatra </script> |
Height is: 11.2
Time Complexity: O(logn)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!