1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【数学苦手な人へ】座標、方向ベクトルから角度を計算する方法

Last updated at Posted at 2023-04-17

Atan(x/y)的なやつがよく検索に引っかかるが、プログラムでは条件分岐ができるのだからこうしてやればよい。

    float Vec2Radian(Vector2 vector)
    {
        if (vector.y == 0) //x軸上
        {
            if (vector.x > 0)
            {
                return Acos(vector.x)
            }
            else
            {
                return Acos(vector.x)
            }
        }   
        if (vector.y >= 0) //第1,2象限
        {
            return Acos(vector.x)
        }
        else //第3,4象限
        {
            return -Acos(vector.x)
        }
    }

このコードはUnityC#を見やすくしたやつだがまぁ雰囲気は誰にでも伝わるだろう。
例のAtanを使う方法はたまに∞がでてバグるし、こっちのが数学苦手でも直観的に理解できるしこっち使えばいいと思う。
あ、あとベクトルの正規化をお忘れなきよう。

1
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?