// Vector2D.cpp: implementation of the Vector2D class.
//
//////////////////////////////////////////////////////////////////////

#include <cmath>
//using namespace std;

#include "Vector2D.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Vector2D::Vector2D(float newx, float newy)
{
	x = newx;
	y = newy;
}

void Vector2D::setMagnitude(float mag)
{
	*this = this->normalized() * mag;
}

Vector2D Vector2D::normalized() const
{
	float m = this->magnitude();

	if(m == 0)
		return Vector2D(1,0);

	return Vector2D(x/m, y/m);
}

float Vector2D::magnitude() const
{
	return (float)sqrt(x*x + y*y);
}

// returns the angle in radians with zero being (0, -1) and increasing clockwise
float Vector2D::angle() const
{
	const float PI = 3.14159f;
	float a;

	if(y == 0)
	{
		if(x > 0)
			return (PI / 2.0f);
		else
			return (-PI / 2.0f);
	}
	else a = (float)atan(-x/y);

	if(y > 0)
		a += PI;

	return a;
}

void Vector2D::setPolar(float angle, float mag)
{
    const float PI = 3.14159f;
    angle += PI/2;
    x = mag*cosf(angle);
    y = mag*sinf(angle);
}

float Vector2D::angle(const Vector2D& A) // angle between this vector and A
{
	return acosf(DotProduct(*this, A)/(this->magnitude() * A.magnitude()));
}