vec2.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 uicore
39 {
40  template<typename Type>
41  class Vec2;
42 
43  template<typename Type>
44  class Vec3;
45 
46  template<typename Type>
47  class Vec4;
48 
49  template<typename Type>
50  class Mat2;
51 
52  template<typename Type>
53  class Mat3;
54 
55  template<typename Type>
56  class Mat4;
57 
58  template<typename Type>
59  class Sizex;
60 
61  template<typename Type>
62  class Pointx;
63 
69  template<typename Type>
70  class Vec2
71  {
72  public:
73  typedef Type datatype;
74 
75  Type x;
76  Type y;
77 
78  Vec2() : x(0), y(0) { }
79  explicit Vec2(const Type &scalar) : x(scalar), y(scalar) { }
80  explicit Vec2(const Vec3<Type> &copy) { x = copy.x; y = copy.y; }
81  explicit Vec2(const Vec4<Type> &copy) { x = copy.x; y = copy.y; }
82  explicit Vec2(const Type &p1, const Type &p2) : x(p1), y(p2) { }
83  explicit Vec2(const Type *array_xy) : x(array_xy[0]), y(array_xy[1]) { }
84 
85  Vec2(const Vec2<double> &copy);
86  Vec2(const Vec2<float> &copy);
87  Vec2(const Vec2<int> &copy);
88 
95  static Vec2<Type> normalize(const Vec2<Type>& vector);
96 
104  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; }
105 
112  static Vec2<Type> round(const Vec2<Type>& vector);
113 
119  static Vec2<Type> rotate(const Vec2<Type>& vector, const Vec2<Type>& hotspot, Type angle);
120 
126  static Pointx<Type> calc_origin(Origin origin, const Sizex<Type> &size);
127 
133  static bool is_equal(const Vec2<Type> &first, const Vec2<Type> &second, Type epsilon)
134  {
135  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y;
136  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon);
137  }
138 
144  Type length() const;
145 
152 
160  Type dot(const Vec2<Type>& vector) const { return x*vector.x + y*vector.y; }
161 
167  Type angle(const Vec2<Type>& vector) const;
168 
174  Type angle_normed(const Vec2<Type>& vector) const;
175 
181  Type angle_line(const Vec2<Type>& point) const;
182 
188  Type distance(const Vec2<Type>& vector) const;
189 
195  Vec2<Type> &round();
196 
203  Vec2<Type> &rotate(const Vec2<Type>& hotspot, Type angle);
204 
210  Type round_value(float value) const;
211 
216  bool is_equal(const Vec2<Type> &other, Type epsilon) const { return Vec2<Type>::is_equal(*this, other, epsilon); }
217 
219  void operator += (const Vec2<Type>& vector) { x += vector.x; y += vector.y; }
220 
222  void operator += (Type value) { x += value; y += value; }
223 
225  void operator -= (const Vec2<Type>& vector) { x -= vector.x; y -= vector.y; }
226 
228  void operator -= (Type value) { x -= value; y -= value; }
229 
231  Vec2<Type> operator - () const { return Vec2<Type>(-x, -y); }
232 
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 Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
247 
249  bool operator == (const Vec2<Type>& vector) const { return ((x == vector.x) && (y == vector.y)); }
250 
252  bool operator != (const Vec2<Type>& vector) const { return ((x != vector.x) || (y != vector.y)); }
253 
255  bool operator < (const Vec2<Type>& vector) const { return y < vector.y || (y == vector.y && x < vector.x); }
256  };
257 
259  template<typename Type>
260  Vec2<Type> operator + (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x + v2.x, v1.y + v2.y); }
261 
263  template<typename Type>
264  Vec2<Type> operator + (Type s, const Vec2<Type>& v) { return Vec2<Type>(s + v.x, s + v.y); }
265 
267  template<typename Type>
268  Vec2<Type> operator + (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x + s, v.y + s); }
269 
271  template<typename Type>
272  Vec2<Type> operator - (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x - v2.x, v1.y - v2.y); }
273 
275  template<typename Type>
276  Vec2<Type> operator - (Type s, const Vec2<Type>& v) { return Vec2<Type>(s - v.x, s - v.y); }
277 
279  template<typename Type>
280  Vec2<Type> operator - (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x - s, v.y - s); }
281 
283  template<typename Type>
284  Vec2<Type> operator * (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x * v2.x, v1.y * v2.y); }
285 
287  template<typename Type>
288  Vec2<Type> operator * (Type s, const Vec2<Type>& v) { return Vec2<Type>(s * v.x, s * v.y); }
289 
291  template<typename Type>
292  Vec2<Type> operator * (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x * s, v.y * s); }
293 
295  template<typename Type>
296  Vec2<Type> operator / (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x / v2.x, v1.y / v2.y); }
297 
299  template<typename Type>
300  Vec2<Type> operator / (Type s, const Vec2<Type>& v) { return Vec2<Type>(s / v.x, s / v.y); }
301 
303  template<typename Type>
304  Vec2<Type> operator / (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x / s, v.y / s); }
305 
306  template<typename Type>
307  Vec2<Type> operator * (const Vec2<Type>& v, const Mat2<Type>& matrix)
308  {
309  return Vec2<Type>(
310  matrix[0 * 2 + 0] * v.x + matrix[0 * 2 + 1] * v.y,
311  matrix[1 * 2 + 0] * v.x + matrix[1 * 2 + 1] * v.y);
312  }
313 
314  template<typename Type>
315  Vec2<Type> operator * (const Mat2<Type>& matrix, const Vec2<Type>& v)
316  {
317  return Vec2<Type>(
318  matrix[0 * 2 + 0] * v.x + matrix[1 * 2 + 0] * v.y,
319  matrix[0 * 2 + 1] * v.x + matrix[1 * 2 + 1] * v.y);
320  }
321 
323 
324  template<>
325  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); }
326 
327  template<>
328  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); }
329 
330  template<>
331  inline Vec2<unsigned char>::Vec2(const Vec2<int> &copy) { x = (unsigned char)copy.x; y = (unsigned char)copy.y; }
332 
333  template<>
334  inline Vec2<char>::Vec2(const Vec2<float> &copy) { x = (char)std::floor(copy.x + 0.5f); y = (char)std::floor(copy.y + 0.5f); }
335 
336  template<>
337  inline Vec2<char>::Vec2(const Vec2<double> &copy) { x = (char)std::floor(copy.x + 0.5); y = (char)std::floor(copy.y + 0.5); }
338 
339  template<>
340  inline Vec2<char>::Vec2(const Vec2<int> &copy) { x = (char)copy.x; y = (char)copy.y; }
341 
342  template<>
343  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); }
344 
345  template<>
346  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); }
347 
348  template<>
349  inline Vec2<unsigned short>::Vec2(const Vec2<int> &copy) { x = (unsigned short)copy.x; y = (unsigned short)copy.y; }
350 
351  template<>
352  inline Vec2<short>::Vec2(const Vec2<float> &copy) { x = (short)std::floor(copy.x + 0.5f); y = (short)std::floor(copy.y + 0.5f); }
353 
354  template<>
355  inline Vec2<short>::Vec2(const Vec2<double> &copy) { x = (short)std::floor(copy.x + 0.5); y = (short)std::floor(copy.y + 0.5); }
356 
357  template<>
358  inline Vec2<short>::Vec2(const Vec2<int> &copy) { x = (short)copy.x; y = (short)copy.y; }
359 
360  template<>
361  inline Vec2<int>::Vec2(const Vec2<float> &copy) { x = (int)std::floor(copy.x + 0.5f); y = (int)std::floor(copy.y + 0.5f); }
362 
363  template<>
364  inline Vec2<int>::Vec2(const Vec2<double> &copy) { x = (int)std::floor(copy.x + 0.5); y = (int)std::floor(copy.y + 0.5); }
365 
366  template<>
367  inline Vec2<int>::Vec2(const Vec2<int> &copy) { x = (int)copy.x; y = (int)copy.y; }
368 
369  template<>
370  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); }
371 
372  template<>
373  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); }
374 
375  template<>
376  inline Vec2<unsigned int>::Vec2(const Vec2<int> &copy) { x = (unsigned int)copy.x; y = (unsigned int)copy.y; }
377 
378  template<>
379  inline Vec2<float>::Vec2(const Vec2<float> &copy) { x = (float)copy.x; y = (float)copy.y; }
380 
381  template<>
382  inline Vec2<float>::Vec2(const Vec2<double> &copy) { x = (float)copy.x; y = (float)copy.y; }
383 
384  template<>
385  inline Vec2<float>::Vec2(const Vec2<int> &copy) { x = (float)copy.x; y = (float)copy.y; }
386 
387  template<>
388  inline Vec2<double>::Vec2(const Vec2<float> &copy) { x = (double)copy.x; y = (double)copy.y; }
389 
390  template<>
391  inline Vec2<double>::Vec2(const Vec2<double> &copy) { x = (double)copy.x; y = (double)copy.y; }
392 
393  template<>
394  inline Vec2<double>::Vec2(const Vec2<int> &copy) { x = (double)copy.x; y = (double)copy.y; }
395 
396  template<typename Type>
397  inline Type Vec2<Type>::length() const { return (Type)floor(sqrt(float(x*x + y*y)) + 0.5f); }
398 
399  template<>
400  inline double Vec2<double>::length() const { return sqrt(x*x + y*y); }
401 
402  template<>
403  inline float Vec2<float>::length() const { return sqrt(x*x + y*y); }
404 
405  template<typename Type>
406  inline Vec2<Type> &Vec2<Type>::normalize() { Type f = length(); if (f != 0) { x /= f; y /= f; } return *this; }
407 
408  template<typename Type>
409  inline Vec2<Type> Vec2<Type>::normalize(const Vec2<Type>& vector) { Vec2<Type> dest(vector); dest.normalize(); return dest; }
410 
412 
414  typedef Vec2<char> Vec2b;
418  typedef Vec2<int> Vec2i;
421 }
Type round_value(float value) const
Rounds a value for the datatype.
Type datatype
Definition: vec2.h:73
void operator/=(const Vec2< Type > &vector)
/= operator.
Definition: vec2.h:240
static Type dot(const Vec2< Type > &vector_1, const Vec2< Type > &vector_2)
Dot products a vector with an other vector.
Definition: vec2.h:104
Vec2()
Definition: vec2.h:78
2D matrix
Definition: mat2.h:41
Type x
Definition: vec3.h:74
2D (width,height) size structure.
Definition: size.h:51
Type distance(const Vec2< Type > &vector) const
Calculate the distance between this vector and an other vector.
Vec2(const Type *array_xy)
Definition: vec2.h:83
3D vector
Definition: line_ray.h:43
Type x
Definition: vec4.h:74
void operator+=(const Vec2< Type > &vector)
+= operator.
Definition: vec2.h:219
Type x
Definition: vec2.h:75
void operator-=(const Vec2< Type > &vector)
-= operator.
Definition: vec2.h:225
2D (x,y) point structure.
Definition: point.h:48
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.
Type angle_line(const Vec2< Type > &point) const
Calculate the angle of the line joining this point and other point.
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:272
Vec2(const Type &p1, const Type &p2)
Definition: vec2.h:82
bool is_equal(const Vec2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec2.h:216
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:260
Vec2< Type > & normalize()
Normalizes this vector.
Definition: vec2.h:406
Type angle_normed(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors...
Type y
Definition: vec2.h:76
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec2.h:397
Type y
Definition: vec4.h:75
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:133
bool operator==(const Vec2< Type > &vector) const
== operator.
Definition: vec2.h:249
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:296
bool operator!=(const Vec2< Type > &vector) const
!= operator.
Definition: vec2.h:252
Type angle(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector.
Vec2< Type > & operator=(const Vec2< Type > &vector)
= operator.
Definition: vec2.h:246
Vec2< Type > operator-() const
operator.
Definition: vec2.h:231
Vec2(const Vec3< Type > &copy)
Definition: vec2.h:80
void operator*=(const Vec2< Type > &vector)
*= operator.
Definition: vec2.h:234
Vec2< Type > & round()
Rounds all components of this vector.
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:284
Type y
Definition: vec3.h:75
Type dot(const Vec2< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec2.h:160
Vec2(const Vec4< Type > &copy)
Definition: vec2.h:81
Vec2(const Type &scalar)
Definition: vec2.h:79
Origin
Alignment origins.
Definition: origin.h:35
Definition: Application/application.h:35
4D vector
Definition: size.h:44
static Vec2< Type > rotate(const Vec2< Type > &vector, const Vec2< Type > &hotspot, Type angle)
Rotate a vector around another point.