From 9b453277059fd015703873172d0dc87b4a29cb55 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 3 Feb 2023 22:00:28 +0100 Subject: Created engine module right now containing only Point This might be a bit too generic of a name, but the intent is to get the main shared abstracts for gameplay loop and/or simulation outside of the game executable implementation to redirect dependencies. --- engine/src/Point.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 engine/src/Point.cpp (limited to 'engine/src') diff --git a/engine/src/Point.cpp b/engine/src/Point.cpp new file mode 100644 index 0000000..b5ce4eb --- /dev/null +++ b/engine/src/Point.cpp @@ -0,0 +1,72 @@ +#include + +#include + + +namespace kurator +{ +namespace engine +{ + + +double +Point::magnitude() const +{ + return std::sqrt(std::pow(x, 2) + std::pow(y, 2)); +} + + +double +Point::distance(const Point& other) const +{ + return std::sqrt(std::pow(other.x - x, 2) + std::pow(other.y - y, 2)); +} + + +double +Point::angle() const +{ + return std::atan2(y, x); // (+x, _) is 0 +} + + +Point +Point::rotate(const double angle) const +{ + return { + x * std::cos(angle) - y * std::sin(angle), + x * std::sin(angle) + y * std::cos(angle), + }; +} + + +Point +Point::scale(const double _scale) const +{ + return {x * _scale, y * _scale}; +} + + +Point +Point::normalized() const +{ + return scale(1.0 / magnitude()); +} + + +Point +Point::operator-(const Point& other) const +{ + return {x - other.x, y - other.y}; +} + + +Point +Point::operator+(const Point& other) const +{ + return {x + other.x, y + other.y}; +} + + +} // namespace engine +} // namespace kurator -- cgit v1.1