CSC3095_Project_And_Dissertation_In_Computing / nclgl / Vector2.h
Vector2.h
Raw
#pragma once
/*
Class:Vector2
Implements:
Author:Rich Davison
Description:VERY simple Vector2 class. Students are encouraged to modify this as necessary!

-_-_-_-_-_-_-_,------,
_-_-_-_-_-_-_-|   /\_/\   NYANYANYAN
-_-_-_-_-_-_-~|__( ^ .^) /
_-_-_-_-_-_-_-""  ""

*/
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
#include <iostream>

class Vector2 {
public:
	Vector2(void) {
		ToZero();
	}

	Vector2(const float x, const float y) {
		this->x = x;
		this->y = y;
	}

	~Vector2(void) {}

	float x;
	float y;

	void ToZero() {
		x = 0.0f;
		y = 0.0f;
	}

	inline friend std::ostream& operator<<(std::ostream& o, const Vector2& v) {
		o << "Vector2(" << v.x << "," << v.y << ")" << std::endl;
		return o;
	}

	inline Vector2  operator-(const Vector2  &a) const {
		return Vector2(x - a.x, y - a.y);
	}

	inline Vector2  operator+(const Vector2  &a) const {
		return Vector2(x + a.x, y + a.y);
	}




	/*void Vector2::addVector3(const Vector3 &a) {
		this->x = a.x;
		this->y = a.y;
	}*/











	// Sets values of x and y for Vector2
	void Vector2::set(float i, float o)
	{
		x = i;
		y = o;
	}

	void Vector2::addVector(Vector2 v)
	{
		x += v.x;
		y += v.y;
	}

	// Adds to a Vector2 by a constant number
	void Vector2::addScalar(float s)
	{
		x += s;
		y += s;
	}

	// Subtracts 2 vectors
	void Vector2::subVector(Vector2 v)
	{
		x -= v.x;
		y -= v.y;
	}

	// Subtracts two vectors and returns the difference as a vector
	Vector2 Vector2::subTwoVector(Vector2 v, Vector2 v2)
	{
		Vector2 tmp;
		v.x -= v2.x;
		v.y -= v2.y;
		tmp.set(v.x, v.y);
		return tmp;
	}

	// Adds to a Vector2 by a constant number
	void Vector2::subScalar(float s)
	{
		x -= s;
		y -= s;
	}

	// Multiplies 2 vectors
	void Vector2::mulVector(Vector2 v)
	{
		x *= v.x;
		y *= v.y;
	}

	// Adds to a Vector2 by a constant number
	void Vector2::mulScalar(float s)
	{
		x *= s;
		y *= s;
	}

	// Divides 2 vectors
	void Vector2::divVector(Vector2 v)
	{
		x /= v.x;
		y /= v.y;
	}

	// Adds to a Vector2 by a constant number
	void Vector2::divScalar(float s)
	{
		x /= s;
		y /= s;
	}

	void Vector2::limit(double max)
	{
		double size = magnitude();

		if (size > max) {
			set(x / size, y / size);
		}
	}

	// Calculates the distance between the first Vector2 and second Vector2
	float Vector2::distance(Vector2 v) const
	{
		float dx = x - v.x;
		float dy = y - v.y;
		float dist = sqrt(dx*dx + dy*dy);
		return dist;
	}

	// Calculates the dot product of a vector
	float Vector2::dotProduct(Vector2 v) const
	{
		float dot = x * v.x + y * v.y;
		return dot;
	}

	// Calculates magnitude of referenced object
	float Vector2::magnitude() const
	{
		return sqrt(x*x + y*y);
	}

	void Vector2::setMagnitude(float x)
	{
		normalize();
		mulScalar(x);
	}

	// Calculate the angle between Vector2 1 and Vector2 2
	float Vector2::angleBetween(Vector2 v) const
	{
		if (x == 0 && y == 0) return 0.0f;
		if (v.x == 0 && v.y == 0) return 0.0f;

		double dot = x * v.x + y * v.y;
		double v1mag = sqrt(x * x + y * y);
		double v2mag = sqrt(v.x * v.x + v.y * v.y);
		double amt = dot / (v1mag * v2mag); //Based of definition of dot product
											//dot product / product of magnitudes gives amt
		if (amt <= -1) {
			return M_PI;
		}
		else if (amt >= 1) {
			return 0;
		}
		float tmp = acos(amt);
		return tmp;
	}

	// normalize divides x and y by magnitude if it has a magnitude.
	void Vector2::normalize()
	{
		float m = magnitude();

		if (m > 0) {
			set(x / m, y / m);
		}
		else {
			set(x, y);
		}
	}

	// Creates and returns a copy of the Vector2 used as a parameter
	Vector2 Vector2::copy(Vector2 v)
	{
		Vector2 copy(v.x, v.y);
		return copy;
	}





	float dist(const Vector2 &v1, const Vector2 &v2) {
		float dX = v2.x - v1.x,
			dY = v2.y - v1.y;

		return sqrtf((dX*dX) + (dY*dY));
	}


};