Math.atan2

Math.atan2

呼び出し形式

Math.atan2(y, x)

組み込みモジュールMathのモジュール関数。

Math.atan?(y/x)と同義。

直交座標上で原点(0, 0)と点(x, y)とのなす角を

ラジアン単位で求められる。(範囲:[-π, π])

(参考) 2点のなす角を求める

2点の座標が(x1, y1)、(x2, y2)であったとき、なす角[rad]は、

Math.atan2(y2-y1, x2-x1)

である。

(参考) ラジアン → 度数 への変換

ラジアンをrad、度数法の角度をangle、とすると、

angle=\frac{rad \times 180}{\pi}

という式が成り立つ。よって、

angle = rad * 180 / Math::PI

と変換できる。

ただし、実用上はラジアンの方が便利なため、

あくまで確認程度に用いた方が良い。

例:

p Math.atan2(1, 1) * 180 / Math::PI    # => 45.0
p Math.atan2(1, 0) * 180 / Math::PI    # => 90.0
p Math.atan2(1, -1) * 180 / Math::PI   # => 135.0