1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
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, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Christian Meyer |
---|
15 | co-programmer: ... |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | #include "vector.h" |
---|
20 | |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | /** |
---|
25 | \brief add two vectors |
---|
26 | \param v: the other vector |
---|
27 | */ |
---|
28 | Vector Vector::operator+ (const Vector& v) const |
---|
29 | { |
---|
30 | Vector r; |
---|
31 | |
---|
32 | r.x = x + v.x; |
---|
33 | r.y = y + v.y; |
---|
34 | r.z = z + v.z; |
---|
35 | |
---|
36 | return r; |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | \brief subtract a vector from another |
---|
41 | \param v: the other vector |
---|
42 | */ |
---|
43 | Vector Vector::operator- (const Vector& v) const |
---|
44 | { |
---|
45 | Vector r; |
---|
46 | |
---|
47 | r.x = x - v.x; |
---|
48 | r.y = y - v.y; |
---|
49 | r.z = z - v.z; |
---|
50 | |
---|
51 | return r; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | \brief calculate the dot product of two vectors |
---|
56 | \param v: the other vector |
---|
57 | */ |
---|
58 | float Vector::operator* (const Vector& v) const |
---|
59 | { |
---|
60 | return x*v.x+y*v.y+z*v.z; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | \brief multiply a vector with a float |
---|
65 | \param v: the factor |
---|
66 | */ |
---|
67 | Vector Vector::operator* (float f) const |
---|
68 | { |
---|
69 | Vector r; |
---|
70 | |
---|
71 | r.x = x * f; |
---|
72 | r.y = y * f; |
---|
73 | r.z = z * f; |
---|
74 | |
---|
75 | return r; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | \brief divide a vector with a float |
---|
80 | \param v: the factor |
---|
81 | */ |
---|
82 | Vector Vector::operator/ (float f) const |
---|
83 | { |
---|
84 | Vector r; |
---|
85 | |
---|
86 | if( f == 0.0) |
---|
87 | { |
---|
88 | // Prevent divide by zero |
---|
89 | return Vector (0,0,0); |
---|
90 | } |
---|
91 | |
---|
92 | r.x = x / f; |
---|
93 | r.y = y / f; |
---|
94 | r.z = z / f; |
---|
95 | |
---|
96 | return r; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | \brief calculate the dot product of two vectors |
---|
101 | \param v: the other vector |
---|
102 | */ |
---|
103 | float Vector::dot (const Vector& v) const |
---|
104 | { |
---|
105 | return x*v.x+y*v.y+z*v.z; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | \brief calculate the cross product of two vectors |
---|
110 | \param v: the other vector |
---|
111 | */ |
---|
112 | Vector Vector::cross (const Vector& v) const |
---|
113 | { |
---|
114 | Vector r; |
---|
115 | |
---|
116 | r.x = y * v.z - z * v.y; |
---|
117 | r.y = z * v.x - x * v.z; |
---|
118 | r.z = x * v.y - y * v.x; |
---|
119 | |
---|
120 | return r; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | \brief normalize the vector to lenght 1.0 |
---|
125 | */ |
---|
126 | void Vector::normalize () |
---|
127 | { |
---|
128 | float l = len(); |
---|
129 | |
---|
130 | if( l == 0.0) |
---|
131 | { |
---|
132 | // Prevent divide by zero |
---|
133 | return; |
---|
134 | } |
---|
135 | |
---|
136 | x = x / l; |
---|
137 | y = y / l; |
---|
138 | z = z / l; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | \brief calculate the lenght of the vector |
---|
143 | */ |
---|
144 | float Vector::len () const |
---|
145 | { |
---|
146 | return sqrt (x*x+y*y+z*z); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | \brief calculate the angle between two vectors in radiances |
---|
151 | \param v1: a vector |
---|
152 | \param v2: another vector |
---|
153 | */ |
---|
154 | float angle_rad (const Vector& v1, const Vector& v2) |
---|
155 | { |
---|
156 | return acos( v1 * v2 / (v1.len() * v2.len())); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | \brief calculate the angle between two vectors in degrees |
---|
161 | \param v1: a vector |
---|
162 | \param v2: another vector |
---|
163 | */ |
---|
164 | float angle_deg (const Vector& v1, const Vector& v2) |
---|
165 | { |
---|
166 | float f; |
---|
167 | f = acos( v1 * v2 / (v1.len() * v2.len())); |
---|
168 | return f * 180 / PI; |
---|
169 | } |
---|