Check if right triangle possible from given area and hypotenuse - GeeksforGeeks
Given area and hypotenuse, the aim is to print all sides if right triangle can exist, else print -1. We need to print all sides in ascending order.
Read full article from Check if right triangle possible from given area and hypotenuse - GeeksforGeeks
Given area and hypotenuse, the aim is to print all sides if right triangle can exist, else print -1. We need to print all sides in ascending order.
In this post, a new solution with below logic is discussed.
Let the two unknown sides be a and b
Area : A = 0.5 * a * b
Hypotenuse Square : H^2 = a^2 + b^2
Substituting b, we get H2 = a2 + (4 * A2)/a2
On re-arranging, we get the equation a4 – (H2)(a2) + 4*(A2)
Area : A = 0.5 * a * b
Hypotenuse Square : H^2 = a^2 + b^2
Substituting b, we get H2 = a2 + (4 * A2)/a2
On re-arranging, we get the equation a4 – (H2)(a2) + 4*(A2)
The discriminant D of this equation would be D = H4 – 16*(A2)
If D = 0, then roots are given by the linear equation formula, roots = (-b +- sqrt(D) )/2*a
these roots would be equal to the square of the sides, finding the square roots would give us the sides.
If D = 0, then roots are given by the linear equation formula, roots = (-b +- sqrt(D) )/2*a
these roots would be equal to the square of the sides, finding the square roots would give us the sides.
def
findRightAngle(A, H):
# Descriminant of the equation
D
=
pow
(H,
4
)
-
16
*
A
*
A
if
D >
=
0
:
# applying the linear equation
# formula to find both the roots
root1
=
(H
*
H
+
sqrt(D))
/
2
root2
=
(H
*
H
-
sqrt(D))
/
2
a
=
sqrt(root1)
b
=
sqrt(root2)
if
b >
=
a:
print
a, b, H
else
:
print
b, a, H
else
:
print
"-1"
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
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, Base2 + Height2 = H2 For maximum area both base and height should be equal, b2 + b2 = H2 b = sqrt(H2/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.
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.
// 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 possible\n"
;
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;
}