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
38namespace 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
149 Type length() const;
150
157
165 Type dot(const Vec2<Type>& vector) const { return x*vector.x + y*vector.y; }
166
172 Angle angle(const Vec2<Type>& vector) const;
173
179 Angle angle_normed(const Vec2<Type>& vector) const;
180
187
193 Type distance(const Vec2<Type>& vector) const;
194
201
208 Vec2<Type> &rotate(const Vec2<Type>& hotspot, const Angle &angle);
209
215 Type round_value(float value) const;
216
221 bool is_equal(const Vec2<Type> &other, Type epsilon) const { return Vec2<Type>::is_equal(*this, other, epsilon); }
222
224 void operator += (const Vec2<Type>& vector) { x += vector.x; y += vector.y; }
225
227 void operator += (Type value) { x += value; y += value; }
228
230 void operator -= (const Vec2<Type>& vector) { x -= vector.x; y -= vector.y; }
231
233 void operator -= (Type value) { x -= value; y -= value; }
234
236 Vec2<Type> operator - () const { return Vec2<Type>(-x, -y); }
237
239 void operator *= (const Vec2<Type>& vector) { x *= vector.x; y *= vector.y; }
240
242 void operator *= (Type value) { x *= value; y *= value; }
243
245 void operator /= (const Vec2<Type>& vector) { x /= vector.x; y /= vector.y; }
246
248 void operator /= (Type value) { x /= value; y /= value; }
249
251 Vec2<Type> &operator = (const Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
252
254 bool operator == (const Vec2<Type>& vector) const { return ((x == vector.x) && (y == vector.y)); }
255
257 bool operator != (const Vec2<Type>& vector) const { return ((x != vector.x) || (y != vector.y)); }
258
260 bool operator < (const Vec2<Type>& vector) const { return y < vector.y || (y == vector.y && x < vector.x); }
262 };
263
265 template<typename Type>
266 Vec2<Type> operator + (const Vec2<Type>& v1, const Vec2<Type>& v2) { return Vec2<Type>(v1.x + v2.x, v1.y + v2.y); }
267
269 template<typename Type>
270 Vec2<Type> operator + (Type s, const Vec2<Type>& v) { return Vec2<Type>(s + v.x, s + v.y); }
271
273 template<typename Type>
274 Vec2<Type> operator + (const Vec2<Type>& v, Type s) { return Vec2<Type>(v.x + s, v.y + s); }
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
312 template<typename Type>
314 {
315 return Vec2<Type>(
316 matrix[0 * 2 + 0] * v.x + matrix[0 * 2 + 1] * v.y,
317 matrix[1 * 2 + 0] * v.x + matrix[1 * 2 + 1] * v.y);
318 }
319
320 template<typename Type>
322 {
323 return Vec2<Type>(
324 matrix[0 * 2 + 0] * v.x + matrix[1 * 2 + 0] * v.y,
325 matrix[0 * 2 + 1] * v.x + matrix[1 * 2 + 1] * v.y);
326 }
327
329
330 template<>
331 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); }
332
333 template<>
334 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); }
335
336 template<>
337 inline Vec2<unsigned char>::Vec2(const Vec2<int> &copy) { x = (unsigned char)copy.x; y = (unsigned char)copy.y; }
338
339 template<>
340 inline Vec2<char>::Vec2(const Vec2<float> &copy) { x = (char)std::floor(copy.x + 0.5f); y = (char)std::floor(copy.y + 0.5f); }
341
342 template<>
343 inline Vec2<char>::Vec2(const Vec2<double> &copy) { x = (char)std::floor(copy.x + 0.5); y = (char)std::floor(copy.y + 0.5); }
344
345 template<>
346 inline Vec2<char>::Vec2(const Vec2<int> &copy) { x = (char)copy.x; y = (char)copy.y; }
347
348 template<>
349 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); }
350
351 template<>
352 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); }
353
354 template<>
355 inline Vec2<unsigned short>::Vec2(const Vec2<int> &copy) { x = (unsigned short)copy.x; y = (unsigned short)copy.y; }
356
357 template<>
358 inline Vec2<short>::Vec2(const Vec2<float> &copy) { x = (short)std::floor(copy.x + 0.5f); y = (short)std::floor(copy.y + 0.5f); }
359
360 template<>
361 inline Vec2<short>::Vec2(const Vec2<double> &copy) { x = (short)std::floor(copy.x + 0.5); y = (short)std::floor(copy.y + 0.5); }
362
363 template<>
364 inline Vec2<short>::Vec2(const Vec2<int> &copy) { x = (short)copy.x; y = (short)copy.y; }
365
366 template<>
367 inline Vec2<int>::Vec2(const Vec2<float> &copy) { x = (int)std::floor(copy.x + 0.5f); y = (int)std::floor(copy.y + 0.5f); }
368
369 template<>
370 inline Vec2<int>::Vec2(const Vec2<double> &copy) { x = (int)std::floor(copy.x + 0.5); y = (int)std::floor(copy.y + 0.5); }
371
372 template<>
373 inline Vec2<int>::Vec2(const Vec2<int> &copy) { x = (int)copy.x; y = (int)copy.y; }
374
375 template<>
376 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); }
377
378 template<>
379 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); }
380
381 template<>
382 inline Vec2<unsigned int>::Vec2(const Vec2<int> &copy) { x = (unsigned int)copy.x; y = (unsigned int)copy.y; }
383
384 template<>
385 inline Vec2<float>::Vec2(const Vec2<float> &copy) { x = (float)copy.x; y = (float)copy.y; }
386
387 template<>
388 inline Vec2<float>::Vec2(const Vec2<double> &copy) { x = (float)copy.x; y = (float)copy.y; }
389
390 template<>
391 inline Vec2<float>::Vec2(const Vec2<int> &copy) { x = (float)copy.x; y = (float)copy.y; }
392
393 template<>
394 inline Vec2<double>::Vec2(const Vec2<float> &copy) { x = (double)copy.x; y = (double)copy.y; }
395
396 template<>
397 inline Vec2<double>::Vec2(const Vec2<double> &copy) { x = (double)copy.x; y = (double)copy.y; }
398
399 template<>
400 inline Vec2<double>::Vec2(const Vec2<int> &copy) { x = (double)copy.x; y = (double)copy.y; }
401
402 template<typename Type>
403 inline Type Vec2<Type>::length() const { return (Type)floor(sqrt(float(x*x + y*y)) + 0.5f); }
404
405 template<>
406 inline double Vec2<double>::length() const { return sqrt(x*x + y*y); }
407
408 template<>
409 inline float Vec2<float>::length() const { return sqrt(x*x + y*y); }
410
411 template<typename Type>
412 inline Vec2<Type> &Vec2<Type>::normalize() { Type f = length(); if (f != 0) { x /= f; y /= f; } return *this; }
413
414 template<typename Type>
415 inline Vec2<Type> Vec2<Type>::normalize(const Vec2<Type>& vector) { Vec2<Type> dest(vector); dest.normalize(); return dest; }
416
418
427
429}
Angle class.
Definition angle.h:60
2D matrix
Definition mat2.h:59
2D (x,y) point structure.
Definition point.h:52
2D (width,height) size structure.
Definition size.h:55
2D vector
Definition vec2.h:76
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 Vec2< double > &copy)
Type y
Definition vec2.h:81
Angle angle_normed(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors.
bool operator!=(const Vec2< Type > &vector) const
!= operator.
Definition vec2.h:257
static Vec2< Type > round(const Vec2< Type > &vector)
Rounds all components on a vector.
Vec2< Type > operator-() const
operator.
Definition vec2.h:236
void operator/=(const Vec2< Type > &vector)
/= operator.
Definition vec2.h:245
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 round_value(float value) const
Rounds a value for the datatype.
Vec2(const Type &scalar)
Definition vec2.h:84
Vec2(const Type &p1, const Type &p2)
Definition vec2.h:87
Vec2(const Vec3< Type > &copy)
Definition vec2.h:85
Vec2()
Definition vec2.h:83
Type t
Definition vec2.h:81
bool operator==(const Vec2< Type > &vector) const
== operator.
Definition vec2.h:254
bool is_equal(const Vec2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition vec2.h:221
Type distance(const Vec2< Type > &vector) const
Calculate the distance between this vector and an other vector.
Vec2< Type > & operator=(const Vec2< Type > &vector)
= operator.
Definition vec2.h:251
bool operator<(const Vec2< Type > &vector) const
< operator.
Definition vec2.h:260
void operator+=(const Vec2< Type > &vector)
+= operator.
Definition vec2.h:224
void operator*=(const Vec2< Type > &vector)
*= operator.
Definition vec2.h:239
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
Type s
Definition vec2.h:80
Type dot(const Vec2< Type > &vector) const
Dot products this vector with an other vector.
Definition vec2.h:165
static Vec2< Type > rotate(const Vec2< Type > &vector, const Vec2< Type > &hotspot, const Angle &angle)
Rotate a vector around another point.
Vec2(const Vec2< float > &copy)
Angle angle(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector.
Vec2(const Type *array_xy)
Definition vec2.h:88
Type datatype
Definition vec2.h:78
Vec2< Type > & rotate(const Vec2< Type > &hotspot, const Angle &angle)
Rotate this vector around another point.
Type x
Definition vec2.h:80
Type r
Definition vec2.h:80
Angle angle_line(const Vec2< Type > &point) const
Calculate the angle of the line joining this point and other point.
Vec2(const Vec4< Type > &copy)
Definition vec2.h:86
Vec2< Type > & round()
Rounds all components of this vector.
Vec2(const Vec2< int > &copy)
void operator-=(const Vec2< Type > &vector)
-= operator.
Definition vec2.h:230
3D vector
Definition vec3.h:75
Type y
Definition vec3.h:80
Type x
Definition vec3.h:79
4D vector
Definition vec4.h:75
Type y
Definition vec4.h:80
Type x
Definition vec4.h:79
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition vec2.h:302
Vec2< Type > & normalize()
Normalizes this vector.
Definition vec2.h:412
Vec2< unsigned int > Vec2ui
Definition vec2.h:423
Vec2< unsigned short > Vec2us
Definition vec2.h:421
Vec2< double > Vec2d
Definition vec2.h:426
Type length() const
Returns the length (magnitude) of this vector.
Definition vec2.h:403
Vec2< char > Vec2b
Definition vec2.h:420
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:278
static Vec2< Type > normalize(const Vec2< Type > &vector)
Normalizes a vector.
Definition vec2.h:415
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:266
Vec2< int > Vec2i
Definition vec2.h:424
Origin
Alignment origins.
Definition origin.h:39
Vec2< float > Vec2f
Definition vec2.h:425
Vec2< unsigned char > Vec2ub
Definition vec2.h:419
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:290
Vec2< short > Vec2s
Definition vec2.h:422
Definition clanapp.h:36
@ length
value is a keyword
@ angle
value is a color