dms::Vector2

The Vector2 class represents a 2D vector. It can be used for direction vectors as well as point positions.

Source Code

Class Specification

#include <dms/Vector2.h>

GLfloat vec[2]

Vector2(void)
Vector2(GLfloat x, GLfloat y)
Vector2(const GLfloat *v)
Vector2(const Vector2 &v)

Vector2& operator= (const Vector2& v)
void set(GLfloat x, GLfloat y)

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

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

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

void drawLine(void) const

static const Vector2 Zero
static const Vector2 X_Axis
static const Vector2 Y_Axis

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

Description

GLfloat vec[2]

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

Vector2 position(1,3);
glRasterPos2fv(position.vec);
Vector2(void)
Vector2(GLfloat x, GLfloat y)
Vector2(const GLfloat *v)
Vector2(const Vector2 &v)

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

Vector2 vec(1,3);
Vector2 vecCopy(vec);
Vector2& operator= (const Vector2& v)

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

Vector2 vec(1,3);
Vector2 vecCopy;
vecCopy = vec;
void set(GLfloat x, GLfloat y)

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

Vector2 vec;
vec.set(1,3);
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.:

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

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

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

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

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

Vector2 direction, oppositeDirection;

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

The equality operator compares two vectors element by element, and returns true if and only if both elements of the two are equal. The inequality operator returns true if either 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), nothing is done. e.g.:

Vector2 vec(1, 1);
vec.normalize();

will cause vec to take the value (0.707, 0.707).

GLfloat dot(const Vector2& v) const

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

GLfloat result = vec1.dot(vec2);
GLfloat distance(const Vector2& v) const
GLfloat distanceSquared(const Vector2& 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) to (vec[0], vec[1]). e.g.:

glColor3f(1.0, 0.0, 0.0);
vec.drawLine();
static const Vector2 Zero
static const Vector2 X_Axis
static const Vector2 Y_Axis

Some constant vectors are defined for convenience. Zero is the vector (0,0). X_Axis is the vector (1,0). Y_Axis is the vector (0,1). e.g.:

Vector2::X_Axis.drawLine();
ostream& operator<< (ostream& s, const Vector2& v)

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

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

would produce the output:

 Vector1 = (1, 2.5)