//Vector2.cpp
#include "Vector2.h"
#include <math.h>
#include <algorithm>

Vector2 Vector2::operator + (const Vector2 &vector) const
{
	Vector2 temp(x + vector.x, y + vector.y);
	return temp;
}

Vector2& Vector2::operator += (const Vector2 &vector)
{
	x += vector.x;
	y += vector.y;;
	return *this;
}

Vector2 Vector2::operator - (const Vector2 &vector) const
{
	Vector2 temp(x - vector.x, y - vector.y);
	return temp;
}

Vector2& Vector2::operator -= (const Vector2 &vector)
{
	x -= vector.x;
	y -= vector.y;
	return *this;
}

Vector2 Vector2::operator * (const Vector2 &vector) const
{
	return Vector2(x * vector.x, y * vector.y);
}

Vector2 Vector2::operator * (const float &f) const
{
	Vector2 temp(x * f, y * f);
	return temp;
}

Vector2& Vector2::operator *= (const float &f)
{
	x *= f;
	y *= f;
	return *this;
}

Vector2 Vector2::operator / (const float &f) const
{
	float tempFloat = 1.0f / f;
	Vector2 tempVec(x * tempFloat, y * tempFloat);
	return tempVec;
}

Vector2& Vector2::operator /= (const float &f)
{
	float temp = 1.0f / f;
	x *= temp;
	y *= temp;
	return *this;
}

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

bool Vector2::operator == (const Vector2 &vector) const
{
	return (x == vector.x && y == vector.y);
}

void Vector2::normalise()
{
	float mag = this->getLength();
	if(mag > 0)
	{
		x /= mag;
		y /= mag; 
	}
}

Vector2 Vector2::perpendicular()
{
	return Vector2(-y, x);
}

float Vector2::dot(const Vector2 &left, const Vector2 &right)
{
	return (left.x * right.x) + (left.y * right.y);
}

float Vector2::cross(const Vector2 &left, const Vector2 &right)
{
	return (left.x * right.y) - (left.y * right.x);
}

Vector2 Vector2::vmin(const Vector2 &a, const Vector2 &b)
{
	Vector2 retVec;
	retVec.x = a.x > b.x ? b.x : a.x;
	retVec.y = a.y > b.y ? b.y : a.y;
	return retVec;
}

Vector2 Vector2::vmax(const Vector2 &a, const Vector2 &b)
{
	Vector2 retVec;
	retVec.x = a.x < b.x ? b.x : a.x;
	retVec.y = a.y < b.y ? b.y : a.y;
	return retVec;
}

float Vector2::getLength() const
{
	float temp = sqrt((x * x) + (y * y));
	return temp;
}

float Vector2::getLengthSqrd() const
{
	float temp = (x * x) + (y * y);
	return temp;
}

Vector2::operator const float* () const
{
	return &x;
}

bool Vector2::isRoughlyEqual(const Vector2 &a, const Vector2 &b)
{
	int ax = (int)a.x; int ay = (int)a.y;
	int bx = (int)b.x; int by = (int)b.y;

	if(ax == bx && ay == by)
		return true;
	else
		return false;
}




Vector2i Vector2i::operator + (const Vector2i &vector) const
{
	Vector2i temp(x + vector.x, y + vector.y);
	return temp;
}

Vector2i& Vector2i::operator += (const Vector2i &vector)
{
	x += vector.x;
	y += vector.y;;
	return *this;
}

Vector2i Vector2i::operator - (const Vector2i &vector) const
{
	Vector2i temp(x - vector.x, y - vector.y);
	return temp;
}

Vector2i& Vector2i::operator -= (const Vector2i &vector)
{
	x -= vector.x;
	y -= vector.y;
	return *this;
}

Vector2i Vector2i::operator * (const Vector2i &vector) const
{
	return Vector2i(x * vector.x, y * vector.y);
}

Vector2i Vector2i::operator * (const int &n) const
{
	Vector2i temp(x * n, y * n);
	return temp;
}

Vector2i& Vector2i::operator *= (const int &n)
{
	x *= n;
	y *= n;
	return *this;
}

Vector2i Vector2i::operator / (const int &n) const
{
	Vector2i tempVec(x / n, y / n);
	return tempVec;
}

Vector2i& Vector2i::operator /= (const int &n)
{
	x /= n;
	y /= n;
	return *this;
}

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

bool Vector2i::operator == (const Vector2i &vector) const
{
	return (x == vector.x && y == vector.y);
}


Vector2i Vector2i::perpendicular()
{
	return Vector2i(-y, x);
}

int Vector2i::dot(const Vector2i &left, const Vector2i &right)
{
	return (left.x * right.x) + (left.y * right.y);
}

int Vector2i::cross(const Vector2i &left, const Vector2i &right)
{
	return (left.x * right.y) - (left.y * right.x);
}

Vector2i Vector2i::vmin(const Vector2i &a, const Vector2i &b)
{
	Vector2i retVec;
	retVec.x = a.x > b.x ? b.x : a.x;
	retVec.y = a.y > b.y ? b.y : a.y;
	return retVec;
}

Vector2i Vector2i::vmax(const Vector2i &a, const Vector2i &b)
{
	Vector2i retVec;
	retVec.x = a.x < b.x ? b.x : a.x;
	retVec.y = a.y < b.y ? b.y : a.y;
	return retVec;
}

float Vector2i::getLength() const
{
	float temp = sqrt((float)((x * x) + (y * y)));
	return temp;
}

int Vector2i::getLengthSqrd() const
{
	int temp = (x * x) + (y * y);
	return temp;
}

Vector2i::operator const int* () const
{
	return &x;
}

