ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
rect.cpp
Go to the documentation of this file.
1#include "rect.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace display {
7
8static const char *const TAG = "display";
9
10void 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
19void 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}
42void 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
63bool 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
67bool 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 }
74 return test_x >= 0 && test_x < this->w && test_y >= 0 && test_y < this->h;
75}
76
77bool Rect::inside(Rect rect) const {
78 if (!this->is_set() || !rect.is_set()) {
79 return true;
80 }
81 return this->x2() >= rect.x && this->x <= rect.x2() && this->y2() >= rect.y && this->y <= rect.y2();
82}
83
84void Rect::info(const std::string &prefix) {
85 if (this->is_set()) {
86 ESP_LOGI(TAG, "%s [%3d,%3d,%3d,%3d] (%3d,%3d)", prefix.c_str(), this->x, this->y, this->w, this->h, this->x2(),
87 this->y2());
88 } else {
89 ESP_LOGI(TAG, "%s ** IS NOT SET **", prefix.c_str());
90 }
91}
92
93} // namespace display
94} // namespace esphome
int16_t x2() const
Definition rect.h:19
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 shrink(Rect rect)
Definition rect.cpp:42
void info(const std::string &prefix="rect info:")
Definition rect.cpp:84
void expand(int16_t horizontal, int16_t vertical)
Definition rect.cpp:10
bool inside(Rect rect) const
Definition rect.cpp:77
bool is_set() const ESPHOME_ALWAYS_INLINE
Y coordinate of corner.
Definition rect.h:22
void extend(Rect rect)
Definition rect.cpp:19
bool equal(Rect rect) const
Definition rect.cpp:63
int16_t y2() const
X coordinate of corner.
Definition rect.h:20
int16_t w
Width of region.
Definition rect.h:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7