Reputation: 1998
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
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
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