2#include <meteorutils/str_extensions.h>
4#include <meteorutils/logging.h>
19 void operator+=(
SVector2 const& other) {
24 void operator-=(
SVector2 const& other) {
31 nVector.x = x + other.x;
32 nVector.y = y + other.y;
38 nVector.x = x - other.x;
39 nVector.y = y - other.y;
44 SVector2 operator*(
int const& scalar) {
46 nVector.x = x * scalar;
47 nVector.y = y * scalar;
51 SVector2 operator/(
int const& scalar) {
53 nVector.x = x / scalar;
54 nVector.y = y / scalar;
58 SVector2 operator*(
float const& scalar) {
60 nVector.x = x * scalar;
61 nVector.y = y * scalar;
65 SVector2 operator/(
float const& scalar) {
67 nVector.x = x / scalar;
68 nVector.y = y / scalar;
73 std::string toString() {
74 return "(" + std::to_string(x) +
"," + std::to_string(y) +
")";
110 return SVector2(v1.x * v2.x, v1.y * v2.y);
120 return sqrt(pow((v1.x - v2.x), 2) + pow((v1.y - v2.y), 2));
132 mError(
"vector parse failed, reason: {}",
"expected ( at the begining.");
135 if (str[str.length() - 1] !=
')') {
136 mError(
"vector parse failed, reason: {}",
"expected ) at the end.");
140 str = string_utils::replace<std::string>(str,
"(",
"");
141 str = string_utils::replace<std::string>(str,
")",
"");
143 if (string_utils::numberOfOccurence<std::string>(str,
",") != 1) {
144 mError(
"vector parse failed, reason: {}",
"too many or too few components");
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]);
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]);
157 out.x = std::stof(split[0]);
158 out.y = std::stof(split[1]);
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