From 52090853dafb75dd99969a0cecdd291c109210f8 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Wed, 16 Mar 2022 22:21:12 +0100 Subject: [PATCH] added vector2d --- src/helpers/Vector2D.cpp | 19 +++++++++++++++++++ src/helpers/Vector2D.hpp | 29 +++++++++++++++++++++++++++++ src/includes.hpp | 4 +++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/helpers/Vector2D.cpp create mode 100644 src/helpers/Vector2D.hpp diff --git a/src/helpers/Vector2D.cpp b/src/helpers/Vector2D.cpp new file mode 100644 index 00000000..33b12e7c --- /dev/null +++ b/src/helpers/Vector2D.cpp @@ -0,0 +1,19 @@ +#include "Vector2D.hpp" + +Vector2D::Vector2D(double xx, double yy) { + x = xx; + y = yy; +} + +Vector2D::Vector2D() { x = 0; y = 0; } +Vector2D::~Vector2D() {} + +double Vector2D::normalize() { + // get max abs + const auto max = abs(x) > abs(y) ? abs(x) : abs(y); + + x /= max; + y /= max; + + return max; +} \ No newline at end of file diff --git a/src/helpers/Vector2D.hpp b/src/helpers/Vector2D.hpp new file mode 100644 index 00000000..9bc427e8 --- /dev/null +++ b/src/helpers/Vector2D.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +class Vector2D { + public: + Vector2D(double, double); + Vector2D(); + ~Vector2D(); + + double x = 0; + double y = 0; + + // returns the scale + double normalize(); + + Vector2D operator+(Vector2D a) { + return Vector2D(this->x + a.x, this->y + a.y); + } + Vector2D operator-(Vector2D a) { + return Vector2D(this->x - a.x, this->y - a.y); + } + Vector2D operator*(float a) { + return Vector2D(this->x * a, this->y * a); + } + Vector2D operator/(float a) { + return Vector2D(this->x / a, this->y / a); + } +}; \ No newline at end of file diff --git a/src/includes.hpp b/src/includes.hpp index d2f49181..352286d4 100644 --- a/src/includes.hpp +++ b/src/includes.hpp @@ -65,4 +65,6 @@ extern "C" { #undef namespace #undef static -#endif \ No newline at end of file +#endif + +#include "helpers/Vector2D.hpp"