vec2.h Source File - ClanLib SDK
Description: vec2.h Source File

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<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
160 Vec2<Type> &normalize();
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
351 typedef Vec2<unsigned char> Vec2ub;
352 typedef Vec2<char> Vec2b;
353 typedef Vec2<unsigned short> Vec2us;
354 typedef Vec2<short> Vec2s;
355 typedef Vec2<unsigned int> Vec2ui;
356 typedef Vec2<int> Vec2i;
357 typedef Vec2<float> Vec2f;
358 typedef Vec2<double> Vec2d;
359
361}
clan::Angle
Angle class.
Definition angle.h:60
clan::Mat2
2D matrix
Definition mat2.h:59
clan::Mat3
3D matrix
Definition mat3.h:60
clan::Mat4
4D matrix
Definition mat4.h:78
clan::Pointx
2D (x,y) point structure.
Definition point.h:52
clan::Sizex
2D (width,height) size structure.
Definition size.h:55
clan::Vec2
2D vector
Definition vec2.h:76
clan::Vec2::calc_origin
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.
clan::Vec2< unsigned char >::g
unsigned char g
Definition vec2.h:81
clan::Vec2< unsigned char >::y
unsigned char y
Definition vec2.h:81
clan::Vec2::angle_normed
Angle angle_normed(const Vec2< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors.
clan::Vec2::operator!=
bool operator!=(const Vec2< Type > &vector) const
!= operator.
Definition vec2.h:261
clan::Vec2::round
static Vec2< Type > round(const Vec2< Type > &vector)
Rounds all components on a vector.
clan::Vec2::operator-
Vec2< Type > operator-() const
operator.
Definition vec2.h:240
clan::Vec2::operator/=
void operator/=(const Vec2< Type > &vector)
/= operator.
Definition vec2.h:249
clan::Vec2::Vec2
Vec2(const Vec2< Type > &copy)=default
clan::Vec2::dot
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
clan::Vec2::round_value
Type round_value(float value) const
Rounds a value for the datatype.
clan::Vec2::Vec2
Vec2(const Type &scalar)
Definition vec2.h:84
clan::Vec2::Vec2
Vec2(const Type &p1, const Type &p2)
Definition vec2.h:87
clan::Vec2::Vec2
Vec2(const Vec3< Type > &copy)
Definition vec2.h:85
clan::Vec2::Vec2
Vec2()
Definition vec2.h:83
clan::Vec2< unsigned char >::t
unsigned char t
Definition vec2.h:81
clan::Vec2::operator==
bool operator==(const Vec2< Type > &vector) const
== operator.
Definition vec2.h:258
clan::Vec2::is_equal
bool is_equal(const Vec2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition vec2.h:225
clan::Vec2::distance
Type distance(const Vec2< Type > &vector) const
Calculate the distance between this vector and an other vector.
clan::Vec2::operator<
bool operator<(const Vec2< Type > &vector) const
< operator.
Definition vec2.h:264
clan::Vec2::operator+=
void operator+=(const Vec2< Type > &vector)
+= operator.
Definition vec2.h:228
clan::Vec2::operator*=
void operator*=(const Vec2< Type > &vector)
*= operator.
Definition vec2.h:243
clan::Vec2::is_equal
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
clan::Vec2< unsigned char >::s
unsigned char s
Definition vec2.h:80
clan::Vec2::dot
Type dot(const Vec2< Type > &vector) const
Dot products this vector with an other vector.
Definition vec2.h:169
clan::Vec2::rotate
static Vec2< Type > rotate(const Vec2< Type > &vector, const Vec2< Type > &hotspot, const Angle &angle)
Rotate a vector around another point.
clan::Vec2< unsigned char >::angle
Angle angle(const Vec2< unsigned char > &vector) const
clan::Vec2::Vec2
Vec2(const Type *array_xy)
Definition vec2.h:88
clan::Vec2::datatype
Type datatype
Definition vec2.h:78
clan::Vec2::rotate
Vec2< Type > & rotate(const Vec2< Type > &hotspot, const Angle &angle)
Rotate this vector around another point.
clan::Vec2< unsigned char >::x
unsigned char x
Definition vec2.h:80
clan::Vec2::Vec2
Vec2(const Vec2< OtherType > &copy)
Definition vec2.h:93
clan::Vec2< unsigned char >::r
unsigned char r
Definition vec2.h:80
clan::Vec2::angle_line
Angle angle_line(const Vec2< Type > &point) const
Calculate the angle of the line joining this point and other point.
clan::Vec2::Vec2
Vec2(const Vec4< Type > &copy)
Definition vec2.h:86
clan::Vec2::operator=
Vec2< Type > & operator=(const Vec2< Type > &vector)=default
= operator.
clan::Vec2::round
Vec2< Type > & round()
Rounds all components of this vector.
clan::Vec2::operator-=
void operator-=(const Vec2< Type > &vector)
-= operator.
Definition vec2.h:234
clan::Vec3
3D vector
Definition vec3.h:75
clan::Vec4
4D vector
Definition vec4.h:75
clan::operator/
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition vec2.h:306
clan::Vec2::normalize
Vec2< Type > & normalize()
Normalizes this vector.
Definition vec2.h:344
clan::Vec2ui
Vec2< unsigned int > Vec2ui
Definition vec2.h:355
clan::Vec2us
Vec2< unsigned short > Vec2us
Definition vec2.h:353
clan::Vec2d
Vec2< double > Vec2d
Definition vec2.h:358
clan::Vec2::length
Type length() const
Returns the length (magnitude) of this vector.
Definition vec2.h:335
clan::Vec2b
Vec2< char > Vec2b
Definition vec2.h:352
clan::operator-
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:282
clan::Vec2::normalize
static Vec2< Type > normalize(const Vec2< Type > &vector)
Normalizes a vector.
Definition vec2.h:347
clan::operator+
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:270
clan::Vec2i
Vec2< int > Vec2i
Definition vec2.h:356
clan::Origin
Origin
Alignment origins.
Definition origin.h:39
clan::Vec2f
Vec2< float > Vec2f
Definition vec2.h:357
clan::Vec2ub
Vec2< unsigned char > Vec2ub
Definition vec2.h:351
clan::operator*
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:294
clan::Vec2s
Vec2< short > Vec2s
Definition vec2.h:354
clan::FillMode::point
@ point
Definition graphic_context.h:78
clan
Definition clanapp.h:36
clan::StyleValueType::length
@ length
value is a keyword
Definition style_value_type.h:38
clan::Key::s
@ s
Definition keys.h:99
clan::Key::f
@ f
Definition keys.h:86
clan::Key::v
@ v
Definition keys.h:102