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 :^)
This commit is contained in:
Andreas Kling 2019-11-17 16:36:43 +01:00
parent 32ff660aa5
commit 6685be36a0
Notes: sideshowbarker 2024-07-19 11:10:53 +09:00
2 changed files with 7 additions and 16 deletions

View file

@ -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&)

View file

@ -1,7 +1,7 @@
#pragma once
#include <AK/String.h>
#include <AK/LogStream.h>
#include <AK/String.h>
#include <LibDraw/Orientation.h>
#include <LibDraw/Point.h>
#include <LibDraw/Size.h>
@ -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);