Given hypotenuse and area of a right angle triangle, get its base and height and if any triangle with given hypotenuse and area is not possible, print not possible.

Examples:

Input : hypotenuse = 5, area = 6 Output : base = 3, height = 4 Input : hypotenuse = 5, area = 7 Output : No triangle possible with above specification.

We can use a property of right angle triangle for solving this problem, which can be stated as follows,

A right angle triangle with fixed hypotenuse attains maximum area, when it is isosceles i.e. both height and base becomes equal so if hypotenuse if H, then by pythagorean theorem, Base^{2}+ Height^{2}= H^{2}For maximum area both base and height should be equal, b^{2}+ b^{2}= H^{2}b = sqrt(H^{2}/2) Above is the length of base at which triangle attains maximum area, given area must be less than this maximum area, otherwise no such triangle will possible.

Now if given area is less than this maximum area, we can do a binary search for length of base, as increasing base will increases area, it is a monotonically increasing function where binary search can be applied easily.

In below code, a method is written for getting area of right angle triangle, recall that for right angle triangle area is ½*base*height and height can be calculated from base and hypotenuse using pythagorean theorem.

// C++ program to get right angle triangle, given // hypotenuse and area of triangle #include <bits/stdc++.h> using namespace std; // limit for float comparison #define eps 1e-6 // Utility method to get area of right angle triangle, // given base and hypotenuse double getArea(double base, double hypotenuse) { double height = sqrt(hypotenuse*hypotenuse - base*base); return 0.5 * base * height; } // Prints base and height of triangle using hypotenuse // and area information void printRightAngleTriangle(int hypotenuse, int area) { int hsquare = hypotenuse * hypotenuse; // maximum area will be obtained when base and height // are equal (= sqrt(h*h/2)) double sideForMaxArea = sqrt(hsquare / 2.0); double maxArea = getArea(sideForMaxArea, hypotenuse); // if given area itself is larger than maxArea then no // solution is possible if (area > maxArea) { cout << "Not possiblen"; return; } double low = 0.0; double high = sideForMaxArea; double base; // binary search for base while (abs(high - low) > eps) { base = (low + high) / 2.0; if (getArea(base, hypotenuse) >= area) high = base; else low = base; } // get height by pythagorean rule double height = sqrt(hsquare - base*base); cout << base << " " << height << endl; } // Driver code to test above methods int main() { int hypotenuse = 5; int area = 6; printRightAngleTriangle(hypotenuse, area); return 0; }

Output:

3 4

One more solution is discussed in below post.

Check if right angles possible from given area and hypotenuse

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.