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