1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
// gelvector.h
// GELVector
// OpenGL object structure
// by Joe Flint
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "vector.h"
#include <math.h>
#include "glhead.h"
/** Default vector constructor.
* Vector points in the i + j + k direction (Components are < 1, 1, 1 >).
*/
Vector::Vector()
{
x = 1.0;
y = 1.0;
z = 1.0;
}
/** Create a new vector with specific components.
* Components of the new vector are < xcomp, ycomp, zcomp >.
* @param xcomp X component of new vector
* @param ycomp Y component of new vector
* @param zcomp Z component of new vector
*/
Vector::Vector( double xcomp, double ycomp, double zcomp )
{
// Set the components of the vector
x = xcomp;
y = ycomp;
z = zcomp;
}
void Vector::glScale() const
{
glScaled( x, y, z );
}
void Vector::glRotate() const
{
glRotated( x, 1, 0, 0 );
glRotated( y, 0, 1, 0 );
glRotated( z, 0, 0, 1 );
}
void Vector::glNormal() const
{
glNormal3d( x, y, z );
}
void Vector::glTranslate() const
{
glTranslated( x, y, z );
}
/** Calculate the magnitude of the vector.
* @return The magnitude of the vector
*/
double Vector::magnitude() const
{
return sqrt( x * x + y * y + z * z );
}
/** Normalize the vector.
* Modifies the current vector so that it's magnitude is 1.
* Direction remains unchanged.
*/
void Vector::normalize()
{
double m = magnitude();
x /= m;
y /= m;
z /= m;
}
/** Multiply the vector by a scalar.
* Multiply each component of the vector by the scalar s.
* @param s The scalar to multiply the vector by
*/
void Vector::scale( double s )
{
x *= s;
y *= s;
z *= s;
}
/** Compute a dot product.
* Dot the current vector with the vector v.
* @param v The vector with which to compute the dot product
* @return The dot product of the vector with vector v
*/
double Vector::dot( Vector v ) const
{
return ( x * v.x + y * v.y + z * v.z );
}
/** Compute a vector cross product.
* Cross the current vector with the vector v.
* @param v The vector with which to compute the cross product
* @return The vector cross product of the vector with vector v
*/
Vector Vector::cross( Vector v ) const
{
Vector c;
c.x = y * v.z - z * v.y;
c.y = -( x * v.z - z * v.x );
c.z = x * v.y - y * v.x;
return c;
}
Vector Vector::operator+( const Vector & rhs ) const
{
Vector temp;
temp.x = x + rhs.x;
temp.y = y + rhs.y;
temp.z = z + rhs.z;
return temp;
}
Vector Vector::operator-( const Vector & rhs ) const
{
Vector temp;
temp.x = x - rhs.x;
temp.y = y - rhs.y;
temp.z = z - rhs.z;
return temp;
}
const Vector & Vector::operator+=( const Vector & rhs )
{
*this = *this + rhs;
return *this;
}
const Vector & Vector::operator-=( const Vector & rhs )
{
*this = *this - rhs;
return *this;
}
const Vector Vector::operator*( const Vector & rhs ) const
{
Vector temp = *this;
temp.x *= rhs.x;
temp.y *= rhs.y;
temp.z *= rhs.z;
return temp;
}
const Vector Vector::operator/( const Vector & rhs ) const
{
Vector temp = *this;
temp.x /= rhs.x;
temp.y /= rhs.y;
temp.z /= rhs.z;
return temp;
}
const Vector Vector::operator*( const double & rhs ) const
{
Vector temp = *this;
temp.scale( rhs );
return temp;
}
const Vector Vector::operator/( const double & rhs ) const
{
Vector temp = *this;
temp.scale( 1 / rhs );
return temp;
}
bool Vector::operator==( const Vector & rhs ) const
{
return ( x == rhs.x && y == rhs.y && z == rhs.z );
}
bool Vector::operator!=( const Vector & rhs ) const
{
return !operator==( rhs );
}
|