dbj44
dbj44

Reputation: 1998

Calculate sides of a right angle triangle with JavaScript?

enter image description here

If I did not know that side AB was 489.84 or that side BC was 12.66, how could I calculate these two lengths with JavaScript given I had all the other information?

Upvotes: 4

Views: 3685

Answers (2)

ManoDestra
ManoDestra

Reputation: 6473

Sin(angle) = opposite / hypotenuse

So

opposite = Sin(angle) * hypotenuse

Therefore...

<script>
var angle = 88.52;
var angleInRadians = angle * Math.PI / 180;
var hypotenuse = 490;
var opposite = Math.sin(angleInRadians) * hypotenuse;
console.log('Opposite: ' + opposite);
console.log('Opposite (to 2 decimal places): ' + opposite.toFixed(2));
</script>

You can get the equivalent for the bottom value by using Math.cos instead of Math.sin, of course.

Upvotes: 1

le_m
le_m

Reputation: 20228

Use the Math.sin and Math.cos functions. Note: these functions accept radians, you would thus need to convert degrees by using rad = deg * Math.PI/180:

Math.cos(88.52 * Math.PI/180) * 490; // 12.655720238100102
Math.sin(88.52 * Math.PI/180) * 490; // 489.83653676022874

Upvotes: 9

Related Questions