dms::Vector4

The Vector4 class represents a 4D vector. It can be used for RGBA colors, or for points with a W component.

Source Code

Class Specification

#include <dms/Vector4.h>

GLfloat vec[4]

Vector4(void)
Vector4(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
Vector4(const GLfloat *v)
Vector4(const Vector4 &v)

Vector4& operator= (const Vector4& v)
void set(GLfloat x, GLfloat y, GLfloat z, GLfloat w)

GLfloat& operator[](int i)
GLfloat operator[](int i) const

Vector4& operator+=(const Vector4& v)
Vector4& operator-=(const Vector4& v)
Vector4& operator*=(GLfloat s)
Vector4& operator/=(GLfloat s)
Vector4 operator+(const Vector4& v) const
Vector4 operator-(const Vector4& v) const
Vector4 operator*(GLfloat s) const
Vector4 operator/(GLfloat s) const
Vector4 operator-(void)
bool operator==(const Vector4& v) const
bool operator!=(const Vector4& v) const

GLfloat length(void) const
GLfloat lengthSquared(void) const
void normalize(void)
GLfloat dot(const Vector4& v) const
GLfloat distance(const Vector4& v) const
GLfloat distanceSquared(const Vector4& v) const

static const Vector4 Zero

ostream& operator<< (ostream& s, const Vector4& v)

Description

GLfloat vec[4]

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.:

Vector4 translucentRed(1,0,0,0.5);
glMaterialfv(GL_FRONT, GL_DIFFUSE, translucentRed.vec);
Vector4(void)
Vector4(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
Vector4(const GLfloat *v)
Vector4(const Vector4 &v)

The default constructor (with no arguments) creates the vector (0,0,0,0). The other constructors provide ways to assign a vector an initial value. e.g.:

Vector4 vec(1,0,2,0);
Vector4 vecCopy(vec);
Vector4& operator= (const Vector4& v)

The assignment operator copies one vector to another. e.g.:

Vector4 vec(1,0,2,0);
Vector4 vecCopy;
vecCopy = vec;
void set(GLfloat x, GLfloat y, GLfloat z, GLfloat w)

set() assigns new values to the vector. e.g.:

Vector4 vec;
vec.set(1,0,2,0);
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.:

Vector4 vec;
x = vec[0];
vec[3] = 1;
Vector4& operator+=(const Vector4& v)
Vector4& operator-=(const Vector4& v)
Vector4 operator+(const Vector4& v) const
Vector4 operator-(const Vector4& v) const

The addition and subtraction operators add or subtract two vectors element by element. That is, (x0, y0, z0, w0) + (x1, y1, z1, w1) yields (x0+x1, y0+y1, z0+z1, w0+w1). e.g.:

vec3 = vec1 + vec2;
vec1 -= vec2;
Vector4& operator*=(GLfloat s)
Vector4& operator/=(GLfloat s)
Vector4 operator*(GLfloat s) const
Vector4 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, w) * s yields (x*s, y*s, z*s, w*s). e.g.:

vec *= 2;
vec2 = vec1 / 3.14;
Vector4 operator-(void)

The unary minus operator returns the negative of a vector.
Example:

Vector4 direction, oppositeDirection;

oppositeDirection = -direction;
bool operator==(const Vector4& v) const
bool operator!=(const Vector4& v) const

The equality operator compares two vectors element by element, and returns true if and only if all four 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,0), nothing is done. e.g.:

Vector4 vec(1, 0, 1, 1);
vec.normalize();

will cause vec to take the value (0.57735, 0, 0.57735, 0.57735).

GLfloat dot(const Vector4& v) const

Returns the dot product of two vectors. e.g.:

GLfloat result = vec1.dot(vec2);
GLfloat distance(const Vector4& v) const
GLfloat distanceSquared(const Vector4& v) const

distance(v) returns the distance between two vectors, treating them as 4D 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);
static const Vector4 Zero

A constant vector defined for convenience; Zero is the vector (0,0,0,0).

ostream& operator<< (ostream& s, const Vector4& v)

The standard C++ output operator << is overloaded to print vectors. e.g.:

Vector4 vec1(1,2.5,0,0);
cout << "Vector1 = " << vec1 << endl;

would produce the output:

 Vector1 = (1, 2.5, 0, 0)