From 6685be36a0c12dfd7e37fcba37afbc4facb6a49d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 17 Nov 2019 16:36:43 +0100 Subject: [PATCH] LibDraw: Add Rect::from_two_points(Point, Point) This returns the rectangle between two given Points. Thanks Owlinator for suggesting this much easier way of doing it :^) --- DevTools/HackStudio/CursorTool.cpp | 16 +--------------- Libraries/LibDraw/Rect.h | 7 ++++++- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/DevTools/HackStudio/CursorTool.cpp b/DevTools/HackStudio/CursorTool.cpp index 637e601f33..b8450073f2 100644 --- a/DevTools/HackStudio/CursorTool.cpp +++ b/DevTools/HackStudio/CursorTool.cpp @@ -142,25 +142,11 @@ void CursorTool::set_rubber_band_position(const Point& position) m_editor.form_widget().update(); } -static Rect rect_from_two_points(const Point& a, const Point& b) -{ - if (a.x() <= b.x()) { - if (a.y() <= b.y()) - return { a, { b.x() - a.x(), b.y() - a.y() } }; - int height = a.y() - b.y(); - return { a.x(), a.y() - height, b.x() - a.x(), height }; - } - if (a.y() >= b.y()) - return { b, { a.x() - b.x(), a.y() - b.y() } }; - int height = b.y() - a.y(); - return { b.x(), b.y() - height, a.x() - b.x(), height }; -} - Rect CursorTool::rubber_band_rect() const { if (!m_rubber_banding) return {}; - return rect_from_two_points(m_rubber_band_origin, m_rubber_band_position); + return Rect::from_two_points(m_rubber_band_origin, m_rubber_band_position); } void CursorTool::on_second_paint(GPainter& painter, GPaintEvent&) diff --git a/Libraries/LibDraw/Rect.h b/Libraries/LibDraw/Rect.h index 25cf87d7a9..1b023615f4 100644 --- a/Libraries/LibDraw/Rect.h +++ b/Libraries/LibDraw/Rect.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include #include #include @@ -237,6 +237,11 @@ public: void intersect(const Rect&); + static Rect from_two_points(const Point& a, const Point& b) + { + return { min(a.x(), b.x()), min(a.y(), b.y()), abs(a.x() - b.x()), abs(a.y() - b.y()) }; + } + static Rect intersection(const Rect& a, const Rect& b) { Rect r(a);