vec2.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 #pragma once
32 
33 #include <cmath>
34 #include "vec3.h"
35 #include "vec4.h"
36 #include "origin.h"
37 
38 namespace clan
39 {
42 
43 template<typename Type>
44 class Vec2;
45 
46 template<typename Type>
47 class Vec3;
48 
49 template<typename Type>
50 class Vec4;
51 
52 template<typename Type>
53 class Mat2;
54 
55 template<typename Type>
56 class Mat3;
57 
58 template<typename Type>
59 class Mat4;
60 
61 template<typename Type>
62 class Sizex;
63 
64 template<typename Type>
65 class Pointx;
66 
67 class Angle;
68 
74 template<typename Type>
75 class Vec2
76 {
77 public:
78  typedef Type datatype;
79 
80  union { Type x; Type s; Type r; };
81  union { Type y; Type t; Type g; };
82 
83  Vec2() : x(0), y(0) { }
84  explicit Vec2(const Type &scalar) : x(scalar), y(scalar) { }
85  explicit Vec2(const Vec3<Type> &copy) { x = copy.x; y = copy.y;}
86  explicit Vec2(const Vec4<Type> &copy) { x = copy.x; y = copy.y;}
87  explicit Vec2(const Type &p1, const Type &p2) : x(p1), y(p2) { }
88  explicit Vec2(const Type *array_xy) : x(array_xy[0]), y(array_xy[1]) { }
89 
90  Vec2(const Vec2<double> &copy);
91  Vec2(const Vec2<float> &copy);
92  Vec2(const Vec2<int> &copy);
93 
100  static Vec2<Type> normalize(const Vec2<Type>& vector);
101 
109  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; }
110 
117  static Vec2<Type> round(const Vec2<Type>& vector);
118 
124  static Vec2<Type> rotate(const Vec2<Type>& vector, const Vec2<Type>& hotspot, const Angle &angle);
125 
131  static Pointx<Type> calc_origin(Origin origin, const Sizex<Type> &size);
132 
138  static bool is_equal(const Vec2<Type> &first, const Vec2<Type> &second, Type epsilon)
139  {
140  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y;
141  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon );
142  }
143 
146 
147 public:
148 
154  Type length() const;
155 
162 
170  Type dot(const Vec2<Type>& vector) const {return x*vector.x + y*vector.y;}
171 
177  Angle angle(const Vec2<Type>& vector) const;
178 
184  Angle angle_normed(const Vec2<Type>& vector) const;
185 
191  Angle angle_line(const Vec2<Type>& point) const;
192 
198  Type distance(const Vec2<Type>& vector) const;
199 
206 
213  Vec2<Type> &rotate(const Vec2<Type>& hotspot, const Angle &angle);
214 
220  Type round_value(float value) const;
221 
226  bool is_equal(const Vec2<Type> &other, Type epsilon) const { return Vec2<Type>::is_equal(*this, other, epsilon); }
227 
231 
232 public:
234  void operator += (const Vec2<Type>& vector) { x+= vector.x; y+= vector.y; }
235 
237  void operator += ( Type value) { x+= value; y+= value; }
238 
240  void operator -= (const Vec2<Type>& vector) { x-= vector.x; y-= vector.y; }
241 
243  void operator -= ( Type value) { x-= value; y-= value; }
244 
246  Vec2<Type> operator - () const {return Vec2<Type>(-x , -y);}
247 
249  void operator *= (const Vec2<Type>& vector) { x*= vector.x; y*= vector.y; }
250 
252  void operator *= ( Type value) { x*= value; y*= value; }
253 
255  void operator /= (const Vec2<Type>& vector) { x/= vector.x; y/= vector.y; }
256 
258  void operator /= ( Type value) { x/= value; y/= value; }
259 
261  Vec2<Type> &operator = (const Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
262 
264  bool operator == (const Vec2<Type>& vector) const {return ((x == vector.x) && (y == vector.y));}
265 
267  bool operator != (const Vec2<Type>& vector) const {return ((x != vector.x) || (y != vector.y));}
268 
270  bool operator < (const Vec2<Type>& vector) const { return y < vector.y || (y == vector.y && x < vector.x); }
272 };
273 
275 template<typename Type>
276 Vec2<Type> operator + (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x + v2.x, v1.y + v2.y);}
277 
279 template<typename Type>
280 Vec2<Type> operator + (Type s, const Vec2<Type>& v) {return Vec2<Type>(s + v.x, s + v.y);}
281 
283 template<typename Type>
284 Vec2<Type> operator + (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x + s, v.y + s);}
285 
287 template<typename Type>
288 Vec2<Type> operator - (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x - v2.x, v1.y - v2.y);}
289 
291 template<typename Type>
292 Vec2<Type> operator - (Type s, const Vec2<Type>& v) {return Vec2<Type>(s - v.x, s - v.y);}
293 
295 template<typename Type>
296 Vec2<Type> operator - (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x - s, v.y - s);}
297 
299 template<typename Type>
300 Vec2<Type> operator * (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x * v2.x, v1.y * v2.y);}
301 
303 template<typename Type>
304 Vec2<Type> operator * (Type s, const Vec2<Type>& v) {return Vec2<Type>(s * v.x, s * v.y);}
305 
307 template<typename Type>
308 Vec2<Type> operator * (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x * s, v.y * s);}
309 
311 template<typename Type>
312 Vec2<Type> operator / (const Vec2<Type>& v1, const Vec2<Type>& v2) {return Vec2<Type>(v1.x / v2.x, v1.y / v2.y);}
313 
315 template<typename Type>
316 Vec2<Type> operator / (Type s, const Vec2<Type>& v) {return Vec2<Type>(s / v.x, s / v.y);}
317 
319 template<typename Type>
320 Vec2<Type> operator / (const Vec2<Type>& v, Type s) {return Vec2<Type>(v.x / s, v.y / s);}
321 
322 template<typename Type>
323 Vec2<Type> operator * (const Vec2<Type>& v, const Mat2<Type>& matrix)
324 {
325  return Vec2<Type>(
326  matrix[0*2+0]*v.x + matrix[0*2+1]*v.y,
327  matrix[1*2+0]*v.x + matrix[1*2+1]*v.y);
328 }
329 
330 template<typename Type>
331 Vec2<Type> operator * (const Mat2<Type>& matrix, const Vec2<Type>& v)
332 {
333  return Vec2<Type>(
334  matrix[0*2+0]*v.x + matrix[1*2+0]*v.y,
335  matrix[0*2+1]*v.x + matrix[1*2+1]*v.y);
336 }
337 
339 
340 template<>
341 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); }
342 
343 template<>
344 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); }
345 
346 template<>
347 inline Vec2<unsigned char>::Vec2(const Vec2<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; }
348 
349 template<>
350 inline Vec2<char>::Vec2(const Vec2<float> &copy) { x = (char) std::floor(copy.x +0.5f); y = (char) std::floor(copy.y + 0.5f); }
351 
352 template<>
353 inline Vec2<char>::Vec2(const Vec2<double> &copy) { x = (char) std::floor(copy.x+0.5); y = (char) std::floor(copy.y+0.5); }
354 
355 template<>
356 inline Vec2<char>::Vec2(const Vec2<int> &copy) { x = (char) copy.x; y = (char) copy.y; }
357 
358 template<>
359 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); }
360 
361 template<>
362 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); }
363 
364 template<>
365 inline Vec2<unsigned short>::Vec2(const Vec2<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; }
366 
367 template<>
368 inline Vec2<short>::Vec2(const Vec2<float> &copy) { x = (short) std::floor(copy.x +0.5f); y = (short) std::floor(copy.y + 0.5f); }
369 
370 template<>
371 inline Vec2<short>::Vec2(const Vec2<double> &copy) { x = (short) std::floor(copy.x+0.5); y = (short) std::floor(copy.y+0.5); }
372 
373 template<>
374 inline Vec2<short>::Vec2(const Vec2<int> &copy) { x = (short) copy.x; y = (short) copy.y; }
375 
376 template<>
377 inline Vec2<int>::Vec2(const Vec2<float> &copy) { x = (int) std::floor(copy.x +0.5f); y = (int) std::floor(copy.y + 0.5f); }
378 
379 template<>
380 inline Vec2<int>::Vec2(const Vec2<double> &copy) { x = (int) std::floor(copy.x+0.5); y = (int) std::floor(copy.y+0.5); }
381 
382 template<>
383 inline Vec2<int>::Vec2(const Vec2<int> &copy) { x = (int) copy.x; y = (int) copy.y; }
384 
385 template<>
386 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); }
387 
388 template<>
389 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); }
390 
391 template<>
392 inline Vec2<unsigned int>::Vec2(const Vec2<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; }
393 
394 template<>
395 inline Vec2<float>::Vec2(const Vec2<float> &copy) { x = (float) copy.x; y = (float) copy.y; }
396 
397 template<>
398 inline Vec2<float>::Vec2(const Vec2<double> &copy) { x = (float) copy.x; y = (float) copy.y; }
399 
400 template<>
401 inline Vec2<float>::Vec2(const Vec2<int> &copy) { x = (float) copy.x; y = (float) copy.y; }
402 
403 template<>
404 inline Vec2<double>::Vec2(const Vec2<float> &copy) { x = (double) copy.x; y = (double) copy.y; }
405 
406 template<>
407 inline Vec2<double>::Vec2(const Vec2<double> &copy) { x = (double) copy.x; y = (double) copy.y; }
408 
409 template<>
410 inline Vec2<double>::Vec2(const Vec2<int> &copy) { x = (double) copy.x; y = (double) copy.y; }
411 
412 template<typename Type>
413 inline Type Vec2<Type>::length() const {return (Type) floor(sqrt(float(x*x+y*y))+0.5f);}
414 
415 template<>
416 inline double Vec2<double>::length() const {return sqrt(x*x+y*y);}
417 
418 template<>
419 inline float Vec2<float>::length() const {return sqrt(x*x+y*y);}
420 
421 template<typename Type>
422 inline Vec2<Type> &Vec2<Type>::normalize() { Type f = length(); if (f!=0) { x /= f; y /= f; } return *this; }
423 
424 template<typename Type>
425 inline Vec2<Type> Vec2<Type>::normalize(const Vec2<Type>& vector) { Vec2<Type> dest(vector); dest.normalize(); return dest; }
426 
428 
434 typedef Vec2<int> Vec2i;
437 
438 }
439 
Type x
Definition: vec4.h:80
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.
Vec2< Type > operator-() const
operator.
Definition: vec2.h:246
void operator/=(const Vec2< Type > &vector)
/= operator.
Definition: vec2.h:255
Vec2< Type > & rotate(const Vec2< Type > &hotspot, const Angle &angle)
Rotate this vector around another point.
Vec2< short > Vec2s
Definition: vec2.h:432
static Vec2< Type > normalize(const Vec2< Type > &vector)
Normalizes a vector.
Definition: vec2.h:425
Type y
Definition: vec2.h:81
void operator*=(const Vec2< Type > &vector)
*= operator.
Definition: vec2.h:249
void operator-=(const Vec2< Type > &vector)
-= operator.
Definition: vec2.h:240
2D (x,y) point structure.
Definition: point.h:53
Type datatype
Definition: vec2.h:78
Type distance(const Vec2< Type > &vector) const
Calculate the distance between this vector and an other vector.
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:276
Type t
Definition: vec2.h:81
Vec2(const Vec2< double > &copy)
bool operator<(const Vec2< Type > &vector) const
< operator.
Definition: vec2.h:270
Vec2< unsigned short > Vec2us
Definition: vec2.h:431
Vec2(const Type &p1, const Type &p2)
Definition: vec2.h:87
Origin
Alignment origins.
Definition: origin.h:41
Vec2(const Vec2< float > &copy)
Vec2(const Vec3< Type > &copy)
Definition: vec2.h:85
Type x
Definition: vec2.h:80
Vec2()
Definition: vec2.h:83
Vec2< double > Vec2d
Definition: vec2.h:436
2D matrix
Definition: mat2.h:45
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:312
bool operator!=(const Vec2< Type > &vector) const
!= operator.
Definition: vec2.h:267
Angle class.
Definition: angle.h:63
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:300
static Vec2< Type > round(const Vec2< Type > &vector)
Rounds all components on a vector.
Type g
Definition: vec2.h:81
Angle angle_line(const Vec2< Type > &point) const
Calculate the angle of the line joining this point and other point.
Angle angle_normed(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors.
@ length
value is a keyword
Vec2(const Vec4< Type > &copy)
Definition: vec2.h:86
Vec2< char > Vec2b
Definition: vec2.h:430
Angle angle(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector.
4D vector
Definition: size.h:47
Type dot(const Vec2< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec2.h:170
Definition: clanapp.h:36
Type s
Definition: vec2.h:80
Vec2< float > Vec2f
Definition: vec2.h:435
Vec2(const Type *array_xy)
Definition: vec2.h:88
Vec2(const Vec2< int > &copy)
Type y
Definition: vec4.h:81
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:288
static Type dot(const Vec2< Type > &vector_1, const Vec2< Type > &vector_2)
Dot products a vector with an other vector.
Definition: vec2.h:109
Type r
Definition: vec2.h:80
Vec2< Type > & operator=(const Vec2< Type > &vector)
= operator.
Definition: vec2.h:261
Type round_value(float value) const
Rounds a value for the datatype.
bool is_equal(const Vec2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec2.h:226
3D vector
Definition: line_ray.h:48
Vec2(const Type &scalar)
Definition: vec2.h:84
Type x
Definition: vec3.h:80
Vec2< Type > & normalize()
Normalizes this vector.
Definition: vec2.h:422
Vec2< unsigned char > Vec2ub
Definition: vec2.h:429
Vec2< int > Vec2i
Definition: vec2.h:434
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:138
2D (width,height) size structure.
Definition: size.h:55
static Vec2< Type > rotate(const Vec2< Type > &vector, const Vec2< Type > &hotspot, const Angle &angle)
Rotate a vector around another point.
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec2.h:413
Vec2< Type > & round()
Rounds all components of this vector.
Vec2< unsigned int > Vec2ui
Definition: vec2.h:433
bool operator==(const Vec2< Type > &vector) const
== operator.
Definition: vec2.h:264
void operator+=(const Vec2< Type > &vector)
+= operator.
Definition: vec2.h:234
Type y
Definition: vec3.h:81