最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calculate sides of a right angle triangle with JavaScript? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jul 12, 2016 at 22:54 dbj44dbj44 1,9983 gold badges26 silver badges49 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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

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.

发布评论

评论列表(0)

  1. 暂无评论