vec2.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 
31 
32 #pragma once
33 
34 #include "../api_core.h"
35 #include <cmath>
36 #include "vec3.h"
37 #include "vec4.h"
38 #include "origin.h"
39 
40 namespace clan
41 {
44 
45 template<typename Type>
46 class Vec2;
47 
48 template<typename Type>
49 class Vec3;
50 
51 template<typename Type>
52 class Vec4;
53 
54 template<typename Type>
55 class Mat2;
56 
57 template<typename Type>
58 class Mat3;
59 
60 template<typename Type>
61 class Mat4;
62 
63 template<typename Type>
64 class Sizex;
65 
66 template<typename Type>
67 class Pointx;
68 
69 class Angle;
70 
76 template<typename Type>
77 class Vec2
78 {
79 public:
80  typedef Type datatype;
81 
82  union { Type x; Type s; Type r; };
83  union { Type y; Type t; Type g; };
84 
85  Vec2() : x(0), y(0) { }
86  explicit Vec2(const Type &scalar) : x(scalar), y(scalar) { }
87  explicit Vec2(const Vec3<Type> &copy) { x = copy.x; y = copy.y;}
88  explicit Vec2(const Vec4<Type> &copy) { x = copy.x; y = copy.y;}
89  explicit Vec2(const Type &p1, const Type &p2) : x(p1), y(p2) { }
90  explicit Vec2(const Type *array_xy) : x(array_xy[0]), y(array_xy[1]) { }
91 
92  Vec2(const Vec2<double> &copy);
93  Vec2(const Vec2<float> &copy);
94  Vec2(const Vec2<int> &copy);
95 
102  static Vec2<Type> normalize(const Vec2<Type>& vector);
103 
111  static Type dot(const Vec2<Type>& vector_1, const Vec2<Type>& vector_2) { return vector_1.x*vector_2.x + vector_1.y*vector_2.y; }
112 
119  static Vec2<Type> round(const Vec2<Type>& vector);
120 
126  static Vec2<Type> rotate(const Vec2<Type>& vector, const Vec2<Type>& hotspot, const Angle &angle);
127 
133  static Pointx<Type> calc_origin(Origin origin, const Sizex<Type> &size);
134 
140  static bool is_equal(const Vec2<Type> &first, const Vec2<Type> &second, Type epsilon)
141  {
142  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y;
143  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon );
144  }
145 
148 
149 public:
150 
156  Type length() const;
157 
164 
172  Type dot(const Vec2<Type>& vector) const {return x*vector.x + y*vector.y;}
173 
179  Angle angle(const Vec2<Type>& vector) const;
180 
186  Angle angle_normed(const Vec2<Type>& vector) const;
187 
193  Angle angle_line(const Vec2<Type>& point) const;
194 
200  Type distance(const Vec2<Type>& vector) const;
201 
207  Vec2<Type> &round();
208 
215  Vec2<Type> &rotate(const Vec2<Type>& hotspot, const Angle &angle);
216 
222  Type round_value(float value) const;
223 
228  bool is_equal(const Vec2<Type> &other, Type epsilon) const { return Vec2<Type>::is_equal(*this, other, epsilon); }
229 
233 
234 public:
236  void operator += (const Vec2<Type>& vector) { x+= vector.x; y+= vector.y; }
237 
239  void operator += ( Type value) { x+= value; y+= value; }
240 
242  void operator -= (const Vec2<Type>& vector) { x-= vector.x; y-= vector.y; }
243 
245  void operator -= ( Type value) { x-= value; y-= value; }
246 
248  Vec2<Type> operator - () const {return Vec2<Type>(-x , -y);}
249 
251  void operator *= (const Vec2<Type>& vector) { x*= vector.x; y*= vector.y; }
252 
254  void operator *= ( Type value) { x*= value; y*= value; }
255 
257  void operator /= (const Vec2<Type>& vector) { x/= vector.x; y/= vector.y; }
258 
260  void operator /= ( Type value) { x/= value; y/= value; }
261 
263  Vec2<Type> &operator = (const Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
264 
266  bool operator == (const Vec2<Type>& vector) const {return ((x == vector.x) && (y == vector.y));}
267 
269  bool operator != (const Vec2<Type>& vector) const {return ((x != vector.x) || (y != vector.y));}
270 
272  bool operator < (const Vec2<Type>& vector) const { return y < vector.y || (y == vector.y && x < vector.x); }
274 };
275 
277 template<typename Type>
278 Vec2<Type> operator + (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x + v2.x, v1.y + v2.y);}
279 
281 template<typename Type>
282 Vec2<Type> operator + (Type s, const Vec2<Type>& v) {return Vec2<Type>(s + v.x, s + v.y);}
283 
285 template<typename Type>
286 Vec2<Type> operator + (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x + s, v.y + s);}
287 
289 template<typename Type>
290 Vec2<Type> operator - (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x - v2.x, v1.y - v2.y);}
291 
293 template<typename Type>
294 Vec2<Type> operator - (Type s, const Vec2<Type>& v) {return Vec2<Type>(s - v.x, s - v.y);}
295 
297 template<typename Type>
298 Vec2<Type> operator - (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x - s, v.y - s);}
299 
301 template<typename Type>
302 Vec2<Type> operator * (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x * v2.x, v1.y * v2.y);}
303 
305 template<typename Type>
306 Vec2<Type> operator * (Type s, const Vec2<Type>& v) {return Vec2<Type>(s * v.x, s * v.y);}
307 
309 template<typename Type>
310 Vec2<Type> operator * (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x * s, v.y * s);}
311 
313 template<typename Type>
314 Vec2<Type> operator / (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x / v2.x, v1.y / v2.y);}
315 
317 template<typename Type>
318 Vec2<Type> operator / (Type s, const Vec2<Type>& v) {return Vec2<Type>(s / v.x, s / v.y);}
319 
321 template<typename Type>
322 Vec2<Type> operator / (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x / s, v.y / s);}
323 
324 template<typename Type>
325 Vec2<Type> operator * (const Vec2<Type>& v, const Mat2<Type>& matrix)
326 {
327  return Vec2<Type>(
328  matrix[0*2+0]*v.x + matrix[0*2+1]*v.y,
329  matrix[1*2+0]*v.x + matrix[1*2+1]*v.y);
330 }
331 
332 template<typename Type>
333 Vec2<Type> operator * (const Mat2<Type>& matrix, const Vec2<Type>& v)
334 {
335  return Vec2<Type>(
336  matrix[0*2+0]*v.x + matrix[1*2+0]*v.y,
337  matrix[0*2+1]*v.x + matrix[1*2+1]*v.y);
338 }
339 
341 
342 template<>
343 inline Vec2<unsigned char>::Vec2(const Vec2<float> &copy) { x = (unsigned char) std::floor(copy.x +0.5f); y = (unsigned char) std::floor(copy.y + 0.5f); }
344 
345 template<>
346 inline Vec2<unsigned char>::Vec2(const Vec2<double> &copy) { x = (unsigned char) std::floor(copy.x+0.5); y = (unsigned char) std::floor(copy.y+0.5); }
347 
348 template<>
349 inline Vec2<unsigned char>::Vec2(const Vec2<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; }
350 
351 template<>
352 inline Vec2<char>::Vec2(const Vec2<float> &copy) { x = (char) std::floor(copy.x +0.5f); y = (char) std::floor(copy.y + 0.5f); }
353 
354 template<>
355 inline Vec2<char>::Vec2(const Vec2<double> &copy) { x = (char) std::floor(copy.x+0.5); y = (char) std::floor(copy.y+0.5); }
356 
357 template<>
358 inline Vec2<char>::Vec2(const Vec2<int> &copy) { x = (char) copy.x; y = (char) copy.y; }
359 
360 template<>
361 inline Vec2<unsigned short>::Vec2(const Vec2<float> &copy) { x = (unsigned short) std::floor(copy.x +0.5f); y = (unsigned short) std::floor(copy.y + 0.5f); }
362 
363 template<>
364 inline Vec2<unsigned short>::Vec2(const Vec2<double> &copy) { x = (unsigned short) std::floor(copy.x+0.5); y = (unsigned short) std::floor(copy.y+0.5); }
365 
366 template<>
367 inline Vec2<unsigned short>::Vec2(const Vec2<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; }
368 
369 template<>
370 inline Vec2<short>::Vec2(const Vec2<float> &copy) { x = (short) std::floor(copy.x +0.5f); y = (short) std::floor(copy.y + 0.5f); }
371 
372 template<>
373 inline Vec2<short>::Vec2(const Vec2<double> &copy) { x = (short) std::floor(copy.x+0.5); y = (short) std::floor(copy.y+0.5); }
374 
375 template<>
376 inline Vec2<short>::Vec2(const Vec2<int> &copy) { x = (short) copy.x; y = (short) copy.y; }
377 
378 template<>
379 inline Vec2<int>::Vec2(const Vec2<float> &copy) { x = (int) std::floor(copy.x +0.5f); y = (int) std::floor(copy.y + 0.5f); }
380 
381 template<>
382 inline Vec2<int>::Vec2(const Vec2<double> &copy) { x = (int) std::floor(copy.x+0.5); y = (int) std::floor(copy.y+0.5); }
383 
384 template<>
385 inline Vec2<int>::Vec2(const Vec2<int> &copy) { x = (int) copy.x; y = (int) copy.y; }
386 
387 template<>
388 inline Vec2<unsigned int>::Vec2(const Vec2<float> &copy) { x = (unsigned int) std::floor(copy.x +0.5f); y = (unsigned int) std::floor(copy.y + 0.5f); }
389 
390 template<>
391 inline Vec2<unsigned int>::Vec2(const Vec2<double> &copy) { x = (unsigned int) std::floor(copy.x+0.5); y = (unsigned int) std::floor(copy.y+0.5); }
392 
393 template<>
394 inline Vec2<unsigned int>::Vec2(const Vec2<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; }
395 
396 template<>
397 inline Vec2<float>::Vec2(const Vec2<float> &copy) { x = (float) copy.x; y = (float) copy.y; }
398 
399 template<>
400 inline Vec2<float>::Vec2(const Vec2<double> &copy) { x = (float) copy.x; y = (float) copy.y; }
401 
402 template<>
403 inline Vec2<float>::Vec2(const Vec2<int> &copy) { x = (float) copy.x; y = (float) copy.y; }
404 
405 template<>
406 inline Vec2<double>::Vec2(const Vec2<float> &copy) { x = (double) copy.x; y = (double) copy.y; }
407 
408 template<>
409 inline Vec2<double>::Vec2(const Vec2<double> &copy) { x = (double) copy.x; y = (double) copy.y; }
410 
411 template<>
412 inline Vec2<double>::Vec2(const Vec2<int> &copy) { x = (double) copy.x; y = (double) copy.y; }
413 
414 template<typename Type>
415 inline Type Vec2<Type>::length() const {return (Type) floor(sqrt(float(x*x+y*y))+0.5f);}
416 
417 template<>
418 inline double Vec2<double>::length() const {return sqrt(x*x+y*y);}
419 
420 template<>
421 inline float Vec2<float>::length() const {return sqrt(x*x+y*y);}
422 
423 template<typename Type>
424 inline Vec2<Type> &Vec2<Type>::normalize() { Type f = length(); if (f!=0) { x /= f; y /= f; } return *this; }
425 
426 template<typename Type>
427 inline Vec2<Type> Vec2<Type>::normalize(const Vec2<Type>& vector) { Vec2<Type> dest(vector); dest.normalize(); return dest; }
428 
430 
436 typedef Vec2<int> Vec2i;
439 
440 }
441 
void operator+=(const Vec2< Type > &vector)
+= operator.
Definition: vec2.h:236
Type r
Definition: vec2.h:82
Vec2< float > Vec2f
Definition: vec2.h:437
Type x
Definition: vec3.h:81
Vec2< Type > & operator=(const Vec2< Type > &vector)
= operator.
Definition: vec2.h:263
Type round_value(float value) const
Rounds a value for the datatype.
Type t
Definition: vec2.h:83
Angle class.
Definition: angle.h:63
void operator/=(const Vec2< Type > &vector)
/= operator.
Definition: vec2.h:257
Type distance(const Vec2< Type > &vector) const
Calculate the distance between this vector and an other vector.
bool operator!=(const Vec2< Type > &vector) const
!= operator.
Definition: vec2.h:269
Type y
Definition: vec3.h:82
Vec2< unsigned char > Vec2ub
Definition: vec2.h:431
static bool is_equal(const Vec2< Type > &first, const Vec2< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition: vec2.h:140
static Vec2< Type > rotate(const Vec2< Type > &vector, const Vec2< Type > &hotspot, const Angle &angle)
Rotate a vector around another point.
Origin
Alignment origins.
Definition: origin.h:41
Vec2(const Vec3< Type > &copy)
Definition: vec2.h:87
Vec2< short > Vec2s
Definition: vec2.h:434
bool is_equal(const Vec2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec2.h:228
Vec2< unsigned short > Vec2us
Definition: vec2.h:433
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:314
Vec2< int > Vec2i
Definition: vec2.h:436
Vec2()
Definition: vec2.h:85
Type s
Definition: vec2.h:82
Angle angle_line(const Vec2< Type > &point) const
Calculate the angle of the line joining this point and other point.
Vec2< char > Vec2b
Definition: vec2.h:432
Vec2< Type > operator-() const
operator.
Definition: vec2.h:248
static Pointx< Type > calc_origin(Origin origin, const Sizex< Type > &size)
Returns the anchor point for the origin within the dimensions of the size structure.
void operator*=(const Vec2< Type > &vector)
*= operator.
Definition: vec2.h:251
Type x
Definition: vec2.h:82
static Type dot(const Vec2< Type > &vector_1, const Vec2< Type > &vector_2)
Dot products a vector with an other vector.
Definition: vec2.h:111
Vec2< double > Vec2d
Definition: vec2.h:438
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:278
Angle angle(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector.
2D matrix
Definition: mat2.h:46
Angle angle_normed(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors...
Vec2(const Type &p1, const Type &p2)
Definition: vec2.h:89
Type datatype
Definition: vec2.h:80
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:302
3D vector
Definition: line_ray.h:49
Type y
Definition: vec4.h:82
Vec2(const Type &scalar)
Definition: vec2.h:86
Type dot(const Vec2< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec2.h:172
2D (x,y) point structure.
Definition: point.h:53
Type g
Definition: vec2.h:83
Type x
Definition: vec4.h:81
4D vector
Definition: size.h:48
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:290
void operator-=(const Vec2< Type > &vector)
-= operator.
Definition: vec2.h:242
Vec2< Type > & round()
Rounds all components of this vector.
Vec2< unsigned int > Vec2ui
Definition: vec2.h:435
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec2.h:415
bool operator==(const Vec2< Type > &vector) const
== operator.
Definition: vec2.h:266
Vec2< Type > & normalize()
Normalizes this vector.
Definition: vec2.h:424
2D (width,height) size structure.
Definition: size.h:55
Vec2(const Vec4< Type > &copy)
Definition: vec2.h:88
Type y
Definition: vec2.h:83
Vec2(const Type *array_xy)
Definition: vec2.h:90