atan与atan2的区别
相比較ATan,ATan2究竟有什么不同?本篇介紹一下ATan2的用法及使用條件。
對(duì)于tan(θ) =?y?/?x:
θ?=?ATan(y?/?x)求出的θ取值范圍是[-PI/2, PI/2]。
θ?=?ATan2(y, x)求出的θ取值范圍是[-PI, PI]。
-
當(dāng) (x,?y) 在第一象限, 0 <?θ?<?PI/2.
-
當(dāng)?(x,?y)?在第二象限?PI/2 <?θ≤PI.
-
當(dāng)?(x,?y) 在第三象限, -PI?<?θ?< -PI/2.
-
當(dāng)?(x,?y)?在第四象限, -PI/2 <?θ?< 0.
當(dāng)點(diǎn)(x,?y)在象限的邊界也就是坐標(biāo)軸上時(shí):
-
當(dāng) y 是 0,x 為非負(fù)值, θ = 0.
-
當(dāng)?y 是 0, x 是?負(fù)值, θ = PI.
-
當(dāng)?y 是?正值, x 是?0, θ = PI/2.
-
當(dāng)?y 是?負(fù)值,?x 是?0, θ = -PI/2.
由此可知,一般情況下用ATan即可,當(dāng)對(duì)所求出角度的取值范圍有特殊要求時(shí),應(yīng)使用ATan2。
?
?atan 和 atan2 都是求反正切函數(shù),如:有兩個(gè)點(diǎn) point(x1,y1), 和 point(x2,y2);
那么這兩個(gè)點(diǎn)形成的斜率的角度計(jì)算方法分別是:
float?angle = atan( (y2-y1)/(x2-x1) );
或
float angle = atan2( y2-y1, x2-x1?);
?
?atan 和 atan2 區(qū)別:
1:參數(shù)的填寫方式不同;
2:atan2 的優(yōu)點(diǎn)在于 如果 x2-x1等于0 依然可以計(jì)算,但是atan函數(shù)就會(huì)導(dǎo)致程序出錯(cuò);
?
結(jié)論:?atan 和 atan2函數(shù),建議用 atan2函數(shù);
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/13224ACMer/p/4693060.html
總結(jié)
以上是生活随笔為你收集整理的atan与atan2的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poj2976 Dropping tes
- 下一篇: 并发编程-concurrent指南-线程