forked from FranciscoBiaso/TileMapEditorRealmz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec2.h
219 lines (196 loc) · 4 KB
/
Vec2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#pragma once
#include "Definitions.h"
#include <ostream>
namespace math {
/*!
Vec2 class
==========
This class represents a vec2(x,y) strucutre and its basic operations.
*/
template <class T>
class Vec2
{
private:
T x, y;
public:
/**
* default constructor.
*
*/
Vec2() : x(0),y(0) {}
/**
* 2º constructor.
*
* @param x position.
* @param y position.
*/
Vec2(T x, T y) : x(x), y(y) {}
/**
* 3º copy constructor.
*
* @param v Vec2 to copy.
*/
Vec2(const Vec2<T>& v) { x = v.getX(); y = v.getY(); }
/**
* @brief This method gets x position.
* \return x.
*/
T getX() const { return x; }
/**
* @brief This method gets y position.
* \return y.
*/
T getY() const { return y; }
/**
* @brief This method sets x position.
* @param x.
*/
void setX(T x) { this->x = x; }
/**
* @brief This method sets y position.
* @param y.
*/
void setY(T y) { this->y = y; }
/**
* @brief This method sets xy position.
* @param x.
* @param y.
*/
void setXY(T x, T y) { this->x = x; this->y = y;}
/**
* @brief This method gets position from index [0,1].
*/
T operator[](int index)
{
if (index < 0 || index > 1)
throw def::ReturnMsg::ARRAY_OUT_OF_RANGE;
return (index == 0) ? x : y;
}
/**
* @brief This method gets vector distance.
*/
double length()
{
return std::sqrt(getX() * getX() + getY() * getY());
}
/**
* @brief This method normalizes vec.
*/
Vec2<T>& normalize()
{
this->x = this->x / length();
this->y = this->y / length();
return *this;
}
/**
* @brief This method swaps x and y.
*/
Vec2<T>& swap()
{
T tmp = x;
x = y;
y = tmp;
return *this;
}
/**
* @brief This method checks if Vec2 is diferent from another Vec2.
* It compares x and y data.
*/
bool operator!=(const Vec2<T>& v2)
{
return x != v2.getX() || y != v2.getY() ? true : false;
}
/**
* @brief This method checks if Vec2A is equal to Vec2B.
* Do not make sense with double type.
*/
bool operator==(const Vec2<T>& v2)
{
return x == v2.getX() && y == v2.getY() ? true : false;
}
/**
* @brief This method overloads the multiplication operator.
*/
Vec2<T> operator*(const double & mult)
{
//Vec2<T> result = *this; // Make a copy of myself. Same as MyClass result(*this);
//result.setXY(result.getX() * mult, result.getY() * mult);
return Vec2(this->getX() * mult, this->getY() * mult);
}
/**
* @brief This method overloads the assignment operator.
*/
Vec2<T>& operator=(const Vec2<T>& v2)
{
this->x = v2.getX();
this->y = v2.getY();
return *this;
}
/**
* @brief This method overloads the - operator.
*/
Vec2<T>& operator-(const Vec2<T>& v2)
{
this->x -= v2.getX();
this->y -= v2.getY();
return *this;
}
/**
* @brief This method overloads the + operator.
*/
Vec2<T>& operator+(const Vec2<T>& v2)
{
this->x += v2.getX();
this->y += v2.getY();
return *this;
}
/**
* @brief This method increments the x and y position as a matrix of width mod.
*/
void rightShiftCursor(int mod)
{
if (getX() == (mod - 1))
{
setX(0);
setY(getY() + 1);
}
else
setX((getX() + 1) % mod);
}
/**
* @brief This method decrements the x and y position as a matrix of width mod.
*/
void leftShiftCursor(int mod)
{
if (getX() == 0)
{
if (getY() > 0)
{
setY(getY() - 1);
setX(mod - 1);
}
}
else
setX(getX() - 1);
}
/**
* @brief This method executes n times rightShiftCursor.
* @see rightShiftCursor
*/
void rightShiftCursorNTimes(int x, int mod)
{
for (int i = 0; i < x; i++)
rightShiftCursor(mod);
}
/**
* @brief This method executes n times leftShiftCursor.
* @see leftShiftCursor
*/
void leftShiftCursorNTimes(int x, int mod)
{
for (int i = 0; i < x; i++)
leftShiftCursor(mod);
}
//friend std::ostream& operator<<(std::ostream& os, const Vec2& dt);
};
}