javascript 在javascript中晒黑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4715271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tan in javascript
提问by blab
tan(5.3) = 0.09276719520463
棕褐色(5.3)= 0.09276719520463
but in javascript:
但在 javascript 中:
Math.tan(5.3) = -1.50127339580693
Math.tan(5.3) = -1.50127339580693
how do i calculate Math.tan(something) in javascript in the deg mode?
我如何在 deg 模式下在 javascript 中计算 Math.tan(something)?
回答by Phrogz
Instead of writing a wrapper function for it (and taking a performance hit), you can multiply by these constants:
您可以乘以这些常量,而不是为它编写包装函数(并影响性能):
var deg2rad = Math.PI/180;
var rad2deg = 180/Math.PI;
And then use them like so:
然后像这样使用它们:
var ratio = Math.tan( myDegrees * deg2rad );
var degrees = Math.atan( ratio ) * rad2deg;
JavaScript deals only in radians, both as arguments and return values. It's up to you to convert them as you see fit.
JavaScript 只处理弧度,作为参数和返回值。您可以根据自己的意愿转换它们。
Also, note that if you're trying to find the degrees of rotation for xy coordinates, you should use Math.atan2so that JavaScript can tell which quadrant the point is in and give you the correct angle:
另外,请注意,如果您试图找到 xy 坐标的旋转度数,您应该使用,Math.atan2以便 JavaScript 可以判断该点位于哪个象限并为您提供正确的角度:
[ Math.atan( 1/ 1), Math.atan2( 1, 1) ]; // [ 45, 45 ]
[ Math.atan( 1/-1), Math.atan2( 1,-1) ]; // [ -45, 135 ]
[ Math.atan(-1/ 1), Math.atan2(-1, 1) ]; // [ -45, -45 ]
[ Math.atan(-1/-1), Math.atan2(-1,-1) ]; // [ 45,-135 ]
回答by Cronco
From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/tan
来自https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/tan
function getTanDeg(deg) {
var rad = deg * Math.PI/180;
return Math.tan(rad)
}
回答by Kirk Strobeck
So hard to find !
太难找了!
To accomplish basic triangle mathin JavaScript, use ..
要在 JavaScript 中完成基本的三角形数学,请使用 ..
Math.atan(opposite/adjacent) * 180/Math.PI

