
#include "vector2d.h"


Vector2D::Vector2D(float X, float Y)
{
	x = X;
	y = Y;
}

Vector2D::Vector2D(const Vector2D& Copy)
{
	x = Copy.x;
	y = Copy.y;
}
Vector2D Vector2D::operator=(const Vector2D &Copy)
{
	x = Copy.x;
	y = Copy.y;
	return *this;
}


Vector2D Vector2D::operator+(const Vector2D& Sum) const
{
	return Vector2D(x+Sum.x, y+Sum.y);
}

Vector2D Vector2D::operator-(const Vector2D& Diff) const
{
	return Vector2D(x-Diff.x, y-Diff.y);
}

void Vector2D::operator+=(const Vector2D& Sum)
{
	x += Sum.x;
	y += Sum.y;
}

void Vector2D::operator-=(const Vector2D& Diff)
{
	x -= Diff.x;
	y -= Diff.y;
}


float Vector2D::operator*(const Vector2D& Prod) const
{
	return x*Prod.x + y*Prod.y;
}

Vector2D Vector2D::operator*(float Prod) const
{
	return Vector2D(x*Prod,y*Prod);
}
void Vector2D::operator*=(float Prod)
{
	x*=Prod;
	y*=Prod;
}

Vector2D Vector2D::operator/(float Div) const
{
	return Vector2D(x/Div, y/Div);
}
void Vector2D::operator/=(float Div)
{
	x/=Div;
	y/=Div;
}


Vector2D Vector2D::Perpendicular() const
{
	return Vector2D(y, -x);
}

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

Vector2D Vector2D::Normalized() const
{
	return *this / Length();
}

float Vector2D::Theta() const
{
	float alpha = asin( Normalized().y );
	alpha *= 180 / M_PI; // radian to degrees

	if(x < 0)
	{
		return(180 - alpha); // alpha is negative
	}
	else
	{
		if(y < 0)
			return(360 + alpha);
		else
			return(alpha);
	}
}


float Vector2D::Distance(const Vector2D& a, const Vector2D& b)
{
	return (a-b).Length();
}

Vector2D Vector2D::IntersectionTwoPointsForm(const Vector2D& L1P1, const Vector2D& L1P2, const Vector2D& L2P1, const Vector2D& L2P2)
{
	return IntersectionParameterForm(L1P1, L1P2-L1P1, L2P1, L2P2-L2P1);
}

Vector2D Vector2D::IntersectionParameterForm(const Vector2D& L1P, const Vector2D& L1Dir, const Vector2D& L2P, const Vector2D& L2Dir)
{
	float k = ((L2Dir.x*L2P.y - L2Dir.y*L2P.x) + (L1P.x*L2Dir.y - L1P.y*L2Dir.x)) / (L2Dir.x*L1Dir.y - L2Dir.y*L1Dir.x);

	return L1P + L1Dir*k;
}
