vec2.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2020 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<Type> &copy) = default;
91 
92  template<typename OtherType, typename std::enable_if_t<std::is_integral<Type>::value && !std::is_integral<OtherType>::value, int> = 0>
93  Vec2(const Vec2<OtherType>& copy) : x(static_cast<Type>(std::floor(copy.x + 0.5f))), y(static_cast<Type>(std::floor(copy.y + 0.5f))) {}
94 
95  template<typename OtherType, typename std::enable_if_t<!std::is_integral<Type>::value || std::is_integral<OtherType>::value, int> = 0>
96  Vec2(const Vec2<OtherType>& copy) : x(static_cast<Type>(copy.x)), y(static_cast<Type>(copy.y)) {}
97 
104  static Vec2<Type> normalize(const Vec2<Type>& vector);
105 
113  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; }
114 
121  static Vec2<Type> round(const Vec2<Type>& vector);
122 
128  static Vec2<Type> rotate(const Vec2<Type>& vector, const Vec2<Type>& hotspot, const Angle &angle);
129 
135  static Pointx<Type> calc_origin(Origin origin, const Sizex<Type> &size);
136 
142  static bool is_equal(const Vec2<Type> &first, const Vec2<Type> &second, Type epsilon)
143  {
144  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y;
145  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon);
146  }
147 
153  Type length() const;
154 
161 
169  Type dot(const Vec2<Type>& vector) const { return x*vector.x + y*vector.y; }
170 
176  Angle angle(const Vec2<Type>& vector) const;
177 
183  Angle angle_normed(const Vec2<Type>& vector) const;
184 
190  Angle angle_line(const Vec2<Type>& point) const;
191 
197  Type distance(const Vec2<Type>& vector) const;
198 
204  Vec2<Type> &round();
205 
212  Vec2<Type> &rotate(const Vec2<Type>& hotspot, const Angle &angle);
213 
219  Type round_value(float value) const;
220 
225  bool is_equal(const Vec2<Type> &other, Type epsilon) const { return Vec2<Type>::is_equal(*this, other, epsilon); }
226 
228  void operator += (const Vec2<Type>& vector) { x += vector.x; y += vector.y; }
229 
231  void operator += (Type value) { x += value; y += value; }
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  Vec2<Type> operator - () const { return Vec2<Type>(-x, -y); }
241 
243  void operator *= (const Vec2<Type>& vector) { x *= vector.x; y *= vector.y; }
244 
246  void operator *= (Type value) { x *= value; y *= value; }
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  Vec2<Type> &operator = (const Vec2<Type>& vector) = default;
256 
258  bool operator == (const Vec2<Type>& vector) const { return ((x == vector.x) && (y == vector.y)); }
259 
261  bool operator != (const Vec2<Type>& vector) const { return ((x != vector.x) || (y != vector.y)); }
262 
264  bool operator < (const Vec2<Type>& vector) const { return y < vector.y || (y == vector.y && x < vector.x); }
266  };
267 
269  template<typename Type>
270  Vec2<Type> operator + (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x + v2.x, v1.y + v2.y); }
271 
273  template<typename Type>
274  Vec2<Type> operator + (Type s, const Vec2<Type>& v) { return Vec2<Type>(s + v.x, s + v.y); }
275 
277  template<typename Type>
278  Vec2<Type> operator + (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x + s, v.y + s); }
279 
281  template<typename Type>
282  Vec2<Type> operator - (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x - v2.x, v1.y - v2.y); }
283 
285  template<typename Type>
286  Vec2<Type> operator - (Type s, const Vec2<Type>& v) { return Vec2<Type>(s - v.x, s - v.y); }
287 
289  template<typename Type>
290  Vec2<Type> operator - (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x - s, v.y - s); }
291 
293  template<typename Type>
294  Vec2<Type> operator * (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x * v2.x, v1.y * v2.y); }
295 
297  template<typename Type>
298  Vec2<Type> operator * (Type s, const Vec2<Type>& v) { return Vec2<Type>(s * v.x, s * v.y); }
299 
301  template<typename Type>
302  Vec2<Type> operator * (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x * s, v.y * s); }
303 
305  template<typename Type>
306  Vec2<Type> operator / (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x / v2.x, v1.y / v2.y); }
307 
309  template<typename Type>
310  Vec2<Type> operator / (Type s, const Vec2<Type>& v) { return Vec2<Type>(s / v.x, s / v.y); }
311 
313  template<typename Type>
314  Vec2<Type> operator / (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x / s, v.y / s); }
315 
316  template<typename Type>
317  Vec2<Type> operator * (const Vec2<Type>& v, const Mat2<Type>& matrix)
318  {
319  return Vec2<Type>(
320  matrix[0 * 2 + 0] * v.x + matrix[0 * 2 + 1] * v.y,
321  matrix[1 * 2 + 0] * v.x + matrix[1 * 2 + 1] * v.y);
322  }
323 
324  template<typename Type>
325  Vec2<Type> operator * (const Mat2<Type>& matrix, const Vec2<Type>& v)
326  {
327  return Vec2<Type>(
328  matrix[0 * 2 + 0] * v.x + matrix[1 * 2 + 0] * v.y,
329  matrix[0 * 2 + 1] * v.x + matrix[1 * 2 + 1] * v.y);
330  }
331 
333 
334  template<typename Type>
335  inline Type Vec2<Type>::length() const { return (Type)floor(sqrt(float(x*x + y*y)) + 0.5f); }
336 
337  template<>
338  inline double Vec2<double>::length() const { return sqrt(x*x + y*y); }
339 
340  template<>
341  inline float Vec2<float>::length() const { return sqrt(x*x + y*y); }
342 
343  template<typename Type>
344  inline Vec2<Type> &Vec2<Type>::normalize() { Type f = length(); if (f != 0) { x /= f; y /= f; } return *this; }
345 
346  template<typename Type>
347  inline Vec2<Type> Vec2<Type>::normalize(const Vec2<Type>& vector) { Vec2<Type> dest(vector); dest.normalize(); return dest; }
348 
350 
352  typedef Vec2<char> Vec2b;
356  typedef Vec2<int> Vec2i;
359 
361 }
Definition: clanapp.h:35
Vec2< float > Vec2f
Definition: vec2.h:357
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 g
Definition: vec2.h:81
Vec2(const Vec3< Type > &copy)
Definition: vec2.h:85
Angle class.
Definition: angle.h:59
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec2.h:335
Vec2< Type > & operator=(const Vec2< Type > &vector)=default
= operator.
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:142
Vec2< Type > & round()
Rounds all components of this vector.
bool operator!=(const Vec2< Type > &vector) const
!= operator.
Definition: vec2.h:261
Vec2< unsigned char > Vec2ub
Definition: vec2.h:351
Vec2< Type > operator-() const
operator.
Definition: vec2.h:240
Origin
Alignment origins.
Definition: origin.h:38
Vec2< short > Vec2s
Definition: vec2.h:354
STL namespace.
Vec2< unsigned short > Vec2us
Definition: vec2.h:353
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:306
Vec2< int > Vec2i
Definition: vec2.h:356
Vec2(const Vec4< Type > &copy)
Definition: vec2.h:86
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 Vec2< OtherType > &copy)
Definition: vec2.h:93
void operator+=(const Vec2< Type > &vector)
+= operator.
Definition: vec2.h:228
Vec2< char > Vec2b
Definition: vec2.h:352
bool is_equal(const Vec2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec2.h:225
Vec2(const Type *array_xy)
Definition: vec2.h:88
Type s
Definition: vec2.h:80
Vec2< double > Vec2d
Definition: vec2.h:358
Type round_value(float value) const
Rounds a value for the datatype.
Vec2(const Type &p1, const Type &p2)
Definition: vec2.h:87
Type t
Definition: vec2.h:81
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:270
void operator*=(const Vec2< Type > &vector)
*= operator.
Definition: vec2.h:243
2D matrix
Definition: mat2.h:44
Type r
Definition: vec2.h:80
void operator-=(const Vec2< Type > &vector)
-= operator.
Definition: vec2.h:234
Vec2(const Type &scalar)
Definition: vec2.h:84
Angle angle(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector.
static Vec2< Type > rotate(const Vec2< Type > &vector, const Vec2< Type > &hotspot, const Angle &angle)
Rotate a vector around another point.
2D vector
Definition: line.h:46
Type y
Definition: vec2.h:81
value is a keyword
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:294
3D vector
Definition: line_ray.h:46
Type datatype
Definition: vec2.h:78
Type x
Definition: vec2.h:80
Angle angle_line(const Vec2< Type > &point) const
Calculate the angle of the line joining this point and other point.
void operator/=(const Vec2< Type > &vector)
/= operator.
Definition: vec2.h:249
bool operator==(const Vec2< Type > &vector) const
== operator.
Definition: vec2.h:258
Type distance(const Vec2< Type > &vector) const
Calculate the distance between this vector and an other vector.
2D (x,y) point structure.
Definition: point.h:51
4D vector
Definition: size.h:47
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:282
Vec2()
Definition: vec2.h:83
Type dot(const Vec2< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec2.h:169
Vec2< unsigned int > Vec2ui
Definition: vec2.h:355
static Type dot(const Vec2< Type > &vector_1, const Vec2< Type > &vector_2)
Dot products a vector with an other vector.
Definition: vec2.h:113
Vec2< Type > & normalize()
Normalizes this vector.
Definition: vec2.h:344
2D (width,height) size structure.
Definition: size.h:54