From 61854d118e350cd0d6bfaf8a02880d54409e45da Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Thu, 21 Apr 2016 17:14:59 +0200 Subject: [PATCH] Kandinsky: Add KDRectIntersect and update KDRectIntersection One gives a boolean, the other the actual KDRect Change-Id: I5a2e6d1110f0ad111ac2f3a46dcd072e72ea786d --- escher/src/view.cpp | 4 +-- kandinsky/include/kandinsky/rect.h | 5 ++- kandinsky/src/rect.c | 58 +++++++++++++++--------------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/escher/src/view.cpp b/escher/src/view.cpp index 11e0e4b5a..a4ac70062 100644 --- a/escher/src/view.cpp +++ b/escher/src/view.cpp @@ -34,8 +34,8 @@ void View::redraw(KDRect rect) { if (subview == nullptr) { continue; } - KDRect intersection = KDRectIntersect(rect, subview->m_frame); - if (intersection.width > 0 || intersection.height > 0) { + if (KDRectIntersect(rect, subview->m_frame)) { + KDRect intersection = KDRectIntersection(rect, subview->m_frame); // Let's express intersection in subview's coordinates intersection.x -= subview->m_frame.x; intersection.y -= subview->m_frame.y; diff --git a/kandinsky/include/kandinsky/rect.h b/kandinsky/include/kandinsky/rect.h index e05389256..fe820db85 100644 --- a/kandinsky/include/kandinsky/rect.h +++ b/kandinsky/include/kandinsky/rect.h @@ -1,6 +1,7 @@ #ifndef KANDINSKY_RECT_H #define KANDINSKY_RECT_H +#include #include #include @@ -22,7 +23,9 @@ typedef struct { } KDRect; extern KDRect KDRectZero; -KDRect KDRectIntersect(KDRect r1, KDRect r2); + +bool KDRectIntersect(KDRect r1, KDRect r2); +KDRect KDRectIntersection(KDRect r1, KDRect r2); void KDFillRect(KDRect rect, KDColor color); void KDDrawRect(KDRect rect, KDColor color); diff --git a/kandinsky/src/rect.c b/kandinsky/src/rect.c index cb59e0311..1dc626994 100644 --- a/kandinsky/src/rect.c +++ b/kandinsky/src/rect.c @@ -4,38 +4,40 @@ KDRect KDRectZero = {.x = 0, .y = 0, .width = 0, .height = 0}; -KDRect KDRectIntersect(KDRect r1, KDRect r2) { - KDRect intersection; +static inline KDCoordinate left(KDRect r) { return r.x; } +static inline KDCoordinate right(KDRect r) { return r.x+r.width; } +static inline KDCoordinate top(KDRect r) { return r.y; } +static inline KDCoordinate bottom(KDRect r) { return r.y+r.height; } +static inline KDCoordinate min(KDCoordinate x, KDCoordinate y) { return (xy ? x : y); } - // Let's start by computing the overlap on the X axis - if (r1.x < r2.x) { - intersection.x = r2.x; - intersection.width = r1.x+r1.width-r2.x; - } else { - intersection.x = r1.x; - intersection.width = r2.x+r2.width-r1.x; - } +bool KDRectIntersect(KDRect r1, KDRect r2) { + return !(right(r2) < left(r1) + || + left(r2) > right(r1) + || + top(r2) > bottom(r1) + || + bottom(r2) < top(r1) + ); +} - if (intersection.width < 0) { - // There's no overlap on the X axis, let's bail out - return KDRectZero; - } +KDRect KDRectIntersection(KDRect r1, KDRect r2) { + if (!KDRectIntersect(r1, r2)) { + return KDRectZero; + } - // Let's then compute the overlap on the Y axis - if (r1.y < r2.y) { - intersection.y = r2.y; - intersection.height = r1.y+r1.height-r2.y; - } else { - intersection.y = r1.y; - intersection.height = r2.y+r2.height-r1.y; - } + KDCoordinate intersectionLeft = max(left(r1), left(r2)); + KDCoordinate intersectionRight = min(right(r1), right(r2)); + KDCoordinate intersectionTop = max(top(r1), top(r2)); + KDCoordinate intersectionBottom = min(bottom(r1), bottom(r2)); - if (intersection.height < 0) { - // There's no overlap on the Y axis, let's bail out - return KDRectZero; - } - - return intersection; + KDRect intersection; + intersection.x = intersectionLeft; + intersection.width = intersectionRight-intersectionLeft; + intersection.y = intersectionTop; + intersection.height = intersectionBottom-intersectionTop; + return intersection; } void KDFillRect(KDRect rect, KDColor color) {