3
// OpenGL object structure
6
/***************************************************************************
8
* This program is free software; you can redistribute it and/or modify *
9
* it under the terms of the GNU General Public License as published by *
10
* the Free Software Foundation; either version 2 of the License, or *
11
* (at your option) any later version. *
13
***************************************************************************/
19
/** Default vector constructor.
20
* Vector points in the i + j + k direction (Components are < 1, 1, 1 >).
29
/** Create a new vector with specific components.
30
* Components of the new vector are < xcomp, ycomp, zcomp >.
31
* @param xcomp X component of new vector
32
* @param ycomp Y component of new vector
33
* @param zcomp Z component of new vector
35
Vector::Vector( double xcomp, double ycomp, double zcomp )
37
// Set the components of the vector
43
void Vector::glScale() const
48
void Vector::glRotate() const
50
glRotated( x, 1, 0, 0 );
51
glRotated( y, 0, 1, 0 );
52
glRotated( z, 0, 0, 1 );
55
void Vector::glNormal() const
57
glNormal3d( x, y, z );
60
void Vector::glTranslate() const
62
glTranslated( x, y, z );
65
/** Calculate the magnitude of the vector.
66
* @return The magnitude of the vector
68
double Vector::magnitude() const
70
return sqrt( x * x + y * y + z * z );
73
/** Normalize the vector.
74
* Modifies the current vector so that it's magnitude is 1.
75
* Direction remains unchanged.
77
void Vector::normalize()
79
double m = magnitude();
85
/** Multiply the vector by a scalar.
86
* Multiply each component of the vector by the scalar s.
87
* @param s The scalar to multiply the vector by
89
void Vector::scale( double s )
96
/** Compute a dot product.
97
* Dot the current vector with the vector v.
98
* @param v The vector with which to compute the dot product
99
* @return The dot product of the vector with vector v
101
double Vector::dot( Vector v ) const
103
return ( x * v.x + y * v.y + z * v.z );
106
/** Compute a vector cross product.
107
* Cross the current vector with the vector v.
108
* @param v The vector with which to compute the cross product
109
* @return The vector cross product of the vector with vector v
111
Vector Vector::cross( Vector v ) const
114
c.x = y * v.z - z * v.y;
115
c.y = -( x * v.z - z * v.x );
116
c.z = x * v.y - y * v.x;
120
Vector Vector::operator+( const Vector & rhs ) const
130
Vector Vector::operator-( const Vector & rhs ) const
140
const Vector & Vector::operator+=( const Vector & rhs )
147
const Vector & Vector::operator-=( const Vector & rhs )
154
const Vector Vector::operator*( const Vector & rhs ) const
164
const Vector Vector::operator/( const Vector & rhs ) const
174
const Vector Vector::operator*( const double & rhs ) const
182
const Vector Vector::operator/( const double & rhs ) const
185
temp.scale( 1 / rhs );
190
bool Vector::operator==( const Vector & rhs ) const
192
return ( x == rhs.x && y == rhs.y && z == rhs.z );
195
bool Vector::operator!=( const Vector & rhs ) const
197
return !operator==( rhs );