~ubuntu-branches/ubuntu/utopic/brutalchess/utopic

« back to all changes in this revision

Viewing changes to vector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2006-04-07 10:41:25 UTC
  • Revision ID: james.westby@ubuntu.com-20060407104125-18mnxbl1yzju7e84
Tags: upstream-0.0.20060314cvs
Import upstream version 0.0.20060314cvs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// gelvector.h
 
2
// GELVector
 
3
// OpenGL object structure
 
4
// by Joe Flint
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
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.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
 
 
15
#include "vector.h"
 
16
#include <math.h>
 
17
#include "glhead.h"
 
18
 
 
19
/** Default vector constructor.
 
20
 *  Vector points in the i + j + k direction (Components are < 1, 1, 1 >).
 
21
 */
 
22
Vector::Vector()
 
23
{
 
24
        x = 1.0;
 
25
        y = 1.0;
 
26
        z = 1.0;
 
27
}
 
28
 
 
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
 
34
 */
 
35
Vector::Vector( double xcomp, double ycomp, double zcomp )
 
36
{
 
37
        // Set the components of the vector
 
38
        x = xcomp;
 
39
        y = ycomp;
 
40
        z = zcomp;
 
41
}
 
42
 
 
43
void Vector::glScale() const
 
44
{
 
45
        glScaled( x, y, z );
 
46
}
 
47
 
 
48
void Vector::glRotate() const
 
49
{
 
50
        glRotated( x, 1, 0, 0 );
 
51
        glRotated( y, 0, 1, 0 );
 
52
        glRotated( z, 0, 0, 1 );
 
53
}
 
54
 
 
55
void Vector::glNormal() const
 
56
{
 
57
        glNormal3d( x, y, z );
 
58
}
 
59
 
 
60
void Vector::glTranslate() const
 
61
{
 
62
        glTranslated( x, y, z );
 
63
}
 
64
 
 
65
/** Calculate the magnitude of the vector.
 
66
  * @return The magnitude of the vector
 
67
  */
 
68
double Vector::magnitude() const
 
69
{
 
70
        return sqrt( x * x + y * y + z * z );
 
71
}
 
72
 
 
73
/** Normalize the vector.
 
74
  * Modifies the current vector so that it's magnitude is 1.
 
75
  * Direction remains unchanged.
 
76
  */
 
77
void Vector::normalize()
 
78
{
 
79
        double m = magnitude();
 
80
        x /= m;
 
81
        y /= m;
 
82
        z /= m;
 
83
}
 
84
 
 
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
 
88
  */
 
89
void Vector::scale( double s )
 
90
{
 
91
        x *= s;
 
92
        y *= s;
 
93
        z *= s;
 
94
}
 
95
 
 
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
 
100
  */
 
101
double Vector::dot( Vector v ) const
 
102
{
 
103
        return ( x * v.x + y * v.y + z * v.z );
 
104
}
 
105
 
 
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
 
110
  */
 
111
Vector Vector::cross( Vector v ) const
 
112
{
 
113
        Vector c;
 
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;
 
117
        return c;
 
118
}
 
119
 
 
120
Vector Vector::operator+( const Vector & rhs ) const
 
121
{
 
122
        Vector temp;
 
123
        temp.x = x + rhs.x;
 
124
        temp.y = y + rhs.y;
 
125
        temp.z = z + rhs.z;
 
126
        
 
127
        return temp;
 
128
}
 
129
 
 
130
Vector Vector::operator-( const Vector & rhs ) const
 
131
{
 
132
        Vector temp;
 
133
        temp.x = x - rhs.x;
 
134
        temp.y = y - rhs.y;
 
135
        temp.z = z - rhs.z;
 
136
        
 
137
        return temp;
 
138
}
 
139
 
 
140
const Vector & Vector::operator+=( const Vector & rhs )
 
141
{
 
142
        *this = *this + rhs;
 
143
        
 
144
        return *this;
 
145
}
 
146
 
 
147
const Vector & Vector::operator-=( const Vector & rhs )
 
148
{
 
149
        *this = *this - rhs;
 
150
 
 
151
        return *this;
 
152
}
 
153
 
 
154
const Vector Vector::operator*( const Vector & rhs ) const
 
155
{
 
156
        Vector temp = *this;
 
157
        temp.x *= rhs.x;
 
158
        temp.y *= rhs.y;
 
159
        temp.z *= rhs.z;
 
160
        
 
161
        return temp;
 
162
}
 
163
 
 
164
const Vector Vector::operator/( const Vector & rhs ) const
 
165
{
 
166
        Vector temp = *this;
 
167
        temp.x /= rhs.x;
 
168
        temp.y /= rhs.y;
 
169
        temp.z /= rhs.z;
 
170
        
 
171
        return temp;
 
172
}       
 
173
 
 
174
const Vector Vector::operator*( const double & rhs ) const
 
175
{
 
176
        Vector temp = *this;    
 
177
        temp.scale( rhs );
 
178
 
 
179
        return temp;
 
180
}
 
181
 
 
182
const Vector Vector::operator/( const double & rhs ) const
 
183
{
 
184
        Vector temp = *this;    
 
185
        temp.scale( 1 / rhs );
 
186
 
 
187
        return temp;
 
188
}
 
189
 
 
190
bool Vector::operator==( const Vector & rhs ) const
 
191
{
 
192
        return ( x == rhs.x && y == rhs.y && z == rhs.z );
 
193
}
 
194
 
 
195
bool Vector::operator!=( const Vector & rhs ) const
 
196
{
 
197
        return !operator==( rhs );
 
198
}