From 29776f836677f37c5eeafb727c9a7fbfd52029f9 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 16 Feb 2017 00:26:01 +0000 Subject: [PATCH] iRect operators. git-svn-id: svn://qnap.local/TwoTerm/branches/fix-gno-scrolling-region@3161 5590a31f-7b70-45f8-8c82-aa3a8e5f4507 --- cpp/iGeometry.cpp | 19 +++++++++++++++++-- cpp/iGeometry.h | 25 +++++++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/cpp/iGeometry.cpp b/cpp/iGeometry.cpp index 99d3bbd..5c685f5 100644 --- a/cpp/iGeometry.cpp +++ b/cpp/iGeometry.cpp @@ -9,7 +9,7 @@ #include "iGeometry.h" - +#include bool iRect::contains(const iPoint aPoint) const { @@ -30,4 +30,19 @@ bool iRect::contains(const iRect aRect) const bool iRect::intersects(const iRect aRect) const { return aRect.contains(origin) || aRect.contains(origin.offset(size)); -} \ No newline at end of file +} + +iRect iRect::intersection(const iRect &rhs) const { + iPoint topLeft; + iPoint bottomRight; + topLeft.x = std::max(origin.x, rhs.origin.x); + topLeft.y = std::max(origin.y, rhs.origin.y); + + bottomRight.x = std::min(maxX(), rhs.maxX()); + bottomRight.y = std::min(maxY(), rhs.maxY()); + + if (bottomRight.x <= topLeft.x) return iRect(); + if (bottomRight.y <= topLeft.y) return iRect(); + + return iRect(topLeft, bottomRight); +} diff --git a/cpp/iGeometry.h b/cpp/iGeometry.h index 9bc89ec..7aac4a6 100644 --- a/cpp/iGeometry.h +++ b/cpp/iGeometry.h @@ -23,10 +23,10 @@ typedef struct iSize { iSize &operator=(const iSize &) = default; - bool operator==(const iSize &aSize) + bool operator==(const iSize &aSize) const { return width == aSize.width && height == aSize.height; } - bool operator!=(const iSize& aSize) + bool operator!=(const iSize& aSize) const { return !(*this == aSize); } } iSize; @@ -43,10 +43,10 @@ typedef struct iPoint { iPoint &operator=(const iPoint &) = default; - bool operator==(const iPoint &aPoint) + bool operator==(const iPoint &aPoint) const { return x == aPoint.x && y == aPoint.y; } - bool operator!=(const iPoint &aPoint) + bool operator!=(const iPoint &aPoint) const { return !(*this == aPoint); } iPoint offset(int dx, int dy) const @@ -80,6 +80,23 @@ typedef struct iRect { bool intersects(const iRect aRect) const; + iRect intersection(const iRect &rhs) const; + + bool operator==(const iRect &rhs) const { + return origin == rhs.origin && size == rhs.size; + } + bool operator!=(const iRect &rhs) const { + return !(*this == rhs); + } + + explicit operator bool() const { return size.height >= 0 && size.width >= 0; } + bool operator!() const { return size.height < 0 || size.width < 0; } + bool valid() const { return size.height >= 0 && size.width >= 0; } + + iPoint topLeft() const { return origin; } + iPoint bottomRight() const { return iPoint(maxX(), maxY()); } + + void setBottomLeft(iPoint &p); int height() const { return size.height; }