ClanLib  2.3.7
vec2.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 ** Harry Storbacka
29 */
30 
33 
34 #pragma once
35 
36 #include "../api_core.h"
37 #include "cl_math.h"
38 #include "vec1.h"
39 #include "vec3.h"
40 #include "vec4.h"
41 #include "origin.h"
42 
43 template<typename Type>
44 class CL_Vec1;
45 
46 template<typename Type>
47 class CL_Vec2;
48 
49 template<typename Type>
50 class CL_Vec3;
51 
52 template<typename Type>
53 class CL_Vec4;
54 
55 template<typename Type>
56 class CL_Mat2;
57 
58 template<typename Type>
59 class CL_Mat3;
60 
61 template<typename Type>
62 class CL_Mat4;
63 
64 template<typename Type>
65 class CL_Sizex;
66 
67 template<typename Type>
68 class CL_Pointx;
69 
70 class CL_Angle;
71 
78 template<typename Type>
79 class CL_Vec2
80 {
81 public:
82  typedef Type datatype;
83 
84  union { Type x; Type s; Type r; };
85  union { Type y; Type t; Type g; };
86 
87  CL_Vec2() : x(0), y(0) { }
88  CL_Vec2(const CL_Vec1<Type> &copy) { x = copy.x; y = 0;}
89  CL_Vec2(const CL_Vec3<Type> &copy) { x = copy.x; y = copy.y;}
90  CL_Vec2(const CL_Vec4<Type> &copy) { x = copy.x; y = copy.y;}
91  CL_Vec2(const Type &p1, const Type &p2 = 0) : x(p1), y(p2) { }
92  CL_Vec2(const Type *array_xy) : x(array_xy[0]), y(array_xy[1]) { }
93  CL_Vec2(const CL_Pointx<int> &point);
94  CL_Vec2(const CL_Pointx<float> &point);
95  CL_Vec2(const CL_Pointx<double> &point);
96 
97  CL_Vec2(const CL_Vec2<double> &copy);
98  CL_Vec2(const CL_Vec2<float> &copy);
99  CL_Vec2(const CL_Vec2<int> &copy);
100 
107  static CL_Vec2<Type> normalize(const CL_Vec2<Type>& vector);
108 
116  static Type dot(const CL_Vec2<Type>& vector_1, const CL_Vec2<Type>& vector_2) { return vector_1.x*vector_2.x + vector_1.y*vector_2.y; }
117 
124  static CL_Vec2<Type> round(const CL_Vec2<Type>& vector);
125 
131  static CL_Vec2<Type> rotate(const CL_Vec2<Type>& vector, const CL_Vec2<Type>& hotspot, const CL_Angle &angle);
132 
138  static CL_Pointx<Type> calc_origin(CL_Origin origin, const CL_Sizex<Type> &size);
139 
142 
143 public:
144 
150  Type length() const;
151 
158 
166  Type dot(const CL_Vec2<Type>& vector) const {return x*vector.x + y*vector.y;}
167 
173  CL_Angle angle(const CL_Vec2<Type>& vector) const;
174 
180  CL_Angle angle_relative(const CL_Vec2<Type>& vector) const;
181 
187  Type distance(const CL_Vec2<Type>& vector) const;
188 
194  CL_Vec2<Type> &round();
195 
202  CL_Vec2<Type> &rotate(const CL_Vec2<Type>& hotspot, const CL_Angle &angle);
203 
209  Type round_value(float value) const;
210 
214 
215 public:
216  const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
217  Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
218  operator Type *() { return (Type *) this; }
219  operator Type * const() const { return (Type * const) this; }
220 
222  void operator += (const CL_Vec2<Type>& vector) { x+= vector.x; y+= vector.y; }
223 
225  void operator += ( Type value) { x+= value; y+= value; }
226 
228  CL_Vec2<Type> operator + (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(vector.x + x, vector.y + y);}
229 
231  CL_Vec2<Type> operator + (Type value) const {return CL_Vec2<Type>(value + x, value + y);}
232 
234  void operator -= (const CL_Vec2<Type>& vector) { x-= vector.x; y-= vector.y; }
235 
237  void operator -= ( Type value) { x-= value; y-= value; }
238 
240  CL_Vec2<Type> operator - (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(x - vector.x, y - vector.y);}
241 
243  CL_Vec2<Type> operator - (Type value) const {return CL_Vec2<Type>(x - value, y - value);}
244 
246  CL_Vec2<Type> operator - () const {return CL_Vec2<Type>(-x , -y);}
247 
249  void operator *= (const CL_Vec2<Type>& vector) { x*= vector.x; y*= vector.y; }
250 
252  void operator *= ( Type value) { x*= value; y*= value; }
253 
255  CL_Vec2<Type> operator * (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(vector.x * x, vector.y * y);}
256 
258  CL_Vec2<Type> operator * (Type value) const {return CL_Vec2<Type>(value * x, value * y);}
259 
261  void operator /= (const CL_Vec2<Type>& vector) { x/= vector.x; y/= vector.y; }
262 
264  void operator /= ( Type value) { x/= value; y/= value; }
265 
267  CL_Vec2<Type> operator / (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(x / vector.x, y / vector.y);}
268 
270  CL_Vec2<Type> operator / (Type value) const {return CL_Vec2<Type>(x / value, y / value);}
271 
273  CL_Vec2<Type> &operator = (const CL_Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
274 
276  bool operator == (const CL_Vec2<Type>& vector) const {return ((x == vector.x) && (y == vector.y));}
277 
279  bool operator != (const CL_Vec2<Type>& vector) const {return ((x != vector.x) || (y != vector.y));}
281 };
282 
283 template<typename Type>
285 {
286  return CL_Vec2<Type>(
287  matrix[0*2+0]*v.x + matrix[0*2+1]*v.y,
288  matrix[1*2+0]*v.x + matrix[1*2+1]*v.y);
289 }
290 
291 template<typename Type>
293 {
294  return CL_Vec2<Type>(
295  matrix[0*2+0]*v.x + matrix[1*2+0]*v.y,
296  matrix[0*2+1]*v.x + matrix[1*2+1]*v.y);
297 }
298 
300 
301 template<>
302 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<float> &copy) { x = (unsigned char) floor(copy.x +0.5f); y = (unsigned char) floor(copy.y + 0.5f); }
303 
304 template<>
305 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<double> &copy) { x = (unsigned char) floor(copy.x+0.5); y = (unsigned char) floor(copy.y+0.5); }
306 
307 template<>
308 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; }
309 
310 template<>
311 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<float> &copy) { x = (char) floor(copy.x +0.5f); y = (char) floor(copy.y + 0.5f); }
312 
313 template<>
314 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<double> &copy) { x = (char) floor(copy.x+0.5); y = (char) floor(copy.y+0.5); }
315 
316 template<>
317 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<int> &copy) { x = (char) copy.x; y = (char) copy.y; }
318 
319 template<>
320 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<float> &copy) { x = (unsigned short) floor(copy.x +0.5f); y = (unsigned short) floor(copy.y + 0.5f); }
321 
322 template<>
323 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<double> &copy) { x = (unsigned short) floor(copy.x+0.5); y = (unsigned short) floor(copy.y+0.5); }
324 
325 template<>
326 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; }
327 
328 template<>
329 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<float> &copy) { x = (short) floor(copy.x +0.5f); y = (short) floor(copy.y + 0.5f); }
330 
331 template<>
332 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<double> &copy) { x = (short) floor(copy.x+0.5); y = (short) floor(copy.y+0.5); }
333 
334 template<>
335 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<int> &copy) { x = (short) copy.x; y = (short) copy.y; }
336 
337 template<>
338 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<float> &copy) { x = (int) floor(copy.x +0.5f); y = (int) floor(copy.y + 0.5f); }
339 
340 template<>
341 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<double> &copy) { x = (int) floor(copy.x+0.5); y = (int) floor(copy.y+0.5); }
342 
343 template<>
344 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<int> &copy) { x = (int) copy.x; y = (int) copy.y; }
345 
346 template<>
347 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<float> &copy) { x = (unsigned int) floor(copy.x +0.5f); y = (unsigned int) floor(copy.y + 0.5f); }
348 
349 template<>
350 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<double> &copy) { x = (unsigned int) floor(copy.x+0.5); y = (unsigned int) floor(copy.y+0.5); }
351 
352 template<>
353 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; }
354 
355 template<>
356 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<float> &copy) { x = (float) copy.x; y = (float) copy.y; }
357 
358 template<>
359 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<double> &copy) { x = (float) copy.x; y = (float) copy.y; }
360 
361 template<>
362 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<int> &copy) { x = (float) copy.x; y = (float) copy.y; }
363 
364 template<>
365 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<float> &copy) { x = (double) copy.x; y = (double) copy.y; }
366 
367 template<>
368 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<double> &copy) { x = (double) copy.x; y = (double) copy.y; }
369 
370 template<>
371 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<int> &copy) { x = (double) copy.x; y = (double) copy.y; }
372 
374 
383