ESPHome  2024.12.2
rect.cpp
Go to the documentation of this file.
1 #include "rect.h"
2 
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace display {
7 
8 static const char *const TAG = "display";
9 
10 void Rect::expand(int16_t horizontal, int16_t vertical) {
11  if (this->is_set() && (this->w >= (-2 * horizontal)) && (this->h >= (-2 * vertical))) {
12  this->x = this->x - horizontal;
13  this->y = this->y - vertical;
14  this->w = this->w + (2 * horizontal);
15  this->h = this->h + (2 * vertical);
16  }
17 }
18 
19 void Rect::extend(Rect rect) {
20  if (!this->is_set()) {
21  this->x = rect.x;
22  this->y = rect.y;
23  this->w = rect.w;
24  this->h = rect.h;
25  } else {
26  if (this->x > rect.x) {
27  this->w = this->w + (this->x - rect.x);
28  this->x = rect.x;
29  }
30  if (this->y > rect.y) {
31  this->h = this->h + (this->y - rect.y);
32  this->y = rect.y;
33  }
34  if (this->x2() < rect.x2()) {
35  this->w = rect.x2() - this->x;
36  }
37  if (this->y2() < rect.y2()) {
38  this->h = rect.y2() - this->y;
39  }
40  }
41 }
42 void Rect::shrink(Rect rect) {
43  if (!this->inside(rect)) {
44  (*this) = Rect();
45  } else {
46  if (this->x2() > rect.x2()) {
47  this->w = rect.x2() - this->x;
48  }
49  if (this->x < rect.x) {
50  this->w = this->w + (this->x - rect.x);
51  this->x = rect.x;
52  }
53  if (this->y2() > rect.y2()) {
54  this->h = rect.y2() - this->y;
55  }
56  if (this->y < rect.y) {
57  this->h = this->h + (this->y - rect.y);
58  this->y = rect.y;
59  }
60  }
61 }
62 
63 bool Rect::equal(Rect rect) const {
64  return (rect.x == this->x) && (rect.w == this->w) && (rect.y == this->y) && (rect.h == this->h);
65 }
66 
67 bool Rect::inside(int16_t test_x, int16_t test_y, bool absolute) const { // NOLINT
68  if (!this->is_set()) {
69  return true;
70  }
71  if (absolute) {
72  return ((test_x >= this->x) && (test_x <= this->x2()) && (test_y >= this->y) && (test_y <= this->y2()));
73  } else {
74  return ((test_x >= 0) && (test_x <= this->w) && (test_y >= 0) && (test_y <= this->h));
75  }
76 }
77 
78 bool Rect::inside(Rect rect, bool absolute) const {
79  if (!this->is_set() || !rect.is_set()) {
80  return true;
81  }
82  if (absolute) {
83  return ((rect.x <= this->x2()) && (rect.x2() >= this->x) && (rect.y <= this->y2()) && (rect.y2() >= this->y));
84  } else {
85  return ((rect.x <= this->w) && (rect.w >= 0) && (rect.y <= this->h) && (rect.h >= 0));
86  }
87 }
88 
89 void Rect::info(const std::string &prefix) {
90  if (this->is_set()) {
91  ESP_LOGI(TAG, "%s [%3d,%3d,%3d,%3d] (%3d,%3d)", prefix.c_str(), this->x, this->y, this->w, this->h, this->x2(),
92  this->y2());
93  } else
94  ESP_LOGI(TAG, "%s ** IS NOT SET **", prefix.c_str());
95 }
96 
97 } // namespace display
98 } // namespace esphome
bool is_set() const ESPHOME_ALWAYS_INLINE
Y coordinate of corner.
Definition: rect.h:22
bool inside(Rect rect, bool absolute=true) const
Definition: rect.cpp:78
int16_t x
X coordinate of corner.
Definition: rect.h:12
int16_t h
Height of region.
Definition: rect.h:15
int16_t y
Y coordinate of corner.
Definition: rect.h:13
void extend(Rect rect)
Definition: rect.cpp:19
bool equal(Rect rect) const
Definition: rect.cpp:63
void info(const std::string &prefix="rect info:")
Definition: rect.cpp:89
int16_t x2() const
Definition: rect.h:19
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void expand(int16_t horizontal, int16_t vertical)
Definition: rect.cpp:10
void shrink(Rect rect)
Definition: rect.cpp:42
int16_t y2() const
X coordinate of corner.
Definition: rect.h:20
int16_t w
Width of region.
Definition: rect.h:14