How To Find Sides Of Right Triangle Given Hypotenuse
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.
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
#define eps 1e-6
double
getArea(
double
base,
double
hypotenuse)
{
double
height =
sqrt
(hypotenuse*hypotenuse - base*base);
return
0.5 * base * height;
}
void
printRightAngleTriangle(
int
hypotenuse,
int
area)
{
int
hsquare = hypotenuse * hypotenuse;
double
sideForMaxArea =
sqrt
(hsquare / 2.0);
double
maxArea = getArea(sideForMaxArea, hypotenuse);
if
(area > maxArea)
{
cout <<
"Not possiblen"
;
return
;
}
double
low = 0.0;
double
high = sideForMaxArea;
double
base;
while
(
abs
(high - low) > eps)
{
base = (low + high) / 2.0;
if
(getArea(base, hypotenuse) >= area)
high = base;
else
low = base;
}
double
height =
sqrt
(hsquare - base*base);
cout << base <<
" "
<< height << endl;
}
int
main()
{
int
hypotenuse = 5;
int
area = 6;
printRightAngleTriangle(hypotenuse, area);
return
0;
}
Java
public
class
GFG {
final
static
double
eps = (
double
) 1e-
6
;
static
double
getArea(
double
base,
double
hypotenuse) {
double
height = Math.sqrt(hypotenuse * hypotenuse - base * base);
return
0.5
* base * height;
}
static
void
printRightAngleTriangle(
int
hypotenuse,
int
area) {
int
hsquare = hypotenuse * hypotenuse;
double
sideForMaxArea = Math.sqrt(hsquare /
2.0
);
double
maxArea = getArea(sideForMaxArea, hypotenuse);
if
(area > maxArea) {
System.out.print(
"Not possible"
);
return
;
}
double
low =
0.0
;
double
high = sideForMaxArea;
double
base =
0
;
while
(Math.abs(high - low) > eps) {
base = (low + high) /
2.0
;
if
(getArea(base, hypotenuse) >= area) {
high = base;
}
else
{
low = base;
}
}
double
height = Math.sqrt(hsquare - base * base);
System.out.println(Math.round(base) +
" "
+ Math.round(height));
}
static
public
void
main(String[] args) {
int
hypotenuse =
5
;
int
area =
6
;
printRightAngleTriangle(hypotenuse, area);
}
}
Python3
import
math
def
getArea(base, hypotenuse):
height
=
math.sqrt(hypotenuse
*
hypotenuse
-
base
*
base);
return
0.5
*
base
*
height
def
printRightAngleTriangle(hypotenuse, area):
hsquare
=
hypotenuse
*
hypotenuse
sideForMaxArea
=
math.sqrt(hsquare
/
2.0
)
maxArea
=
getArea(sideForMaxArea, hypotenuse)
if
(area > maxArea):
print
(
"Not possiblen"
)
return
low
=
0.0
high
=
sideForMaxArea
while
(
abs
(high
-
low) >
1e
-
6
):
base
=
(low
+
high)
/
2.0
if
(getArea(base, hypotenuse) >
=
area):
high
=
base
else
:
low
=
base
height
=
math.ceil(math.sqrt(hsquare
-
base
*
base))
base
=
math.floor(base)
print
(base,height)
if
__name__
=
=
'__main__'
:
hypotenuse
=
5
area
=
6
printRightAngleTriangle(hypotenuse, area)
C#
using
System;
public
class
GFG{
static
double
eps = (
double
) 1e-6;
static
double
getArea(
double
base1,
double
hypotenuse) {
double
height = Math.Sqrt(hypotenuse * hypotenuse - base1 * base1);
return
0.5 * base1 * height;
}
static
void
printRightAngleTriangle(
int
hypotenuse,
int
area) {
int
hsquare = hypotenuse * hypotenuse;
double
sideForMaxArea = Math.Sqrt(hsquare / 2.0);
double
maxArea = getArea(sideForMaxArea, hypotenuse);
if
(area > maxArea) {
Console.Write(
"Not possible"
);
return
;
}
double
low = 0.0;
double
high = sideForMaxArea;
double
base1 = 0;
while
(Math.Abs(high - low) > eps) {
base1 = (low + high) / 2.0;
if
(getArea(base1, hypotenuse) >= area) {
high = base1;
}
else
{
low = base1;
}
}
double
height = Math.Sqrt(hsquare - base1 * base1);
Console.WriteLine(Math.Round(base1) +
" "
+ Math.Round(height));
}
static
public
void
Main() {
int
hypotenuse = 5;
int
area = 6;
printRightAngleTriangle(hypotenuse, area);
}
}
PHP
<?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
)),
"\n"
;
}
$hypotenuse
= 5;
$area
= 6;
printRightAngleTriangle(
$hypotenuse
,
$area
);
?>
Javascript
<script>
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(
"Not possible"
);
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);
</script>
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.
How To Find Sides Of Right Triangle Given Hypotenuse
Source: https://www.geeksforgeeks.org/find-sides-right-angled-triangle-given-hypotenuse-area/
Posted by: cowleslingthe.blogspot.com
0 Response to "How To Find Sides Of Right Triangle Given Hypotenuse"
Post a Comment