Vector to angle conversion function sometimes returns radians and sometimes degree

  2d-vector, angle, c++, degrees, radians

I tried to write a program where a vector is calculated that bisects the angle between two lines that share one common point.

For that I’ve come up with some code (Since this it not the only thing I’m trying to do the code is a bit longer but I’ve boiled down the problem to this snipped.):

#include <iostream>
#include <cmath>

// A 2D Vector class
class PVector {

public: PVector() = default;
public: PVector(double _x, double _y) : x(_x), y(_y) {};

public: double x, y;

public: PVector set(double _x, double _y) {
        x = _x, y = _y;
        return *this;
    };
    
public: double getMag() const {
        return sqrt(x * x + y * y);
    };

public: PVector setMag(double mag) {
        mag *= getMag();
        return (mag == 0) ? set(0, 0) : set(x / mag, y / mag);
    };

public: PVector &operator+=(const PVector &rhs) {
        x += rhs.x, y += rhs.y;
        return *this;
    };

public: PVector operator+(const PVector &rhs) {
        return PVector(*this) += rhs;
    };

public: PVector &operator-=(const PVector &rhs) {
        x -= rhs.x, y -= rhs.y;
        return *this;
    };

public: PVector operator-(const PVector &rhs) {
        return PVector(*this) -= rhs;
    };
public: PVector &operator*=(const double &m) {
        x *= m, y *= m;
        return *this;
    };
};

// A function to convert a 2D vector into an angle
double vector2Angle(double x, double y) {

    if (x == 0)
        return (y >= 0) ? 0 : 180;
    else if (y == 0)
        return (x > 0) ? 90 : 270;

    double angle = atan(y / x);

    // bottom left (90 - 180)
    if (x < 0 && y < 0)
        // angle is positive (180 location)
        angle = M_PI / 2;
        // top left (0 - 90)
    else if (x < 0)
        // angle is negative (90 positive) + (0 location)
        angle += M_PI / 2;
        // bottom right (180 - 270)
    else if (y < 0)
        // angle is negative (90 positive) + (180 location)
        angle += 1.5 * M_PI;
        // top right (270 - 360)
    else {
        angle += 1.5 * M_PI;
        // angle is positive
    }
    return angle;
};
double vector2Angle(PVector v) {
    return vector2Angle(v.x, v.y);
};

int main()
{
    PVector p0 = PVector(90, 90);
    PVector p1 = PVector(10, 90);
    PVector p2 = PVector(10, 10);

// The sum of two unit vectors must return a vector that bisects the angle between the two vectors.
    std::cout << "Expected: " << (vector2Angle(p1 - p0) + vector2Angle(p1 - p2)) / 2 << std::endl;
    std::cout << "Got: " << vector2Angle((p1 - p0).setMag(1) + (p1 - p2).setMag(1)) << std::endl;
    return 0;
}

From intuition the output should be either 135° or 315° but the program delivers:

Expected: 135
Got: 0.785398

The first strange thing about this output is that one is in degrees although vector2Angle returns a radian value. Even stranger is the fact that both results are in different units. And at last I’m wondering where my mistake in the calculation is because 0.785… radian are about 45° and not 135°.

Source: Windows Questions C++

LEAVE A COMMENT