The Vector3 class represents a 3D vector. It can be used for direction vectors as well as point positions.
[Several of the ideas for features included in Vector3 were borrowed from OpenGL Performer's pfVec3 class and Magic Software's MgcVector3 class.]
#include <dms/Vector3.h> GLfloat vec[3] Vector3(void) Vector3(GLfloat x, GLfloat y, GLfloat z) Vector3(const GLfloat *v) Vector3(const Vector3 &v) Vector3& operator= (const Vector3& v) void set(GLfloat x, GLfloat y, GLfloat z) GLfloat& operator[](int i) GLfloat operator[](int i) const Vector3& operator+=(const Vector3& v) Vector3& operator-=(const Vector3& v) Vector3& operator*=(GLfloat s) Vector3& operator/=(GLfloat s) Vector3 operator+(const Vector3& v) const Vector3 operator-(const Vector3& v) const Vector3 operator*(GLfloat s) const Vector3 operator/(GLfloat s) const Vector3 operator-(void) bool operator==(const Vector3& v) const bool operator!=(const Vector3& v) const GLfloat length(void) const GLfloat lengthSquared(void) const void normalize(void) GLfloat dot(const Vector3& v) const Vector3 cross(const Vector3& v) const GLfloat distance(const Vector3& v) const GLfloat distanceSquared(const Vector3& v) const void drawLine(void) const static const Vector3 Zero static const Vector3 X_Axis static const Vector3 Y_Axis static const Vector3 Z_Axis ostream& operator<< (ostream& s, const Vector3& v)
GLfloat vec[3]
The values of the vector are stored in the publically accessible member variable vec[]. This variable can be used when passing vector data to a function that expects a GLfloat array. e.g.:
Vector3 position(1,0,2); glVector3fv(position.vec);
Vector3(void) Vector3(GLfloat x, GLfloat y, GLfloat z) Vector3(const GLfloat *v) Vector3(const Vector3 &v)
The default constructor (with no arguments) creates the vector (0,0,0). The other constructors provide ways to assign a vector an initial value. e.g.:
Vector3 vec(1,0,2); Vector3 vecCopy(vec);
Vector3& operator= (const Vector3& v)
The assignment operator copies one vector to another. e.g.:
Vector3 vec(1,0,2); Vector3 vecCopy; vecCopy = vec;
void set(GLfloat x, GLfloat y, GLfloat z)
set() assigns new values to the vector. e.g.:
Vector3 vec; vec.set(1,0,2);
GLfloat& operator[](int i) GLfloat operator[](int i) const
The [] operator can be used to access individual elements of a vector, like an ordinary array. e.g.:
Vector3 vec; x = vec[0]; vec[2] = 17.5;
Vector3& operator+=(const Vector3& v) Vector3& operator-=(const Vector3& v) Vector3 operator+(const Vector3& v) const Vector3 operator-(const Vector3& v) const
The addition and subtraction operators add or subtract two vectors element by element. That is, (x0, y0, z0) + (x1, y1, z1) yields (x0+x1, y0+y1, z0+z1). e.g.:
vec3 = vec1 + vec2; vec1 -= vec2;
Vector3& operator*=(GLfloat s) Vector3& operator/=(GLfloat s) Vector3 operator*(GLfloat s) const Vector3 operator/(GLfloat s) const
The multiplication and division operators multiply/divide a vector by a scalar. That is, each element of the vector is multiplied or divided by the single value s; (x, y, z) * s yields (x*s, y*s, z*s). e.g.:
vec *= 2; vec2 = vec1 / 3.14;
Vector3 operator-(void)
The unary minus operator returns the negative of a vector.
Example:
Vector3 direction, oppositeDirection; oppositeDirection = -direction;
bool operator==(const Vector3& v) const bool operator!=(const Vector3& v) const
The equality operator compares two vectors element by element, and returns true if and only if all three elements of the two are equal. The inequality operator returns true if any of the elements are not equal. e.g.:
if (vec1 == vec2) printf("vec1 and vec2 are the same\n");
GLfloat length(void) const GLfloat lengthSquared(void) const
length() returns the length of a vector. lengthSquared() returns the square of the length of a vector, which avoids the square-root operation necessary for length(), and is sufficient for many operations. e.g.:
len = vec.length(); if (vec1.lengthSquared() > vec2.lengthSquared()) printf("vec1 is longer than vec2\n");
void normalize(void)
Scales a vector so that its length is 1. If the input vector is (0,0,0), nothing is done. e.g.:
Vector3 vec(1, 0, 1); vec.normalize();
will cause vec to take the value (0.707, 0, 0.707).
GLfloat dot(const Vector3& v) const
Returns the dot product of two vectors. e.g.:
GLfloat result = vec1.dot(vec2);
Vector3 cross(const Vector3& v) const
Returns the cross product of two vectors. e.g.:
Vector3 vec = vec1.cross(vec2);
GLfloat distance(const Vector3& v) const GLfloat distanceSquared(const Vector3& v) const
distance(v) returns the distance between two vectors, treating them as 3D points rather than direction vectors. distanceSquared(v) returns the square of the distance, avoiding the need for a square root. e.g.:
d = point1.distance(point2);
void drawLine(void) const
Draws an OpenGL line representing the vector. The line goes from (0,0,0) to (vec[0], vec[1], vec[2]). e.g.:
glColor3f(1.0, 0.0, 0.0); vec.drawLine();
static const Vector3 Zero static const Vector3 X_Axis static const Vector3 Y_Axis static const Vector3 Z_Axis
Some constant vectors are defined for convenience. Zero is the vector (0,0,0). X_Axis is the vector (1,0,0). Y_Axis is the vector (0,1,0). Z_Axis is the vector (0,0,1). e.g.:
Vector3::X_Axis.drawLine();
ostream& operator<< (ostream& s, const Vector3& v)
The standard C++ output operator << is overloaded to print vectors. e.g.:
Vector3 vec1(1,2.5,0); cout << "Vector1 = " << vec1 << endl;
would produce the output:
Vector1 = (1, 2.5, 0)