Edit: Here's the latest version of my vector classes. Do you guys have any suggestion(s)/comment(s) ?[syntax="cpp"]#ifndef _VECTOR_H_INCLUDED_
#define _VECTOR_H_INCLUDED_
#include <cmath>
/*
class declarations
*/
template <class T> class IVec {
public:
IVec( ) : x(0), y(0) { }
IVec(T _x, T _y) : x(_x), y(_y) { }
virtual void Normalize( ) = 0;
virtual T Magnitude( ) const = 0;
virtual T SquaredMagnitude( ) const = 0;
protected:
T x, y;
};
template <class T> class Vec_2D : public IVec<T> {
public:
Vec_2D( ) : IVec<T>( ) { };
Vec_2D(T _x, T _y) : IVec<T>(_x, _y) { }
Vec_2D(const Vec_2D<T>& v) { this->x = v.x; this->y = v.y; }
virtual void Normalize( );
virtual T Magnitude( ) const;
virtual T SquaredMagnitude( ) const;
float ScalarProduct(const Vec_2D<T>&);
Vec_2D& operator/(float);
Vec_2D& operator/=(float);
Vec_2D& operator*(float);
Vec_2D& operator+(const Vec_2D<T>&);
Vec_2D& operator-(const Vec_2D<T>&);
Vec_2D& operator=(const Vec_2D<T>&);
};
template <class T> class Vec_3D : public IVec<T> {
public:
Vec_3D( ) : IVec<T>( ), z(0) { }
Vec_3D(T _x, T _y, T _z) : IVec<T>(_x, _y), z(_z) { }
Vec_3D(const Vec_3D<T>& v) { this->x = v.x; this->y = v.y; this->z = v.z; }
virtual void Normalize( );
virtual T Magnitude( ) const;
virtual T SquaredMagnitude( ) const;
float ScalarProduct(const Vec_3D<T>&);
Vec_3D& operator/(float);
Vec_3D& operator/=(float);
Vec_3D& operator*(float);
Vec_3D& operator+(const Vec_3D<T>&);
Vec_3D& operator-(const Vec_3D<T>&);
Vec_3D& operator=(const Vec_3D<T>&);
private:
T z;
};
/*
definition of Vec_2D's members
*/
template <class T> void Vec_2D<T>::Normalize( ) {
float Length = Magnitude( );
(*this) /= Length;
}
template <class T> T Vec_2D<T>::Magnitude( ) const {
return (T)(sqrt(SquaredMagnitude( )));
}
template <class T> T Vec_2D<T>::SquaredMagnitude( ) const {
return (T)((pow(this->x,2) + pow(this->y,2)));
}
template <class T> float Vec_2D<T>::ScalarProduct(const Vec_2D<T>& v) {
return (this->x * v.x + this->y * v.y);
}
template <class T> Vec_2D<T>& Vec_2D<T>::operator/(float scalar) {
try {
if(scalar == 0) {
throw ;
}
this->x /= scalar;
this->y /= scalar;
}
catch(...) {
scalar = 1;
throw ;
}
return (*this);
}
template <class T> Vec_2D<T>& Vec_2D<T>::operator/=(float scalar) {
(*this) /= scalar;
return (*this);
}
template <class T> Vec_2D<T>& Vec_2D<T>::operator*(float scalar) {
this->x *= scalar;
this->y *= scalar;
return (*this);
}
template <class T> Vec_2D<T>& Vec_2D<T>::operator+(const Vec_2D<T>& v) {
this->x += v.x;
this->y += v.y;
return (*this);
}
template <class T> Vec_2D<T>& Vec_2D<T>::operator-(const Vec_2D<T>& v) {
this->x -= v.x;
this->y -= v.y;
return (*this);
}
template <class T> Vec_2D<T>& Vec_2D<T>::operator=(const Vec_2D<T>& v) {
this->x = v.x;
this->y = v.y;
return (*this);
}
/*
definition of Vec_3D's members
*/
template <class T> void Vec_3D<T>::Normalize( ) {
float Length = Magnitude( );
(*this) /= Length;
}
template <class T> T Vec_3D<T>::Magnitude( ) const {
return (T)(sqrt(SquaredMagnitude( )));
}
template <class T> T Vec_3D<T>::SquaredMagnitude( ) const {
return (T)(pow(this->x,2) + pow(this->y,2) + pow(this->z,2));
}
template <class T> float Vec_3D<T>::ScalarProduct(const Vec_3D<T>& v) {
return (this->x * v.x + this->y * v.y + this->z * v.z);
}
template <class T> Vec_3D<T>& Vec_3D<T>::operator/(float scalar) {
try {
if(scalar == 0) {
throw ;
}
this->x /= scalar;
this->y /= scalar;
this->z /= scalar;
}
catch(...) {
scalar = 1;
throw ;
}
return (*this);
}
template <class T> Vec_3D<T>& Vec_3D<T>::operator/=(float scalar) {
(*this) = (*this) / scalar;
return (*this);
}
template <class T> Vec_3D<T>& Vec_3D<T>::operator*(float scalar) {
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
return (*this);
}
template <class T> Vec_3D<T>& Vec_3D<T>::operator+(const Vec_3D<T>& v) {
this->x += v.x;
this->y += v.y;
this->z += v.z;
return (*this);
}
template <class T> Vec_3D<T>& Vec_3D<T>::operator-(const Vec_3D<T>& v) {
this->x -= v.x;
this->y -= v.y;
this->z -= v.z;
return (*this);
}
template <class T> Vec_3D<T>& Vec_3D<T>::operator=(const Vec_3D<T>& v) {
this->x = v.x;
this->y = v.y;
this->z = v.z;
return (*this);
}
#endif[/syntax]
