Vectors, help im stupid
I was wondering if C/C++ has a built-in vector class, like mathematical vectors (V.x, V.y, V.z, dot product, etc.), not array-like vectors.
If not, I want to write my own. I''m no good with C++, but I want to give it a try. Should I:
A) Make vectors a class and have different methods, such as dot product, in that class.
B) Make vectors a struct, and then have like functions for dot product/cross product
I have some issues with creating anything with C++ under VC++ 6.0 (which is all I have), so any help would be greatly appreciated...
---Sk
None exists. I suggest you do make your own, I did. Just do something like:
typedef struct
{
GLfloat x, y, z;
}vector;
then you can declare them as:
vector test;
test.x=1.0f;
you get the idea... if you want functions build in, make a class. But for a standard vector, a structure should be enough.
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
typedef struct
{
GLfloat x, y, z;
}vector;
then you can declare them as:
vector test;
test.x=1.0f;
you get the idea... if you want functions build in, make a class. But for a standard vector, a structure should be enough.
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Just a thought. If you aren''t great at c++, here''s how to make the class as well. Paste the following into a file called "vector.h"
#ifndef vector_h_
#define vector_h_
#include
class vector
{
public:
vector();
vector(GLfloat, GLfloat, GLfloat);
GLfloat getX();
GLfloat getY();
GLfloat getZ();
void set(GLfloat, GLfloat, GLfloat);
GLfloat* getVector();
private:
GLfloat x, y, z;
};
vector::vector() //Basic Constructor
{
x=0.0f;
y=0.0f;
z=0.0f;
}
void vector::set(GLfloat X, GLfloat Y, GLfloat Z)
{
x=X;
y=Y;
z=Z;
}
GLfloat vector::getX()
{
return x;
}
GLfloat vector::getY()
{
return y;
}
GLfloat vector::getZ()
{
return z;
}
GLfloat vector::getVector()
{
GLfloat ret[3];
ret[0]=x;
ret[1]=y;
ret[2]=z;
return ret;
}
void vector::set(GLfloat X, GLfloat Y, GLfloat Z)
{
x=X;
y=Y;
z=Z;
}
#endif
usage is as follows:
vector v;
v.set(3.0f, 1.0f, 0.0f); //Set X to 3.0f
cout << v.getX; //print x value
or:
vector v(1.0f, 2.0f, 4.0f); //create vector with initial values
cout << v.getX << " " << v.getY << " " << v.getZ;
Hope this helps!
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
#ifndef vector_h_
#define vector_h_
#include
class vector
{
public:
vector();
vector(GLfloat, GLfloat, GLfloat);
GLfloat getX();
GLfloat getY();
GLfloat getZ();
void set(GLfloat, GLfloat, GLfloat);
GLfloat* getVector();
private:
GLfloat x, y, z;
};
vector::vector() //Basic Constructor
{
x=0.0f;
y=0.0f;
z=0.0f;
}
void vector::set(GLfloat X, GLfloat Y, GLfloat Z)
{
x=X;
y=Y;
z=Z;
}
GLfloat vector::getX()
{
return x;
}
GLfloat vector::getY()
{
return y;
}
GLfloat vector::getZ()
{
return z;
}
GLfloat vector::getVector()
{
GLfloat ret[3];
ret[0]=x;
ret[1]=y;
ret[2]=z;
return ret;
}
void vector::set(GLfloat X, GLfloat Y, GLfloat Z)
{
x=X;
y=Y;
z=Z;
}
#endif
usage is as follows:
vector v;
v.set(3.0f, 1.0f, 0.0f); //Set X to 3.0f
cout << v.getX; //print x value
or:
vector v(1.0f, 2.0f, 4.0f); //create vector with initial values
cout << v.getX << " " << v.getY << " " << v.getZ;
Hope this helps!
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Oh correction.
(3.0f, 1.0f, 0.0f); //Set X to 3.0f, Y to 1.0f, Z to 0.0f
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
(3.0f, 1.0f, 0.0f); //Set X to 3.0f, Y to 1.0f, Z to 0.0f
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
If your going to use C++ go full 00 if your going to use c use a struct...I personaly like the class version myself...and this is not intended to be a flame war
-----------------------------------------------------------"People who usualy use the word pedantic usualy are pedantic!"-me
check out www.flipcode.com code of the day ..
there is a template for a vector class...
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-VectQuat&forum=cotd&id=-1
there is a template for a vector class...
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-VectQuat&forum=cotd&id=-1
May 01, 2001 12:31 AM
I don''t suppose the class I made for vector operations is the best example of elegant C++ coding and it probably could be optimised a great deal further but I get by with it. At the very least, maybe it will give you some ideas on how it can be done. The code for both the class header file and implementation file appear below. Hope they help
//-------------------tvector.h-------------------
#ifndef tvector_h
#define tvector_h
#include
class TVector
{
//private member variables
float x, y, z;
float* array;
public:
//Constructors / Deconstructor
TVector() : x(0), y(0), z(0) { array = new float[3]; }
TVector(float _x, float _y, float _z) : x(_x), y(_y), z(_z) { array = new float[3]; }
~TVector()
{
delete array;
}
//public member functions
TVector Cross(const TVector& v);
void Normalize(void);
TVector Add(const TVector& v);
float* TVector:ts(void);
TVector Subtract(const TVector& v);
void Load(float _x, float _y, float _z);
float Dot(const TVector& v);
};
#endif
//-------------------tvector.cpp-------------------
#include "tvector.h"
//Definition of TVector Member Functions
TVector TVector::Cross(const TVector& v)
{
TVector result;
result.x = y * (v.z) - z * (v.y);
result.y = -x * (v.z) - z * (v.x);
result.z = x * (v.y) - y * (v.x);
return result;
}
void TVector::Normalize(void)
{
//length (magnitude) of a vector is the square root of the sum of the squares
//of each of the vector''s components - be sure to check that length^2 isn''t = 0
float lengthXlength = x * x + y * y + z * z;
if (lengthXlength != 0.0)
{
float length = sqrt( lengthXlength );
//assign normalized values to x, y, and z
x = x / length;
y = y / length;
z = z / length;
}
}
TVector TVector::Add(const TVector& v)
{
TVector result;
result.x = x + v.x;
result.y = y + v.y;
result.z = z + v.z;
return result;
}
TVector TVector::Subtract(const TVector& v)
{
TVector result;
result.x = x - v.x;
result.y = y - v.y;
result.z = z - v.z;
return result;
}
float* TVector:ts(void)
{
*array = x;
array++;
*array = y;
array++;
*array = z;
//Restore array pointer to point to beginning of array
array = array - 2;
return (array);
}
void TVector::Load(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
float TVector::Dot(const TVector& v)
{
float result;
result = x * v.x + y * v.y + z * v.z;
return result;
}
//-------------------tvector.h-------------------
#ifndef tvector_h
#define tvector_h
#include
class TVector
{
//private member variables
float x, y, z;
float* array;
public:
//Constructors / Deconstructor
TVector() : x(0), y(0), z(0) { array = new float[3]; }
TVector(float _x, float _y, float _z) : x(_x), y(_y), z(_z) { array = new float[3]; }
~TVector()
{
delete array;
}
//public member functions
TVector Cross(const TVector& v);
void Normalize(void);
TVector Add(const TVector& v);
float* TVector:ts(void);
TVector Subtract(const TVector& v);
void Load(float _x, float _y, float _z);
float Dot(const TVector& v);
};
#endif
//-------------------tvector.cpp-------------------
#include "tvector.h"
//Definition of TVector Member Functions
TVector TVector::Cross(const TVector& v)
{
TVector result;
result.x = y * (v.z) - z * (v.y);
result.y = -x * (v.z) - z * (v.x);
result.z = x * (v.y) - y * (v.x);
return result;
}
void TVector::Normalize(void)
{
//length (magnitude) of a vector is the square root of the sum of the squares
//of each of the vector''s components - be sure to check that length^2 isn''t = 0
float lengthXlength = x * x + y * y + z * z;
if (lengthXlength != 0.0)
{
float length = sqrt( lengthXlength );
//assign normalized values to x, y, and z
x = x / length;
y = y / length;
z = z / length;
}
}
TVector TVector::Add(const TVector& v)
{
TVector result;
result.x = x + v.x;
result.y = y + v.y;
result.z = z + v.z;
return result;
}
TVector TVector::Subtract(const TVector& v)
{
TVector result;
result.x = x - v.x;
result.y = y - v.y;
result.z = z - v.z;
return result;
}
float* TVector:ts(void)
{
*array = x;
array++;
*array = y;
array++;
*array = z;
//Restore array pointer to point to beginning of array
array = array - 2;
return (array);
}
void TVector::Load(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
float TVector::Dot(const TVector& v)
{
float result;
result = x * v.x + y * v.y + z * v.z;
return result;
}
A cool thing to do is to overload the +-=/* += -= *= /= operators for your vector class. This also has the advantage of making your code that uses the vector class much easier to read.
-DeVore
ps: oh yeah and if you inline it all...
-DeVore
ps: oh yeah and if you inline it all...
So, It would seem that my C++ book has a basic vector class written in it, which I used as a template for creating my own.
I also had some help from BasKuenen''s (excuse the spelling if its wrong) page, which had his engine source, which included vector stuff.
My question is this: I''ve been reading about making functions inline in a program, and my book says its only really good when the inline code is short (1 or 2 lines).
Currently, my code is split into Vector.cpp and Vector.h, with nothing declared as INLINE. BasKuenen''s code, on the other hand, is all in a file called Vector.h, and he has everything in there, including function definitions, although I don''t believe any of his stuff is declared INLINE either, but he says on his page that its all inline. Does that mean that when you declared everything in the .h file that it makes it all inline? Im confused because it doesn''t seem to follow the .cpp and .h file coding conventions (.h has prototypes, .cpp has definitions).
Should I do like BasKuenen did and put it all in the .h file, or will that make very little difference?
I also had some help from BasKuenen''s (excuse the spelling if its wrong) page, which had his engine source, which included vector stuff.
My question is this: I''ve been reading about making functions inline in a program, and my book says its only really good when the inline code is short (1 or 2 lines).
Currently, my code is split into Vector.cpp and Vector.h, with nothing declared as INLINE. BasKuenen''s code, on the other hand, is all in a file called Vector.h, and he has everything in there, including function definitions, although I don''t believe any of his stuff is declared INLINE either, but he says on his page that its all inline. Does that mean that when you declared everything in the .h file that it makes it all inline? Im confused because it doesn''t seem to follow the .cpp and .h file coding conventions (.h has prototypes, .cpp has definitions).
Should I do like BasKuenen did and put it all in the .h file, or will that make very little difference?
The compiler will inline code wherever it think its suitable. And it will also skip inlining a function even if it is declared inline if it don't think it is suitable to do so.
I think baskuenen is talking about including. Including a header file is more or less the sam thing to pasting the code in the header file before the code in the c/cpp file.
I use templates in my vector class since I use it in situations where I need high precision and situations where I don't. I can do like this Vector for highprecision enviroments and Vector everywhere else or even Vector in some places to get slightly higher performance.
this is my vector class in it's current state (not completely finished)
Snale
+--My humble and superior homepage
Edited by - snale on May 1, 2001 6:34:38 PM
I think baskuenen is talking about including. Including a header file is more or less the sam thing to pasting the code in the header file before the code in the c/cpp file.
I use templates in my vector class since I use it in situations where I need high precision and situations where I don't. I can do like this Vector for highprecision enviroments and Vector everywhere else or even Vector in some places to get slightly higher performance.
this is my vector class in it's current state (not completely finished)
#ifndef __VECTOR_H__#define __VECTOR_H__#include <math.h>#include <fstream.h>namespace l3d{ template<class _Ty = float> class Vector { public: //-Variables--------------------------------------------- _Ty x, y, z; //-Constructors------------------------------------------ Vector( _Ty a, _Ty b, _Ty c ) { x = a; y = b; z = c; } Vector() { x = 0; y = 0; z = 0; } Vector( const Vector &V ) { x = V.x; y = V.y; z = V.z; } //-Operators--------------------------------------------- void operator=( const Vector &U ) { x = U.x; y = U.y; z = U.z; } void operator=( const _Ty array[3] ) { x = array[0]; y = array[1]; z = array[2]; } //-Member Functions-------------------------------------- double length() { return sqrt( pow( x, 2) + pow( y, 2 ) + pow( z, 2 ) ); } // Calculates the length of the vector. //-Operators--------------------------------------------- //Cross product////////////////////////// friend Vector operator*( Vector v1, Vector v2 ) { return Vector<_Ty>( v1.y * v2.z - v1.z * v2.y, v1.x * v2.z - v1.z * v2.x, v1.x * v2.y - v1.y * v2.x ); } //Scale////////////////////////////////// friend Vector operator*( float s, Vector &v ) { return Vector<_Ty>( v.x * s, v.y * s, v.z * s ); } friend Vector operator*( Vector &v, float s ) { return Vector<_Ty>( v.x * s, v.y * s, v.z * s ); } //Inv Scale////////////////////////////// friend Vector operator/( Vector &v, float s ) { return Vector<_Ty>( v.x / s, v.y / s, v.z / s ); } friend Vector operator/( float s, Vector &v ) { return Vector<_Ty>( s / v.x, s / v.y, s / v.z ); } //Dot product//////////////////////////// friend float operator^( Vector v1, Vector v2 ) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } //Add//////////////////////////////////// friend Vector operator+( Vector v1, Vector v2 ) { return Vector<_Ty>( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ); } //Subtract/////////////////////////////// friend Vector operator-( Vector v1, Vector v2 ) { return Vector<_Ty>( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ); } //Equal////////////////////////////////// friend bool operator==( Vector &v1, Vector &v2 ) { return ( v1.x == v2.x && v1.y == v2.y && v1.z == v2.z ) ? true : false; } //Read/////////////////////////////////// friend istream& operator>>( istream ∈, Vector &v ) { _Ty val[3]; in.read( (char *)val, 3 * sizeof(_Ty) ); v.x = val[0]; v.y = val[1]; v.z = val[2]; return in; } friend ostream& operator<<( ostream &out, Vector &v ) { out.write( (char *)&v.x, sizeof( v.x ) ); out.write( (char *)&v.y, sizeof( v.y ) ); out.write( (char *)&v.z, sizeof( v.z ) ); return out; } }; typedef Vector<> VECTOR; typedef Vector<double> VECTOR_D; typedef Vector<int> VECTOR_I;}#endif
Snale
+--My humble and superior homepage
Edited by - snale on May 1, 2001 6:34:38 PM
Snale+--My humble and superior homepage
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement