From 654c94e1d312970413e376214b11bd554040d30f Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 21 Apr 2023 22:52:28 +0200 Subject: Replaced Mouse dragging start+pos combo with a Rect --- engine/src/Point.cpp | 7 +++++ engine/src/Rect.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 engine/src/Rect.cpp (limited to 'engine/src') diff --git a/engine/src/Point.cpp b/engine/src/Point.cpp index e01de57..8dd50bd 100644 --- a/engine/src/Point.cpp +++ b/engine/src/Point.cpp @@ -96,5 +96,12 @@ Point::operator!=(const Point& other) const } +Point +abs(const Point& point) +{ + return {std::abs(point.x), std::abs(point.y)}; +} + + } // namespace engine } // namespace kurator diff --git a/engine/src/Rect.cpp b/engine/src/Rect.cpp new file mode 100644 index 0000000..293e099 --- /dev/null +++ b/engine/src/Rect.cpp @@ -0,0 +1,74 @@ +#include + +#include +#include + +#include + +#include + + +using std::abs; + + +namespace kurator +{ +namespace engine +{ + + +Rect::Rect() : + position {0, 0}, + size {0, 0} +{ +} + + +Rect::Rect(const Point& first, const Point& second) : + position {std::min(first.x, second.x), std::min(first.y, second.y)}, + size {abs(second - first)} +{ +} + + +Point Rect::topleft() const +{ + return position; +} + + +Point Rect::bottomright() const +{ + return position + size; +} + + +Point Rect::center() const +{ + return position + size.scale(0.5); +} + + +bool Rect::contains(const Point& point) +{ + const auto br = bottomright(); + return point.x >= position.x && point.x <= br.x && point.y >= position.y && point.y <= br.y; +} + + +bool Rect::contains(const Point& circle, double radius) +{ + return CheckCollisionCircleRec( + {static_cast(circle.x), static_cast(circle.y)}, + radius, + { + static_cast(position.x), + static_cast(position.y), + static_cast(size.x), + static_cast(size.y) + }); +} + + +} // namespace engine +} // namespace kurator -- cgit v1.1