public static double Atan2(double y,double x)
Parameters(參數(shù)):
- The y coordinate(坐標(biāo)) of a point(點(diǎn)).
- The x coordinate of a point.
Return Value
An angle, θ, measured in radians(以弧度來計(jì)量), such that -π≤θ≤π, and tan(θ) = y / x, where (x, y) is a point in the Cartesian plane(笛卡爾平面).
Observe the following(觀察如下:)[實(shí)際就是atan2實(shí)現(xiàn)細(xì)節(jié)]
- For (x, y) in quadrant(象限) 1, 0 < θ < π/2.
- For (x, y) in quadrant(象限) 2, π/2 < θ≤π.
- For (x, y) in quadrant(象限) 3, -π < θ < -π/2.
- For (x, y) in quadrant(象限) 4, -π/2 < θ < 0.
For points on the boundaries(邊緣或分界線) of the quadrants(象限), the return value is the following:
- If y is 0 and x is not negative(不為負(fù)), θ = 0.
- If y is 0 and x is negative, θ = π.
- If y is positive(為正) and x is 0, θ = π/2.
- If y is negative and x is 0, θ = -π/2.
- If y is 0 and x is 0, θ = 0.
If x or y is NaN(not a number,不是一個(gè)數(shù)值), or if x and y are either(要么是) PositiveInfinity(正無窮大) or (要么是)NegativeInfinity(負(fù)無窮大), the method returns NaN.
This is the counterclockwise(逆時(shí)針) angle.

專業(yè)詞匯區(qū)別:
- Parameter: 形式參數(shù)(函數(shù)聲明中的參數(shù))
- Argument: 實(shí)際參數(shù)(函數(shù)調(diào)用時(shí)的參數(shù))
下面擴(kuò)展是因?yàn)楹蛥?shù)關(guān)系比較密切(其實(shí)和類型更密切):
擴(kuò)展一下,什么是函數(shù)簽名(the function’s signature)
強(qiáng)類型語言中,函數(shù)簽名是非常重要的一個(gè)概念,那么
提問: 函數(shù)簽名包括那些內(nèi)容呢?
回答: 除了函數(shù)名之外的任何東西
例如: double Atan2(double y,double x)
簽名包括:
- 函數(shù)的返回值類型(double)
- 函數(shù)的參數(shù)個(gè)數(shù)(2個(gè))
- 函數(shù)的參數(shù)類型(2個(gè)都是double)
- 函數(shù)的參數(shù)的順序位置(不能順序錯(cuò)誤)
- 如果是c/c++的話,還可能包括函數(shù)的調(diào)用規(guī)范
其實(shí)回調(diào)函數(shù)就是函數(shù)簽名的一個(gè)經(jīng)典應(yīng)用場(chǎng)合。
重點(diǎn)句型及擴(kuò)展:
- either....or....: 要么.....要么.....(兩者選其一)
- neither...nor...: 即不是....也不是....(兩者都否定)
- not only...but also...: 不但...而且...(兩者都為肯定)
如果這三句不放在一起講解,那肯定不是個(gè)好老師,哈哈!
與atan區(qū)別:
與 atan 的不同,atan2 比 atan 穩(wěn)定。
如:atan(y/x),當(dāng) y 遠(yuǎn)遠(yuǎn)大于 x 時(shí),計(jì)算結(jié)果是不穩(wěn)定的。
atan2(y,x)的做法:
當(dāng) x 的絕對(duì)值比 y 的絕對(duì)值大時(shí)使用 atan(y/x);
反之使用 atan(x/y)。
這樣就保證了數(shù)值穩(wěn)定性。
這三天,分享了三角函數(shù)sin/cos/atan2這三個(gè)基礎(chǔ)重要的函數(shù),因?yàn)橹灰@三個(gè)函數(shù),就能解決經(jīng)典的2D朝向正確的位移功能。

上面就是sin/cos/atan2的應(yīng)用。當(dāng)然還有高效的矢量法及矩陣法。但是三角函數(shù)的確是最基礎(chǔ)最基礎(chǔ)的解決方案。其他方案都是從三角函數(shù)開始更高層次的公式推導(dǎo)而來。
其實(shí)你會(huì)發(fā)現(xiàn)一個(gè)有趣的事實(shí),當(dāng)你學(xué)了矢量數(shù)學(xué),你會(huì)覺得經(jīng)典牛頓力學(xué)的計(jì)算會(huì)方便快速高效很多,而當(dāng)你學(xué)了導(dǎo)數(shù)后,高中時(shí)令人頭疼的極值問題迎刃而解。
但是不管如何,只有一步一步,踏踏實(shí)實(shí)的學(xué)習(xí)才能有進(jìn)步不是。
順便提一下,關(guān)于參考文檔,我個(gè)人偏好微軟的MSDN。