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
< ? php $ eps = .0000001 ; function getArea ( $ base , $ hypotenuse ) { $ height = sqrt ( $ hypotenuse * $ hypotenuse - $ base * $ base ) ; return 0.5 * $ base * $ height ; } function printRightAngleTriangle ( $ hypotenuse , $ area ) { global $ eps ; $ hsquare = $ hypotenuse * $ hypotenuse ; $ sideForMaxArea = sqrt ( $ hsquare / 2.0 ) ; $ maxArea = getArea ( $ sideForMaxArea , $ hypotenuse ) ; if ( $ area > $ maxArea ) { echo " Not ▁ possiblen " ; return ; } $ low = 0.0 ; $ high = $ sideForMaxArea ; $ base ; while ( abs ( $ high - $ low ) > $ eps ) { $ base = ( $ low + $ high ) / 2.0 ; if ( getArea ( $ base , $ hypotenuse ) >= $ area ) $ high = $ base ; else $ low = $ base ; } $ height = sqrt ( $ hsquare - $ base * $ base ) ; echo ( ceil ( $ base ) ) , " ▁ " , ( floor ( $ height ) ) , " STRNEWLINE " ; } $ hypotenuse = 5 ; $ area = 6 ; printRightAngleTriangle ( $ hypotenuse , $ area ) ; ? >