How can I get sin, cos, and tan to use degrees instead of radians?

后端 未结 5 2144
误落风尘
误落风尘 2020-11-29 00:13

When I\'m working with math in JS I would like its trig functions to use degree values instead of radian values. How would I do that?

5条回答
  •  心在旅途
    2020-11-29 00:59

    You can use a function like this to do the conversion:

    function toDegrees (angle) {
      return angle * (180 / Math.PI);
    }
    

    Note that functions like sin, cos, and so on do not return angles, they take angles as input. It seems to me that it would be more useful to you to have a function that converts a degree input to radians, like this:

    function toRadians (angle) {
      return angle * (Math.PI / 180);
    }
    

    which you could use to do something like tan(toRadians(45)).

提交回复
热议问题