2d Vector class
Posted: Tue Jan 26, 2010 10:33 am
I felt like sharing this, it's my 2d vector class, useful for game physic systems and rather versatile (tailored to my needs/style of coding). I'm fairly sure all the maths is in order though I haven't touched it for a while. Feel free to pick it apart and make suggestions, or take ideas from it. It can easily be extended to 3d by adding the z variable and perhaps a function for getting the cross product of two vectors.
Code: Select all
/*
* 2D vector class
*
*
* Created by Sam Zeid on 6/11/09.
* Copyright 2009 *. All rights reserved.
*
*/
#ifndef _VECTOR2_H
#define _VECTOR2_H
#include <math.h>
template<typename T>
class Vector2
{
public:
//the cartesian coordinates that are stored for a vector are not declared private.
//should the user want to assign new values to the vector,
//they can do so by accessing its members directly or using
//the operator ()
T x,y;
//if a vector is declared by default its values are 0
Vector2():x(0),y(0){}
//the user can define the vector coordinates upon creation
Vector2(T X, T Y):x(X),y(Y){}
//S P E C I A L F U N C T I O N S A N D O P E R A T O R S
//the () operator sets values of the vector
void operator()(const T X, const T Y)
{
x=X;y=Y;
}
//magnitude returns the vectors length
T magnitude() const
{
return sqrt(pow(x,2)+pow(y,2));
}
//get the dot product of two vectors as a scalar value
T dot(Vector2<T> rightHandSide) const
{
return ((x*rightHandSide.x)+(y*rightHandSide.y));
}
const Vector2<T> normalise()
{
T n=magnitude();
if(n==0)return *this;
x/=n;y/=n;
return Vector2<T>(x, y);
}
//C O M P A R I T I V E O P E R A T O R S
//the == operator tests if a vector is equal to another
bool operator==(const Vector2<T> &rightHandSide)
{
return (x==rightHandSide.x && y==rightHandSide.y);
}
//the != operator tests if a vector is not equal to another
bool operator!=(const Vector2<T> &rightHandSide)
{
return (x!=rightHandSide.x || y!=rightHandSide.y);
}
//A R I T H M E T I C O P E R A T O R S
//the = operator makes a vector equal to another
const Vector2<T> &operator=(const Vector2<T> &rightHandSide)
{
x=rightHandSide.x;y=rightHandSide.y;
return *this; //the resulting vector is returned
}
//the - operator may return the negative of a vector
const Vector2<T> operator-(void)const
{
return Vector2<T>(-x, -y);
}
//the - operator may also return the subtraction of two vectors
const Vector2<T> operator-(Vector2<T> &rightHandSide)const
{
return Vector2<T>(x-rightHandSide.x, y-rightHandSide.y);
}
//the + operator returns the addition of two vectors
const Vector2<T> operator+(Vector2<T> &rightHandSide)const
{
return Vector2<T>(x+rightHandSide.x, y+rightHandSide.y);
}
//the * operator returns the uniform scale of a vector
const Vector2<T> operator*(T s)const
{
return Vector2<T>(x*s, y*s);
}
//the / operator returns the uniform scale of a vector
const Vector2<T> operator/(T s)const
{
return Vector2<T>(x/s, y/s);
}
//the += operator adds one vector to another
const Vector2<T> &operator+=(const Vector2<T> &rightHandSide)
{
x+=rightHandSide.x;y+=rightHandSide.y;
return *this; //the resulting vector is returned
}
//the -= operator subtracts one vector from another
const Vector2<T> &operator-=(const Vector2<T> &rightHandSide)
{
x-=rightHandSide.x;y-=rightHandSide.y;
return *this; //the resulting vector is returned
}
//the *= operator scales a vector by a uniform value
const Vector2<T> &operator*=(T s)
{
x*=s;y*=s;
return *this; //the resulting vector is returned
}
//the /= operator scales a vector by a uniform value
const Vector2<T> &operator/=(T s)
{
x/=s;y/=s;
return *this; //the resulting vector is returned
}
};
#endif