forked from RobLoach/raylib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.hpp
More file actions
398 lines (317 loc) · 10.7 KB
/
Vector2.hpp
File metadata and controls
398 lines (317 loc) · 10.7 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#ifndef RAYLIB_CPP_INCLUDE_VECTOR2_HPP_
#define RAYLIB_CPP_INCLUDE_VECTOR2_HPP_
#ifndef RAYLIB_CPP_NO_MATH
#include <cmath>
#endif
#include <string>
#include "./raylib-cpp-utils.hpp"
#include "./raylib.hpp"
#include "./raymath.hpp"
namespace raylib {
/**
* Vector2 type
*/
class Vector2 : public ::Vector2 {
public:
Vector2(const ::Vector2& vec) : ::Vector2{vec.x, vec.y} {}
Vector2(float x, float y) : ::Vector2{x, y} {}
Vector2(float x) : ::Vector2{x, 0} {}
Vector2() : ::Vector2{0, 0} {}
GETTERSETTER(float, X, x)
GETTERSETTER(float, Y, y)
/**
* Set the Vector2 to the same as the given Vector2.
*/
Vector2& operator=(const ::Vector2& vector2) {
set(vector2);
return *this;
}
/**
* Determine whether or not the vectors are equal.
*/
bool operator==(const ::Vector2& other) const { return x == other.x && y == other.y; }
/**
* Determines if the vectors are not equal.
*/
bool operator!=(const ::Vector2& other) const { return !(*this == other); }
[[nodiscard]] std::string ToString() const { return TextFormat("Vector2(%f, %f)", x, y); }
operator std::string() const { return ToString(); }
#ifndef RAYLIB_CPP_NO_MATH
/**
* Add two vectors (v1 + v2)
*/
Vector2 Add(const ::Vector2& vector2) const { return Vector2Add(*this, vector2); }
/**
* Add two vectors (v1 + v2)
*/
Vector2 operator+(const ::Vector2& vector2) const { return Vector2Add(*this, vector2); }
/**
* Add two vectors (v1 + v2)
*/
Vector2& operator+=(const ::Vector2& vector2) {
set(Vector2Add(*this, vector2));
return *this;
}
/**
* Add vector and float value
*/
Vector2 Add(float value) const {
return Vector2AddValue(*this, value);
}
/**
* Add vector and float value
*/
Vector2 operator+(float value) const {
return Vector2AddValue(*this, value);
}
/**
* Add vector and float value
*/
Vector2& operator+=(float value) {
set(Vector2AddValue(*this, value));
return *this;
}
/**
* Subtract two vectors (v1 - v2)
*/
[[nodiscard]] Vector2 Subtract(const ::Vector2& vector2) const { return Vector2Subtract(*this, vector2); }
/**
* Subtract two vectors (v1 - v2)
*/
Vector2 operator-(const ::Vector2& vector2) const { return Vector2Subtract(*this, vector2); }
/**
* Subtract two vectors (v1 - v2)
*/
Vector2& operator-=(const ::Vector2& vector2) {
set(Vector2Subtract(*this, vector2));
return *this;
}
/**
* Subtract vector by float value
*/
[[nodiscard]] Vector2 Subtract(float value) const {
return Vector2SubtractValue(*this, value);
}
/**
* Subtract vector by float value
*/
Vector2 operator-(float value) const {
return Vector2SubtractValue(*this, value);
}
/**
* Subtract vector by float value
*/
Vector2& operator-=(float value) {
set(Vector2SubtractValue(*this, value));
return *this;
}
/**
* Negate vector
*/
[[nodiscard]] Vector2 Negate() const { return Vector2Negate(*this); }
/**
* Negate vector
*/
Vector2 operator-() const { return Vector2Negate(*this); }
/**
* Multiply vector by vector
*/
[[nodiscard]] Vector2 Multiply(const ::Vector2& vector2) const { return Vector2Multiply(*this, vector2); }
/**
* Multiply vector by vector
*/
Vector2 operator*(const ::Vector2& vector2) const { return Vector2Multiply(*this, vector2); }
/**
* Multiply vector by vector
*/
Vector2& operator*=(const ::Vector2& vector2) {
set(Vector2Multiply(*this, vector2));
return *this;
}
/**
* Scale vector (multiply by value)
*/
[[nodiscard]] Vector2 Scale(const float scale) const { return Vector2Scale(*this, scale); }
/**
* Scale vector (multiply by value)
*/
Vector2 operator*(const float scale) const { return Vector2Scale(*this, scale); }
/**
* Scale vector (multiply by value)
*/
Vector2& operator*=(const float scale) {
set(Vector2Scale(*this, scale));
return *this;
}
/**
* Divide vector by vector
*/
[[nodiscard]] Vector2 Divide(const ::Vector2& vector2) const { return Vector2Divide(*this, vector2); }
/**
* Divide vector by vector
*/
Vector2 operator/(const ::Vector2& vector2) const { return Vector2Divide(*this, vector2); }
/**
* Divide vector by vector
*/
Vector2& operator/=(const ::Vector2& vector2) {
set(Vector2Divide(*this, vector2));
return *this;
}
/**
* Divide vector by value
*/
[[nodiscard]] Vector2 Divide(const float div) const { return ::Vector2{x / div, y / div}; }
/**
* Divide vector by value
*/
Vector2 operator/(const float div) const { return Divide(div); }
/**
* Divide vector by value
*/
Vector2& operator/=(const float div) {
this->x /= div;
this->y /= div;
return *this;
}
/**
* Normalize provided vector
*/
[[nodiscard]] Vector2 Normalize() const { return Vector2Normalize(*this); }
/**
* Transforms a Vector2 by a given Matrix
*/
[[nodiscard]] Vector2 Transform(::Matrix mat) const { return ::Vector2Transform(*this, mat); }
/**
* Calculate linear interpolation between two vectors
*/
[[nodiscard]] Vector2 Lerp(const ::Vector2& vector2, float amount) const { return Vector2Lerp(*this, vector2, amount); }
/**
* Calculate reflected vector to normal
*/
[[nodiscard]] Vector2 Reflect(const ::Vector2& normal) const { return Vector2Reflect(*this, normal); }
/**
* Rotate Vector by float in radians
*/
[[nodiscard]] Vector2 Rotate(float angle) const { return Vector2Rotate(*this, angle); }
/**
* Move Vector towards target
*/
[[nodiscard]] Vector2 MoveTowards(const ::Vector2& target, float maxDistance) const {
return Vector2MoveTowards(*this, target, maxDistance);
}
/**
* Invert the given vector
*/
[[nodiscard]] Vector2 Invert() const { return ::Vector2Invert(*this); }
/**
* Clamp the components of the vector between
*/
[[nodiscard]] Vector2 Clamp(::Vector2 min, ::Vector2 max) const { return ::Vector2Clamp(*this, min, max); }
/**
* // Clamp the magnitude of the vector between two min and max values
*/
[[nodiscard]] Vector2 Clamp(float min, float max) const { return ::Vector2ClampValue(*this, min, max); }
/**
* Check whether two given vectors are almost equal
*/
[[nodiscard]] int Equals(::Vector2 q) const { return ::Vector2Equals(*this, q); }
/**
* Calculate vector length
*/
[[nodiscard]] float Length() const { return Vector2Length(*this); }
/**
* Calculate vector square length
*/
[[nodiscard]] float LengthSqr() const { return Vector2LengthSqr(*this); }
/**
* Calculate two vectors dot product
*/
[[nodiscard]] float DotProduct(const ::Vector2& vector2) const { return Vector2DotProduct(*this, vector2); }
/**
* Calculate distance between two vectors
*/
[[nodiscard]] float Distance(const ::Vector2& vector2) const { return Vector2Distance(*this, vector2); }
/**
* Calculate square distance between two vectors
*/
[[nodiscard]] float DistanceSqr(::Vector2 v2) const { return ::Vector2DistanceSqr(*this, v2); }
/**
* Calculate angle from two vectors in X-axis
*/
[[nodiscard]] float Angle(const ::Vector2& vector2) const { return Vector2Angle(*this, vector2); }
/**
* Vector with components value 0.0f
*/
static Vector2 Zero() { return Vector2Zero(); }
/**
* Vector with components value 1.0f
*/
static Vector2 One() { return Vector2One(); }
#endif
void DrawPixel(::Color color = {0, 0, 0, 255}) const { ::DrawPixelV(*this, color); }
void DrawLine(::Vector2 endPos, ::Color color = {0, 0, 0, 255}) const { ::DrawLineV(*this, endPos, color); }
void DrawLine(::Vector2 endPos, float thick, ::Color color = {0, 0, 0, 255}) const {
::DrawLineEx(*this, endPos, thick, color);
}
void DrawLineBezier(::Vector2 endPos, float thick, ::Color color = {0, 0, 0, 255}) const {
::DrawLineBezier(*this, endPos, thick, color);
}
/**
* Draw a color-filled circle (Vector version)
*/
void DrawCircle(float radius, ::Color color = {0, 0, 0, 255}) const { ::DrawCircleV(*this, radius, color); }
void DrawRectangle(::Vector2 size, ::Color color = {0, 0, 0, 255}) const { ::DrawRectangleV(*this, size, color); }
void DrawPoly(int sides, float radius, float rotation, ::Color color = {0, 0, 0, 255}) const {
::DrawPoly(*this, sides, radius, rotation, color);
}
/**
* Check collision between two circles
*/
[[nodiscard]] bool CheckCollisionCircle(float radius1, ::Vector2 center2, float radius2) const {
return ::CheckCollisionCircles(*this, radius1, center2, radius2);
}
/**
* Check collision between circle and rectangle
*/
[[nodiscard]] bool CheckCollisionCircle(float radius, ::Rectangle rec) const {
return ::CheckCollisionCircleRec(*this, radius, rec);
}
/**
* Check if point is inside rectangle
*/
[[nodiscard]] bool CheckCollision(::Rectangle rec) const { return ::CheckCollisionPointRec(*this, rec); }
/**
* Check if point is inside circle
*/
[[nodiscard]] bool CheckCollision(::Vector2 center, float radius) const {
return ::CheckCollisionPointCircle(*this, center, radius);
}
/**
* Check if point is inside a triangle
*/
[[nodiscard]] bool CheckCollision(::Vector2 p1, ::Vector2 p2, ::Vector2 p3) const {
return ::CheckCollisionPointTriangle(*this, p1, p2, p3);
}
/**
* Check the collision between two lines defined by two points each, returns collision point by reference
*/
bool
CheckCollisionLines(::Vector2 endPos1, ::Vector2 startPos2, ::Vector2 endPos2, ::Vector2* collisionPoint) const {
return ::CheckCollisionLines(*this, endPos1, startPos2, endPos2, collisionPoint);
}
/**
* Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
*/
[[nodiscard]] bool CheckCollisionPointLine(::Vector2 p1, ::Vector2 p2, int threshold = 1) const {
return ::CheckCollisionPointLine(*this, p1, p2, threshold);
}
protected:
void set(const ::Vector2& vec) {
x = vec.x;
y = vec.y;
}
};
} // namespace raylib
using RVector2 = raylib::Vector2;
#endif // RAYLIB_CPP_INCLUDE_VECTOR2_HPP_