CUGL  2.0
Public Member Functions | Private Attributes | Friends | List of all members
cugl::Vector Class Reference

#include <cugl.h>

Public Member Functions

 Vector ()
 
 Vector (GLfloat x, GLfloat y, GLfloat z)
 
 Vector (GLfloat coordinates[])
 
 Vector (Point points[], int numPoints)
 
 Vector (const Point &p, const Point &q)
 
 Vector (const Quaternion &q)
 
Vector operator+= (const Vector &v)
 
Vector operator-= (const Vector &v)
 
Vector operator- () const
 
Vector operator*= (GLfloat scale)
 
Vector operator/ (GLfloat scale) const
 
Vector operator/= (GLfloat scale)
 
void normalize ()
 
Vector unit () const
 
GLfloat norm () const
 
GLfloat length () const
 
void translate () const
 
void drawNormal () const
 
Matrix skew ()
 
void draw (const Point &p=Point()) const
 
GLfloat & operator[] (int i)
 
const GLfloat & operator[] (int i) const
 

Private Attributes

GLfloat x
 
GLfloat y
 
GLfloat z
 

Friends

class Point
 
class Matrix
 
class Quaternion
 
Vector cross (const Vector &u, const Vector &v)
 
Vector operator* (const Vector &u, const Vector &v)
 
GLfloat dot (const Vector &u, const Vector &v)
 
Vector operator* (const Vector &v, GLfloat s)
 
Vector operator* (GLfloat s, const Vector &v)
 
Vector operator+ (const Vector &u, const Vector &v)
 
Vector operator- (const Vector &u, const Vector &v)
 
Point operator+ (const Vector &v, const Point &p)
 
Point operator+ (const Point &p, const Vector &v)
 
bool operator== (const Vector &x, const Vector &y)
 
bool operator!= (const Vector &x, const Vector &y)
 
std::ostream & operator<< (std::ostream &os, const Vector &v)
 

Detailed Description

An instance is a vector with 3 real components. A vector v is a 3-vector represented by three orthogonal components vx, vy, and vz. The norm of the vector v is vx*vx+vy*vy+vz*vz. The length of v is sqrt(norm(v)).

Definition at line 612 of file cugl.h.

Constructor & Destructor Documentation

◆ Vector() [1/6]

cugl::Vector::Vector ( )
inline

Construct the zero vector: (0,0,0).

Definition at line 622 of file cugl.h.

Referenced by operator-().

◆ Vector() [2/6]

cugl::Vector::Vector ( GLfloat  x,
GLfloat  y,
GLfloat  z 
)
inline

Construct the vector (x,y,z).

Definition at line 628 of file cugl.h.

◆ Vector() [3/6]

cugl::Vector::Vector ( GLfloat  coordinates[])
inline

Construct a vector from the array coordinates.

Precondition
The array must have at least three components.

Definition at line 2743 of file cugl.h.

References x, y, and z.

◆ Vector() [4/6]

cugl::Vector::Vector ( Point  points[],
int  numPoints 
)

Construct a vector normal to the polygon defined by the given points using Martin Newell's algorithm. The normal vector will be exact if the points lie in a plane, otherwise it will be a sort of average value. As with OpenGL, the vector will point in the direction from which the points are enumerated in a counter-clockwise direction.

Unlike other functions, this function does not use homogeneous coordinates. The points are assumed to have (x,y,z) coordinates; the w component is ignored.

Parameters
pointsis an array of points.
numPointsis the number of points in the array.
Returns
the vector normal to the plane defined by points.
Note
The vector is not a unit vector because it will probably be averaged with other vectors.

◆ Vector() [5/6]

cugl::Vector::Vector ( const Point p,
const Point q 
)
inline

Construct a vector from two points. Vector(p, q) is equivalent to p - q.

Definition at line 2750 of file cugl.h.

References cugl::Point::w, cugl::Point::x, x, cugl::Point::y, y, cugl::Point::z, and z.

◆ Vector() [6/6]

cugl::Vector::Vector ( const Quaternion q)
inlineexplicit

Construct a vector from a quaternion by ignoring the scalar component of the quaternion. Vector(q) constructs the vector returned by q.vector().

Parameters
qis the Quaternion whose vector component is to be used.

Definition at line 2766 of file cugl.h.

References cugl::Quaternion::v, x, y, and z.

Member Function Documentation

◆ draw()

void cugl::Vector::draw ( const Point p = Point()) const
inline

Draw this vector as a line in the graphics window. The line joins P and P+V, where P is the point provided, or the origin if no point is given.

Parameters
pis the start point for the vector.

Definition at line 2833 of file cugl.h.

References cugl::Point::draw().

◆ drawNormal()

void cugl::Vector::drawNormal ( ) const
inline

Use this vector as a normal vector when drawing a surface. This is implemented by passing the vector to glNormal().

Definition at line 2828 of file cugl.h.

References x, y, and z.

◆ length()

GLfloat cugl::Vector::length ( ) const
inline

Return the length of this vector. The length of a vector is the square root of its norm.

Returns
the length of this vector.

Definition at line 2809 of file cugl.h.

References norm().

◆ norm()

GLfloat cugl::Vector::norm ( ) const
inline

Return the norm of this vector. The norm of a vector is the sum of its squared components and is also the square of the length.

Returns
the norm of this vector.

Definition at line 2804 of file cugl.h.

References x, y, and z.

Referenced by length(), and cugl::Quaternion::norm().

◆ normalize()

void cugl::Vector::normalize ( )

Normalize this vector. See also Vector::unit(). Reports error ZERO_NORM if the vector is zero.

Note
The value of this vector is changed by this operation.

◆ operator*=()

Vector cugl::Vector::operator*= ( GLfloat  scale)
inline

Multiply each component of the vector by scale.

Definition at line 2796 of file cugl.h.

References x, y, and z.

◆ operator+=()

Vector cugl::Vector::operator+= ( const Vector v)
inline

Add vector v to this vector.

Definition at line 2775 of file cugl.h.

References x, y, and z.

◆ operator-()

Vector cugl::Vector::operator- ( ) const
inline

Return Vector (-x,-y,-z).

Definition at line 2791 of file cugl.h.

References Vector(), x, y, and z.

◆ operator-=()

Vector cugl::Vector::operator-= ( const Vector v)
inline

Subtract vector v from this vector.

Definition at line 2783 of file cugl.h.

References x, y, and z.

◆ operator/()

Vector cugl::Vector::operator/ ( GLfloat  scale) const

Return the vector (x/scale,y/scale,z/scale). Error ZERO_DIVISOR reported if scale is zero.

Returns
the Vector (x/scale,y/scale,z/scale).

◆ operator/=()

Vector cugl::Vector::operator/= ( GLfloat  scale)

Divide each component of this vector by scale and return the new value. Error ZERO_DIVISOR reported if scale is zero.

◆ operator[]() [1/2]

GLfloat& cugl::Vector::operator[] ( int  i)

Get or set a reference to a component of this vector. v[0] is the x component; v[1] is the y component; v[2] is the z component. Error BAD_INDEX reported if i is not one of 0, 1, 2.

◆ operator[]() [2/2]

const GLfloat& cugl::Vector::operator[] ( int  i) const

Get a reference to a component of this vector. This form is used for const instances (v[i] cannot appear on the left of =). v[0] is the x component; v[1] is the y component; v[2] is the z component. Error BAD_INDEX reported if i is not one of 0, 1, 2.

◆ skew()

Matrix cugl::Vector::skew ( )
inline

Construct the skew-symmetric matrix corresponding to this vector.

Definition at line 2819 of file cugl.h.

References x, y, and z.

◆ translate()

void cugl::Vector::translate ( ) const
inline

Use this vector to translate an object. This is implemented by passing the vector to glTranslate().

Definition at line 2814 of file cugl.h.

References x, y, and z.

◆ unit()

Vector cugl::Vector::unit ( ) const

Return a unit vector with the same direction as this vector. See also Vector::normalize(). Reports error ZERO_NORM if the vector is zero.

Note
The value of this vector is not changed by this operation.

Referenced by cugl::Quaternion::axis(), and cugl::Plane::normal().

Friends And Related Function Documentation

◆ cross

Vector cross ( const Vector u,
const Vector v 
)
friend

Return cross product of vectors u and v.

Definition at line 2871 of file cugl.h.

◆ dot

GLfloat dot ( const Vector u,
const Vector v 
)
friend

Return dot product of vectors u and v.

Definition at line 2887 of file cugl.h.

◆ operator!=

bool operator!= ( const Vector x,
const Vector y 
)
friend

Compare two vectors. This function returns false only if corresponding components are exactly equal. Values that are theoretically equal but computed in different ways are likely to be unequal according to this function.

Definition at line 2897 of file cugl.h.

◆ operator* [1/3]

Vector operator* ( const Vector u,
const Vector v 
)
friend

Return cross product of vectors u and v.

Definition at line 2879 of file cugl.h.

◆ operator* [2/3]

Vector operator* ( const Vector v,
GLfloat  s 
)
friend

Return Vector s*v.

Definition at line 2861 of file cugl.h.

◆ operator* [3/3]

Vector operator* ( GLfloat  s,
const Vector v 
)
friend

Return Vector s*v.

Definition at line 2866 of file cugl.h.

◆ operator+ [1/3]

Point operator+ ( const Point p,
const Vector v 
)
friend

Return Point p displaced by vector v.

Displace a Point with a Vector. The components of the Vector are added to the corresponding components of the Point. If the Point is not in normal form, the Vector is implicitly scaled.

Returns
Point at (p.x+p.w*v.x, p.y+p.w*v.y, p.z+p.w*v.z, p.w).

Definition at line 2655 of file cugl.h.

◆ operator+ [2/3]

Vector operator+ ( const Vector u,
const Vector v 
)
friend

Return Vector u+v.

Definition at line 2851 of file cugl.h.

◆ operator+ [3/3]

Point operator+ ( const Vector v,
const Point p 
)
friend

Displace a Point with a Vector.

Returns
Point at (p.x+p.w*v.x, p.y+p.w*v.y, p.z+p.w*v.z, p.w).

Displace a Point with a Vector. The components of the Vector are added to the corresponding components of the Point. If the Point is not in normal form, the Vector is implicitly scaled.

Returns
Point at (p.x+p.w*v.x, p.y+p.w*v.y, p.z+p.w*v.z, p.w).

Definition at line 2660 of file cugl.h.

◆ operator-

Vector operator- ( const Vector u,
const Vector v 
)
friend

Return Vector u-v.

Definition at line 2856 of file cugl.h.

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Vector v 
)
friend

Write vector to output stream as (x,y,z). Inserts the components of the vector into the output stream os in the format (x,y,z). The current settings of the stream formatting parameters are used. If a width is specified with setw(), it is applied to each coordinate, not to the vector as a whole.

◆ operator==

bool operator== ( const Vector x,
const Vector y 
)
friend

Compare two vectors. This function returns true only if corresponding components are exactly equal. Values that are theoretically equal but computed in different ways are likely to be unequal according to this function.

Definition at line 2892 of file cugl.h.

Member Data Documentation

◆ x

GLfloat cugl::Vector::x
private

◆ y

GLfloat cugl::Vector::y
private

◆ z

GLfloat cugl::Vector::z
private

The documentation for this class was generated from the following file: