web-dev-qa-db-ja.com

2D単位ベクトルからの角度?

単位ベクトル0.5、0.5が与えられた場合、どのように角度(その方向)を見つけることができますか?

それは...ですか

cos(x)+ sin(y)?

25
jmasterx

Yとxを指定すると、x軸との角度は次のようになります。

atan2(y,x)

(0.5、0.5)の場合、角度は次のとおりです。

ラジアン:

In [2]: math.atan2(0.5,0.5)
Out[2]: 0.7853981633974483

度:

In [3]: math.atan2(0.5, 0.5)*180/math.pi
Out[3]: 45.0
64
mpenkov
#include <cmath>

double x = 0.5;
double y = 0.5;
double angleInRadians = std::atan2(y, x);
double angleInDegrees = (angleInRadians / M_PI) * 180.0;
13

残念ながら残念ながら、OPが方向を計算するコードを望んでいるという回答はなく、むしろ全体的な角度です。これを修正させてください。

atan(他の回答で言及)は、±0..90°の角度を与えます。その後、ベクトルがどの象限にあるかを把握し、それに応じて角度を変更する必要があります。 xまたはyがゼロに等しい特殊なケースを忘れないでください!以下は、私が使用するわずかに変更されたコードです。

#include <cmath>
#include <iostream>

using namespace std;

constexpr int radToDeg(float rad) { return rad*(180/M_PI); }

int vectorAngle(int x, int y) {
    if (x == 0) // special cases
        return (y > 0)? 90
            : (y == 0)? 0
            : 270;
    else if (y == 0) // special cases
        return (x >= 0)? 0
            : 180;
    int ret = radToDeg(atanf((float)y/x));
    if (x < 0 && y < 0) // quadrant Ⅲ
        ret = 180 + ret;
    else if (x < 0) // quadrant Ⅱ
        ret = 180 + ret; // it actually substracts
    else if (y < 0) // quadrant Ⅳ
        ret = 270 + (90 + ret); // it actually substracts
    return ret;
}

int main() {
    cout << vectorAngle(1,0) << endl
         << vectorAngle(1,1) << endl
         << vectorAngle(0,1) << endl
         << vectorAngle(-1,1) << endl
         << vectorAngle(-1,0) << endl
         << vectorAngle(-1,-1) << endl
         << vectorAngle(0,-1) << endl
         << vectorAngle(1,-1) << endl
         << endl;
}

$ g++ test2.cpp -o a -g3 -O0 && ./a
0
45
90
135
180
225
270
315

ただし、実際のコードでは、度とラジアンの両方を頻繁に使用する場合(たとえば、度で入力を取得し、C++関数がラジアンを使用しているため)、ラッパーを使用することをお勧めします時々それらを交換しないためにそれらのまわりで(それは私に起こりました)。以下の完全を期すために、自動車ゲーム用のボットからの関連コードの抜粋を示します。好きなように使用してください:)

#include <cmath>
#include <iostream>

using namespace std;

struct Point {
    int x, y;
    bool operator==(const Point& p) const {
        return p.x == x && p.y == y;
    }
    bool operator!=(const Point& p) const {
        return !(p == *this);
    }
    Point operator+(const Point& rhs) const {
        return {x + rhs.x, y + rhs.y};
    }
    Point operator-(const Point& rhs) const {
        return {x - rhs.x, y - rhs.y};
    }
    void operator+=(const Point& rhs) {
        x += rhs.x;
        y += rhs.y;
    }
    friend ostream& operator<<(ostream& os, const Point& p) {
        os << "x = " << p.x << ", y = " << p.y;
        return os;
    }
};

template<typename T>
struct NumWrapper {
    T val;
    friend ostream& operator<<(ostream& os, const NumWrapper& w) {
        os << w.val;
        return os;
    }
    friend istream& operator>>(istream& is, NumWrapper& w) {
        is >> w.val;
        return is;
    }
    NumWrapper operator-(const T rhs) const {
        return {val - rhs};
    }
    NumWrapper operator-(const NumWrapper rhs) const {
        return {val - rhs.val};
    }
    NumWrapper operator-() const {
        return {-val};
    }
    NumWrapper operator+(const T rhs) const {
        return {val + rhs};
    }
    NumWrapper operator+(const NumWrapper rhs) const {
        return {val + rhs.val};
    }
};
using Degree = NumWrapper<int>;
using Radian = NumWrapper<float>;

constexpr Radian degToRad(Degree degree) { return {degree.val*(M_PI/180)}; }
constexpr Radian degToRad(int degree)    { return {degree*(M_PI/180)}; }
constexpr Degree radToDeg(Radian rad)    { return {rad.val*(180/M_PI)}; }
constexpr Degree radToDeg(float rad)     { return {rad*(180/M_PI)}; }

Degree vectorAngle(const Point& vec) {
    if (vec.x == 0) // special cases
        return (vec.y > 0)? Degree{90}
            : (vec.y == 0)? Degree{0}
            : Degree{270};
    else if (vec.y == 0) // special cases
        return (vec.x >= 0)? Degree{0}
            : Degree{180};
    Degree ret = radToDeg(atanf((float)vec.y/vec.x));
    if (vec.x < 0 && vec.y < 0) // quadrant Ⅲ
        ret.val = 180 + ret.val;
    else if (vec.x < 0) // quadrant Ⅱ
        ret.val = 180 + ret.val; // it actually substracts
    else if (vec.y < 0) // quadrant Ⅳ
        ret.val = 270 + (90 + ret.val); // it actually substracts
    return ret;
}

int main() {
    cout << vectorAngle({1,0}) << endl
         << vectorAngle({1,1}) << endl
         << vectorAngle({0,1}) << endl
         << vectorAngle({-1,1}) << endl
         << vectorAngle({-1,0}) << endl
         << vectorAngle({-1,-1}) << endl
         << vectorAngle({0,-1}) << endl
         << vectorAngle({1,-1}) << endl
         << endl;
}
4
Hi-Angel