34

Given unit vector 0.5, 0.5 how could I find the angle (its direction),

is it

cos(x) + sin(y)?

genpfault
  • 47,669
  • 9
  • 68
  • 119
jmasterx
  • 47,653
  • 87
  • 281
  • 523
  • To convert from slope to degrees its atan therefore you can use atan2 for your vector. – Dair Jun 06 '11 at 01:53
  • 20
    That's not a unit vector, by the way. A unit vector has a length of 1, and that vector has a length of sqrt(2)/2 – Benjamin Lindley Jun 06 '11 at 02:29
  • @BenjaminLindley That's absolutely correct, and it makes all the answers therefore a bit wonky. There will never be a unit vector with (0.5,0.5) - the closest you can get is roughly (0.707,0.707) from pi/2 radians. – Benjamin R Jan 08 '15 at 08:00

3 Answers3

85

Given y and x, the angle with the x axis is given by:

atan2(y,x)

With (0.5, 0.5) the angle is:

radians:

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

degrees:

In [3]: math.atan2(0.5, 0.5)*180/math.pi
Out[3]: 45.0
mpenkov
  • 20,356
  • 7
  • 75
  • 121
15
#include <cmath>

double x = 0.5;
double y = 0.5;
double angleInRadians = std::atan2(y, x);
double angleInDegrees = (angleInRadians / M_PI) * 180.0;
Jonathan Grynspan
  • 42,652
  • 8
  • 68
  • 102
7

Good answers already posted, unfortunately nobody addressed that OP wanted code to calculate the direction, being rather a global angle. Let me fix this.

atan (mentioned in other answers) would give you an angle ±0..90°. Afterwards you'd need to figure out which quadrant the vector is in, and modify the angle accordingly; and don't forget the special cases of either x or y equal to zero! Here's a slightly modified code I use:

#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

In real code, however, if you use both degrees and radians a lot (e.g. because you're getting an input with degrees, and then C++ functions are using radians), I'd recommend to use wrappers around them to not swap them occasionally (which did happen to me). For the sake of completeness below is a snip of relevant code from my bot for a car game, use it however you like :)

#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;
}
Hi-Angel
  • 3,835
  • 6
  • 50
  • 78
  • 2
    Other answers were referencing `atan2` which given x and y components returns an angle in a proper quadrant. – Teivaz May 07 '18 at 17:29
  • @teivaz hah, ⁺¹, indeed. Oh, well, at least I exercised my trigonometry skills, and posted ㎭ & degree wrappers — I guess they still useful to people searching for that kind of answer. – Hi-Angel May 07 '18 at 20:13