Vector class

Post any maths and/or physics related questions here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Postby t i l e x » Tue Aug 30, 2005 5:28 pm

Thank you Beer Hunter, that fixed my problem. That's one weird thing :? Is there an actual reason that we have to use the 'this' pointer ?

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]
User avatar
t i l e x
 
Posts: 3604
Joined: Wed Dec 03, 2003 3:59 pm
Location: Québec (Canada)

Postby t i l e x » Fri Sep 02, 2005 2:09 pm

Hey, could you guys tell me if my function is correct ? I just learned vector product yesterday so I might still be confused a little bit.[syntax="cpp"]template <class T> Vec_3D<T>& Vec_3D<T>::VectorProduct(const Vec_3D<T>& v) {
this->x += (this->x * v.y);
this->y -= (this->x * v.z);
this->z -= (this->y * v.x);
this->x += (this->y * v.z);
this->y += (this->z * v.x);
this->x -= (this->z * v.y);
return (*this);
}[/syntax]
User avatar
t i l e x
 
Posts: 3604
Joined: Wed Dec 03, 2003 3:59 pm
Location: Québec (Canada)

Postby Animatronic » Fri Sep 02, 2005 2:35 pm

Well you got a typo on the last line (x should be z) and the maths seems off:

[syntax="cpp"] x = y*v.z - z*v.y
y = z*v.x - x*v.z
z = x*v.y - y*v.x[/syntax]

I would have a VectorProduct that returns a new vector as a result of the product and another MakeVectorProduct that makes the current vector the product.

I wouldnt have the default constructor set the elements to zero, if you want a zero'd vector the user could just do: Vector3 v(0,0,0);. If your making large arrays (e.g. 6000 elements) of vectors that default constructor could hurt performance.
Animatronic
 
Posts: 90
Joined: Wed Jun 23, 2004 9:08 pm

Postby Alvaro » Fri Sep 02, 2005 2:47 pm

I agree with everything Animatronic said.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Zen » Fri Sep 02, 2005 4:56 pm

RITZ wrote:
Zen wrote:
Alvaro wrote:I also don't understand what normal() is supposed to do.
...

Return a normalized version of the vector? (ie. length == 1)
That would me normalize. Normal, and normalize have different meanings I believe.

I guess you're right. How would you define a normal on a vector? (I guess you would need some additional parameters to make it unambigous)
User avatar
Zen
 
Posts: 1088
Joined: Wed Sep 24, 2003 1:41 am
Location: Norway

Previous

Return to Maths & Physics

Who is online

Users browsing this forum: No registered users and 2 guests