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?

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

Last updated at Posted at 2023-04-17

Atan(x/y)的なやつや,ベクトルを引数に取れるAtan2がよく検索に引っかかりますが、プログラムでは条件分岐ができるのでこんな風に素朴に書いちゃってもいいと思います。
スマートではないですが左右反転させたいとか角度を測る基準が違うときには頭がこんがらがりにくくなると思います。

    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)
        }
    }
1
1
2

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?