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?
当我在JS中使用数学时,我希望它的trig函数使用度数值而不是弧度值。我该怎么办?
5 个解决方案
#1
160
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:
请注意,像sin,cos等函数不返回角度,它们将角度视为输入。在我看来,拥有一个将度数输入转换为弧度的函数会更有用,如下所示:
function toRadians (angle) {
return angle * (Math.PI / 180);
}
which you could use to do something like tan(toRadians(45))
.
你可以用来做tan之类的东西(toRadians(45))。
#2
22
Multiply the result by 180/Math.PI
to convert from radians to degrees.
将结果乘以180 / Math.PI,将弧度转换为度数。
You could also define your own functions:
您还可以定义自己的功能:
function sinDegrees(angle) {return Math.sin(angle/180*Math.PI);};
and so on.
等等。
#3
13
I created my own little lazy Math-Object for degree (MathD), hope it helps:
我为学位(MathD)创建了自己的小懒惰Math-Object,希望它有所帮助:
//helper
/**
* converts degree to radians
* @param degree
* @returns {number}
*/
var toRadians = function (degree) {
return degree * (Math.PI / 180);
};
/**
* Converts radian to degree
* @param radians
* @returns {number}
*/
var toDegree = function (radians) {
return radians * (180 / Math.PI);
}
/**
* Rounds a number mathematical correct to the number of decimals
* @param number
* @param decimals (optional, default: 5)
* @returns {number}
*/
var roundNumber = function(number, decimals) {
decimals = decimals || 5;
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
//the object
var MathD = {
sin: function(number){
return roundNumber(Math.sin(toRadians(number)));
},
cos: function(number){
return roundNumber(Math.cos(toRadians(number)));
},
tan: function(number){
return roundNumber(Math.tan(toRadians(number)));
},
asin: function(number){
return roundNumber(toDegree(Math.asin(number)));
},
acos: function(number){
return roundNumber(toDegree(Math.acos(number)));
},
atan: function(number){
return roundNumber(toDegree(Math.atan(number)));
}
};
#4
0
Create your own conversion function that applies the needed math, and invoke those instead. http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees
创建自己的转换函数,应用所需的数学,然后调用它们。 http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees
#5
0
I like a more general functional approach:
我喜欢更通用的功能方法:
/**
* converts a trig function taking radians to degrees
* @param {function} trigFunc - eg. Math.cos, Math.sin, etc.
* @param {number} angle - in degrees
* @returns {number}
*/
const dTrig = (trigFunc, angle) => trigFunc(angle * Math.PI / 180);
or,
要么,
function dTrig(trigFunc, angle) {
return trigFunc(angle * Math.PI / 180);
}
which can be used with any radian-taking function:
可以与任何弧度函数一起使用:
dTrig(Math.sin, 90);
// -> 1
dTrig(Math.tan, 180);
// -> 0
Hope this helps!
希望这可以帮助!
#1
160
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:
请注意,像sin,cos等函数不返回角度,它们将角度视为输入。在我看来,拥有一个将度数输入转换为弧度的函数会更有用,如下所示:
function toRadians (angle) {
return angle * (Math.PI / 180);
}
which you could use to do something like tan(toRadians(45))
.
你可以用来做tan之类的东西(toRadians(45))。
#2
22
Multiply the result by 180/Math.PI
to convert from radians to degrees.
将结果乘以180 / Math.PI,将弧度转换为度数。
You could also define your own functions:
您还可以定义自己的功能:
function sinDegrees(angle) {return Math.sin(angle/180*Math.PI);};
and so on.
等等。
#3
13
I created my own little lazy Math-Object for degree (MathD), hope it helps:
我为学位(MathD)创建了自己的小懒惰Math-Object,希望它有所帮助:
//helper
/**
* converts degree to radians
* @param degree
* @returns {number}
*/
var toRadians = function (degree) {
return degree * (Math.PI / 180);
};
/**
* Converts radian to degree
* @param radians
* @returns {number}
*/
var toDegree = function (radians) {
return radians * (180 / Math.PI);
}
/**
* Rounds a number mathematical correct to the number of decimals
* @param number
* @param decimals (optional, default: 5)
* @returns {number}
*/
var roundNumber = function(number, decimals) {
decimals = decimals || 5;
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
//the object
var MathD = {
sin: function(number){
return roundNumber(Math.sin(toRadians(number)));
},
cos: function(number){
return roundNumber(Math.cos(toRadians(number)));
},
tan: function(number){
return roundNumber(Math.tan(toRadians(number)));
},
asin: function(number){
return roundNumber(toDegree(Math.asin(number)));
},
acos: function(number){
return roundNumber(toDegree(Math.acos(number)));
},
atan: function(number){
return roundNumber(toDegree(Math.atan(number)));
}
};
#4
0
Create your own conversion function that applies the needed math, and invoke those instead. http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees
创建自己的转换函数,应用所需的数学,然后调用它们。 http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees
#5
0
I like a more general functional approach:
我喜欢更通用的功能方法:
/**
* converts a trig function taking radians to degrees
* @param {function} trigFunc - eg. Math.cos, Math.sin, etc.
* @param {number} angle - in degrees
* @returns {number}
*/
const dTrig = (trigFunc, angle) => trigFunc(angle * Math.PI / 180);
or,
要么,
function dTrig(trigFunc, angle) {
return trigFunc(angle * Math.PI / 180);
}
which can be used with any radian-taking function:
可以与任何弧度函数一起使用:
dTrig(Math.sin, 90);
// -> 1
dTrig(Math.tan, 180);
// -> 0
Hope this helps!
希望这可以帮助!