[kandinsky] Add method KDRect::differencedWith(const KDRect & other)

Change-Id: Ib6c61bbd3bffc90bc3e704a7133eae0731500864
This commit is contained in:
Léa Saviot
2018-04-25 13:43:09 +02:00
parent 9f09ee8339
commit 34d6028882
2 changed files with 42 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ public:
bool intersects(const KDRect & other) const;
KDRect intersectedWith(const KDRect & other) const;
KDRect unionedWith(const KDRect & other) const; // Returns the smallest rectangle containing r1 and r2
KDRect differencedWith(const KDRect & other) const; // Returns the smallest rectangle containing r1\r2
bool contains(KDPoint p) const;
bool isAbove(KDPoint p) const;
bool isUnder(KDPoint p) const;

View File

@@ -101,6 +101,47 @@ KDRect KDRect::unionedWith(const KDRect & other) const {
);
}
KDRect KDRect::differencedWith(const KDRect & other) const {
if (this->isEmpty() || other.isEmpty()) {
return *this;
}
KDRect intersection = intersectedWith(other);
if (intersection.isEmpty()) {
return *this;
}
if (intersection == *this) {
return KDRectZero;
}
KDCoordinate resultLeft = left();
KDCoordinate resultTop = top();
KDCoordinate resultRight = right();
KDCoordinate resultBottom = bottom();
if (intersection.height() == height()) {
if (intersection.left() == left()) {
resultLeft = intersection.right() + 1;
} else if (intersection.right() == right()) {
resultRight = intersection.left() - 1;
}
} else if (intersection.width() == width()) {
if (intersection.top() == top()) {
resultTop = intersection.bottom() + 1;
} else if (intersection.bottom() == bottom()) {
resultBottom = intersection.top() - 1;
}
}
return KDRect(
resultLeft,
resultTop,
resultRight-resultLeft+1,
resultBottom-resultTop+1
);
}
bool KDRect::contains(KDPoint p) const {
return (p.x() >= x() && p.x() <= right() && p.y() >= y() && p.y() <= bottom());
}