How can I get sin, cos, and tan to use degrees instead of radians?
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))
.
Multiply the input by Math.PI/180
to convert from degrees to radians before calling the system trig functions.
You could also define your own functions:
function sinDegrees(angleDegrees) { return Math.sin(angleDegrees*Math.PI/180);};
and so on.
I created my own little lazy Math-Object for degree (MathD), hope it helps:
//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 objectvar 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))); }};