fixing shrink and extend functions of the displaybuffer's Rect class (#4565)

* fixing rectangle's `shrink` and `extend`

* fixed the rect::shrink and rect::inside methods
and added rect:equal() method

* fixed internal clang issue again. When would is
this going to be fixed :(

* fixed internal clang issue again. When would is
this going to be fixed :(

* remove trailing space
This commit is contained in:
NP v/d Spek 2023-03-15 20:45:50 +01:00 committed by Jesse Hills
parent 9922c1503a
commit 5ffdc66864
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
2 changed files with 24 additions and 14 deletions

View file

@ -32,9 +32,11 @@ void Rect::extend(Rect rect) {
this->h = rect.h; this->h = rect.h;
} else { } else {
if (this->x > rect.x) { if (this->x > rect.x) {
this->w = this->w + (this->x - rect.x);
this->x = rect.x; this->x = rect.x;
} }
if (this->y > rect.y) { if (this->y > rect.y) {
this->h = this->h + (this->y - rect.y);
this->y = rect.y; this->y = rect.y;
} }
if (this->x2() < rect.x2()) { if (this->x2() < rect.x2()) {
@ -49,29 +51,35 @@ void Rect::shrink(Rect rect) {
if (!this->inside(rect)) { if (!this->inside(rect)) {
(*this) = Rect(); (*this) = Rect();
} else { } else {
if (this->x < rect.x) {
this->x = rect.x;
}
if (this->y < rect.y) {
this->y = rect.y;
}
if (this->x2() > rect.x2()) { if (this->x2() > rect.x2()) {
this->w = rect.x2() - this->x; this->w = rect.x2() - this->x;
} }
if (this->x < rect.x) {
this->w = this->w + (this->x - rect.x);
this->x = rect.x;
}
if (this->y2() > rect.y2()) { if (this->y2() > rect.y2()) {
this->h = rect.y2() - this->y; this->h = rect.y2() - this->y;
} }
if (this->y < rect.y) {
this->h = this->h + (this->y - rect.y);
this->y = rect.y;
}
} }
} }
bool Rect::inside(int16_t x, int16_t y, bool absolute) { // NOLINT bool Rect::equal(Rect rect) {
return (rect.x == this->x) && (rect.w == this->w) && (rect.y == this->y) && (rect.h == this->h);
}
bool Rect::inside(int16_t test_x, int16_t test_y, bool absolute) { // NOLINT
if (!this->is_set()) { if (!this->is_set()) {
return true; return true;
} }
if (absolute) { if (absolute) {
return ((x >= 0) && (x <= this->w) && (y >= 0) && (y <= this->h)); return ((test_x >= this->x) && (test_x <= this->x2()) && (test_y >= this->y) && (test_y <= this->y2()));
} else { } else {
return ((x >= this->x) && (x <= this->x2()) && (y >= this->y) && (y <= this->y2())); return ((test_x >= 0) && (test_x <= this->w) && (test_y >= 0) && (test_y <= this->h));
} }
} }
@ -80,15 +88,16 @@ bool Rect::inside(Rect rect, bool absolute) {
return true; return true;
} }
if (absolute) { if (absolute) {
return ((rect.x <= this->w) && (rect.w >= 0) && (rect.y <= this->h) && (rect.h >= 0));
} else {
return ((rect.x <= this->x2()) && (rect.x2() >= this->x) && (rect.y <= this->y2()) && (rect.y2() >= this->y)); return ((rect.x <= this->x2()) && (rect.x2() >= this->x) && (rect.y <= this->y2()) && (rect.y2() >= this->y));
} else {
return ((rect.x <= this->w) && (rect.w >= 0) && (rect.y <= this->h) && (rect.h >= 0));
} }
} }
void Rect::info(const std::string &prefix) { void Rect::info(const std::string &prefix) {
if (this->is_set()) { if (this->is_set()) {
ESP_LOGI(TAG, "%s [%3d,%3d,%3d,%3d]", prefix.c_str(), this->x, this->y, this->w, this->h); ESP_LOGI(TAG, "%s [%3d,%3d,%3d,%3d] (%3d,%3d)", prefix.c_str(), this->x, this->y, this->w, this->h, this->x2(),
this->y2());
} else } else
ESP_LOGI(TAG, "%s ** IS NOT SET **", prefix.c_str()); ESP_LOGI(TAG, "%s ** IS NOT SET **", prefix.c_str());
} }

View file

@ -120,8 +120,9 @@ class Rect {
void extend(Rect rect); void extend(Rect rect);
void shrink(Rect rect); void shrink(Rect rect);
bool inside(Rect rect, bool absolute = false); bool inside(Rect rect, bool absolute = true);
bool inside(int16_t x, int16_t y, bool absolute = false); bool inside(int16_t test_x, int16_t test_y, bool absolute = true);
bool equal(Rect rect);
void info(const std::string &prefix = "rect info:"); void info(const std::string &prefix = "rect info:");
}; };