Mercurial > fife-parpg
comparison engine/core/util/structures/point.h @ 645:291ba2946c73
* Added the ability to normalize a 2D and 3D point.
* You can now rotate a 2D point around the origin or a specific point.
* Modified the shooter demo to use the new point functionality.
* Removed the fife_math extension.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Tue, 12 Oct 2010 18:58:47 +0000 |
parents | 90005975cdbb |
children | 07b1cf8e92b5 |
comparison
equal
deleted
inserted
replaced
644:b84dbc4665b0 | 645:291ba2946c73 |
---|---|
58 * Creates a with 0 as default values. | 58 * Creates a with 0 as default values. |
59 */ | 59 */ |
60 explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) { | 60 explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) { |
61 } | 61 } |
62 | 62 |
63 /** Copy Constructor | |
64 */ | |
65 PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) { | |
66 } | |
67 | |
63 /** Vector addition | 68 /** Vector addition |
64 */ | 69 */ |
65 PointType2D<T> operator+(const PointType2D<T>& p) const { | 70 PointType2D<T> operator+(const PointType2D<T>& p) const { |
66 return PointType2D<T>(x + p.x, y + p.y); | 71 return PointType2D<T>(x + p.x, y + p.y); |
67 } | 72 } |
118 double sq; | 123 double sq; |
119 sq = x*x + y*y; | 124 sq = x*x + y*y; |
120 return static_cast<T>(sqrt(sq)); | 125 return static_cast<T>(sqrt(sq)); |
121 } | 126 } |
122 | 127 |
123 inline T& operator[] (int ind) { | 128 /** Normalizes the point |
129 */ | |
130 void normalize() { | |
131 T invLength = 1.0/length(); | |
132 | |
133 //TODO: get rid of this static cast | |
134 if (invLength > static_cast<T>(DBL_ZERO_TOLERANCE)) { | |
135 x = x * invLength; | |
136 y = y * invLength; | |
137 } | |
138 else { | |
139 x = 0; | |
140 y = 0; | |
141 } | |
142 } | |
143 | |
144 /** Rotates the point around the origin | |
145 */ | |
146 void rotate(T angle){ | |
147 //TODO: get rid of this static cast | |
148 T theta = (angle * static_cast<T>(DBL_PI))/180; | |
149 T costheta = cos(theta); | |
150 T sintheta = sin(theta); | |
151 | |
152 T nx = x; | |
153 T ny = y; | |
154 | |
155 x = costheta * nx - sintheta * ny; | |
156 y = sintheta * nx + costheta * ny; | |
157 } | |
158 | |
159 /** Rotates the point around an origin | |
160 */ | |
161 void rotate(const PointType2D<T>& origin, T angle){ | |
162 //TODO: get rid of this static cast | |
163 T theta = (angle * static_cast<T>(DBL_PI))/180; | |
164 T costheta = cos(theta); | |
165 T sintheta = sin(theta); | |
166 | |
167 T nx = x - origin.x; | |
168 T ny = y - origin.y; | |
169 | |
170 x = costheta * nx - sintheta * ny; | |
171 y = sintheta * nx + costheta * ny; | |
172 } | |
173 | |
174 inline T& operator[] (int ind) { | |
124 assert(ind > -1 && ind < 2); | 175 assert(ind > -1 && ind < 2); |
125 return val[ind]; | 176 return val[ind]; |
126 } | 177 } |
127 }; | 178 }; |
128 | 179 |
155 * Creates a with 0 as default values. | 206 * Creates a with 0 as default values. |
156 */ | 207 */ |
157 explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) { | 208 explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) { |
158 } | 209 } |
159 | 210 |
211 /** Copy Constructor | |
212 */ | |
213 PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) { | |
214 } | |
215 | |
160 /** Vector addition | 216 /** Vector addition |
161 */ | 217 */ |
162 PointType3D<T> operator+(const PointType3D<T>& p) const { | 218 PointType3D<T> operator+(const PointType3D<T>& p) const { |
163 return PointType3D<T>(x + p.x, y + p.y, z + p.z); | 219 return PointType3D<T>(x + p.x, y + p.y, z + p.z); |
164 } | 220 } |
217 double sq; | 273 double sq; |
218 sq = x*x + y*y + z*z; | 274 sq = x*x + y*y + z*z; |
219 return static_cast<T>(sqrt(sq)); | 275 return static_cast<T>(sqrt(sq)); |
220 } | 276 } |
221 | 277 |
222 inline T& operator[] (int ind) { | 278 /** Normalizes the point |
223 assert(ind > -1 && ind < 3); | 279 */ |
224 return val[ind]; | 280 void normalize() { |
281 T invLength = 1.0/length(); | |
282 | |
283 //TODO: get rid of this static cast | |
284 if (invLength > static_cast<T>(DBL_ZERO_TOLERANCE)) { | |
285 x = x * invLength; | |
286 y = y * invLength; | |
287 z = z * invLength; | |
288 } | |
289 else { | |
290 x = 0; | |
291 y = 0; | |
292 z = 0; | |
293 } | |
294 } | |
295 | |
296 inline T& operator[] (int ind) { | |
297 assert(ind > -1 && ind < 3); | |
298 return val[ind]; | |
225 } | 299 } |
226 }; | 300 }; |
227 | 301 |
228 /** Print coords of the Point to a stream | 302 /** Print coords of the Point to a stream |
229 */ | 303 */ |