Meteor2D  dev internal
Meteor is a lightweight 2D game engine.
Loading...
Searching...
No Matches
vector2d.h
1#pragma once
2#include <meteorutils/str_extensions.h>
3#include <string>
4#include <meteorutils/logging.h>
5
6
7namespace meteor {
11 struct SVector2 {
12 public:
13 float x = 0;
14 float y = 0;
15
16 /************* VECTOR ARITHM*******/
17
18
19 void operator+=(SVector2 const& other) {
20 x += other.x;
21 y += other.y;
22 }
23
24 void operator-=(SVector2 const& other) {
25 x += other.x;
26 y += other.y;
27 }
28
29 SVector2 operator+(SVector2 const& other) {
30 SVector2 nVector;
31 nVector.x = x + other.x;
32 nVector.y = y + other.y;
33 return nVector;
34 }
35
36 SVector2 operator-(SVector2 const& other) {
37 SVector2 nVector;
38 nVector.x = x - other.x;
39 nVector.y = y - other.y;
40 return nVector;
41 }
42
43 /************ SCALAR******************/
44 SVector2 operator*(int const& scalar) {
45 SVector2 nVector;
46 nVector.x = x * scalar;
47 nVector.y = y * scalar;
48 return nVector;
49 }
50
51 SVector2 operator/(int const& scalar) {
52 SVector2 nVector;
53 nVector.x = x / scalar;
54 nVector.y = y / scalar;
55 return nVector;
56 }
57
58 SVector2 operator*(float const& scalar) {
59 SVector2 nVector;
60 nVector.x = x * scalar;
61 nVector.y = y * scalar;
62 return nVector;
63 }
64
65 SVector2 operator/(float const& scalar) {
66 SVector2 nVector;
67 nVector.x = x / scalar;
68 nVector.y = y / scalar;
69 return nVector;
70 }
71
72
73 std::string toString() {
74 return "(" + std::to_string(x) + "," + std::to_string(y) + ")";
75 }
76
77 /************ UTILS ******************/
78
85 static SVector2 make(float x, float y) {
86 return SVector2(x,y);
87 }
88
92 static SVector2 one() {
93 return SVector2(1,1);
94 }
95
99 static SVector2 zero() {
100 return SVector2(1, 1);
101 }
102
110 return SVector2(v1.x * v2.x, v1.y * v2.y);
111 }
112
119 static float dist(SVector2 v1, SVector2 v2) {
120 return sqrt(pow((v1.x - v2.x), 2) + pow((v1.y - v2.y), 2));
121 }
122
130 static bool parse(std::string str, SVector2& out) {
131 if (str[0] != '(') {
132 mError("vector parse failed, reason: {}", "expected ( at the begining.");
133 return false;
134 }
135 if (str[str.length() - 1] != ')') {
136 mError("vector parse failed, reason: {}", "expected ) at the end.");
137 return false;
138 }
139
140 str = string_utils::replace<std::string>(str,"(","");
141 str = string_utils::replace<std::string>(str, ")", "");
142
143 if (string_utils::numberOfOccurence<std::string>(str,",") != 1) {
144 mError("vector parse failed, reason: {}", "too many or too few components");
145 return false;
146 }
147 auto split = string_utils::split<std::string, std::string>(str, ",");
148 if (!(string_utils::isInt(split[0]) || string_utils::isFloat(split[0]))) {
149 mError("vector parse failed, reason: {} : x component is not a number", split[0]);
150 return false;
151 }
152 if (!(string_utils::isInt(split[1]) || string_utils::isFloat(split[1]))) {
153 mError("vector parse failed, reason: {} : y component is not a number", split[1]);
154 return false;
155 }
156
157 out.x = std::stof(split[0]);
158 out.y = std::stof(split[1]);
159 return true;
160 }
161 };
162}
A structure representing a 2D Vector.
Definition vector2d.h:11
static SVector2 make(float x, float y)
returns a vector2 using x and y. (similar to Vector(x,y) constructor)
Definition vector2d.h:85
static float dist(SVector2 v1, SVector2 v2)
Computes the euclidian distance.
Definition vector2d.h:119
static SVector2 one()
Definition vector2d.h:92
static SVector2 zero()
Definition vector2d.h:99
static SVector2 scale(SVector2 v1, SVector2 v2)
Multiplies each component of v1 with their respective parts in v2.
Definition vector2d.h:109
static bool parse(std::string str, SVector2 &out)
Parses a string to a vector, serialized vector to be in the form '(x,y)'.
Definition vector2d.h:130