Bladeren bron

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 :^)
Andreas Kling 5 jaren geleden
bovenliggende
commit
6685be36a0
2 gewijzigde bestanden met toevoegingen van 7 en 16 verwijderingen
  1. 1 15
      DevTools/HackStudio/CursorTool.cpp
  2. 6 1
      Libraries/LibDraw/Rect.h

+ 1 - 15
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&)

+ 6 - 1
Libraries/LibDraw/Rect.h

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