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

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

*/
#include <cmath>
#include <iostream>

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

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

	~Vector3(void){}

	float x;
	float y;
	float z;

	void			Normalise() {
		float length = Length();

		if(length != 0.0f)	{
			length = 1.0f / length;
			x = x * length;
			y = y * length;
			z = z * length;
		}
	}

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

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

	void			Invert() {
		x = -x;
		y = -y;	
		z = -z;	
	}

	Vector3			Inverse() const{
		return Vector3(-x,-y,-z);
	}

	static float	Dot(const Vector3 &a, const Vector3 &b) {
		return (a.x*b.x)+(a.y*b.y)+(a.z*b.z);
	}

	static Vector3	Cross(const Vector3 &a, const Vector3 &b) {
		return Vector3((a.y*b.z) - (a.z*b.y) , (a.z*b.x) - (a.x*b.z) , (a.x*b.y) - (a.y*b.x));	
	}

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

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

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

	inline Vector3  operator-() const{
		return Vector3(-x,-y,-z);
	}

	inline void operator+=(const Vector3  &a){
		x += a.x;
		y += a.y;
		z += a.z;
	}

	inline void operator-=(const Vector3  &a){
		x -= a.x;
		y -= a.y;
		z -= a.z;
	}

	inline Vector3  operator*(const float a) const{
		return Vector3(x * a,y * a, z * a);
	}

	inline Vector3  operator*(const Vector3  &a) const{
		return Vector3(x * a.x,y * a.y, z * a.z);
	}

	inline Vector3  operator/(const Vector3  &a) const{
		return Vector3(x / a.x,y / a.y, z / a.z);
	};

	inline Vector3  operator/(const float v) const{
		return Vector3(x / v,y / v, z / v);
	};

	inline bool	operator==(const Vector3 &A)const {return (A.x == x && A.y == y && A.z == z) ? true : false;};
	inline bool	operator!=(const Vector3 &A)const {return (A.x == x && A.y == y && A.z == z) ? false : true;};

	/**/
	Vector3 makeUnitVector3(const Vector3 &a) const {
		//Store the magnitude of this Vector3D
		float mag = magnitude(a);

		//If magnitude is 0 return default Vector3D
		if (mag == 0.0f) {
			return Vector3();
		}
		//If magnitude is one, return copy of current Vector3D 
		else if (mag == 1.0f) {
			return Vector3(*this);
		}

		//Calculate each component of unit vector
		float xU = x / mag;
		float yU = y / mag;
		float zU = 0;

		return Vector3(xU, yU, zU);
	}


	inline float Vector3::magnitude(const Vector3  &a) const {
		return sqrt((pow(x, 2) + pow(y, 2) + pow(z , 2)));
	}

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

	inline void Vector3::limit(float max)
	{
		float size = magnitudeCalc();

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

	inline float Vector3::magnitudeCalc() const
	{
		return sqrt(x*x + y * y + z * z);
	}

	inline void Vector3::set(float x, float y, float z)
	{
		this->x = x;
		this->y = y;
		this->z = z;
	}
};