Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver Code
let eps = 1e-6 ; function getArea ( base , hypotenuse ) { let height = Math . sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } function printRightAngleTriangle ( hypotenuse , area ) { let hsquare = hypotenuse * hypotenuse ; let sideForMaxArea = Math . sqrt ( hsquare / 2.0 ) ; let maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { document . write ( " " ) ; return ; } let low = 0.0 ; let high = sideForMaxArea ; let base = 0 ; while ( Math . abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) { high = base ; } else { low = base ; } } let height = Math . sqrt ( hsquare - base * base ) ; document . write ( Math . round ( base ) + " " + Math . round ( height ) ) ; } let hypotenuse = 5 ; let area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ;