SSD1306 OLED display driver 1.8.2 This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays

rect.h
Go to the documentation of this file.
1 /*
2 MIT License
3
4 Copyright (c) 2018, Alexey Dynda
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
28 #ifndef _NANO_RECT_H_
29 #define _NANO_RECT_H_
30
31 #include "point.h"
32 #include "ssd1306_hal/io.h"
33
42 typedef struct _NanoRect
43 {
45 NanoPoint p1;
46
48 NanoPoint p2;
49
51 lcdint_t width() const
52 {
53 return p2.x - p1.x + 1;
54 }
55
57 const NanoPoint size() const
58 {
59 return {width(), height()};
60 }
61
63 lcdint_t height() const
64 {
65 return p2.y - p1.y + 1;
66 }
67
73 void move(lcdint_t dx, lcdint_t dy)
74 {
75 p1.x += dx; p2.x += dx;
76 p1.y += dy; p2.y += dy;
77 }
78
83 void addH(lcdint_t dx)
84 {
85 p1.x += dx; p2.x += dx;
86 }
87
92 void addV(lcdint_t dy)
93 {
94 p1.y += dy;
95 p2.y += dy;
96 }
97
105 void setRect(lcdint_t l, lcdint_t t, lcdint_t r, lcdint_t b)
106 {
107 p1.x = l; p1.y = t;
108 p2.x = r; p2.y = b;
109 }
110
116 void crop(const _NanoRect& rect)
117 {
118 if (p1.x < rect.p1.x) p1.x = rect.p1.x;
119 if (p1.y < rect.p1.y) p1.y = rect.p1.y;
120 if (p2.x > rect.p2.x) p2.x = rect.p2.x;
121 if (p2.y > rect.p2.y) p2.y = rect.p2.y;
122 }
123
128 bool collisionX(lcdint_t x) const
129 {
130 return (x >= p1.x) && (x <= p2.x);
131 }
132
137 bool collisionY(lcdint_t y) const { return (y >= p1.y) && (y <= p2.y); };
138
143 bool collision(const NanoPoint &p) const
144 {
145 return collisionX(p.x) && collisionY(p.y);
146 }
147
153 bool contains(const NanoPoint &p) const
154 {
155 return collision(p);
156 }
157
163 bool contains(const _NanoRect &r) const
164 {
165 return contains(r.p1) && contains(r.p2);
166 }
167
173 bool containsPartOf(const _NanoRect &r) const
174 {
175 return contains(r.p1) || contains(r.p2);
176 }
177
182 bool above(const NanoPoint &p) const { return (p.y < p1.y); };
183
188 bool below(const NanoPoint &p) const { return (p.y > p2.y); };
189
194 _NanoRect operator-(const _NanoPoint &p)
195 {
196 return { {static_cast<lcdint_t>(p1.x - p.x), static_cast<lcdint_t>(p1.y - p.y) },
197 {static_cast<lcdint_t>(p2.x - p.x), static_cast<lcdint_t>(p2.y - p.y) } };
198 }
199
204 _NanoRect operator+(const _NanoPoint &p)
205 {
206 return { {static_cast<lcdint_t>(p1.x + p.x), static_cast<lcdint_t>(p1.y + p.y) },
207 {static_cast<lcdint_t>(p2.x + p.x), static_cast<lcdint_t>(p2.y + p.y) } };
208 }
209
214 _NanoRect& operator+=(const _NanoPoint &p)
215 {
216 p1.x += p.x;
217 p1.y += p.y;
218 p2.x += p.x;
219 p2.y += p.y;
220 return *this;
221 }
222
227 _NanoRect operator>>(const uint8_t bits) const
228 {
229 return { p1 >> bits, p2 >> bits };
230 };
231
236 _NanoRect operator<<(const uint8_t bits) const
237 {
238 return { p1 << bits, p2 << bits };
239 };
240
241
242 } NanoRect;
243
244 #ifndef DOXYGEN_SHOULD_SKIP_THIS
245 inline NanoRect operator+(const NanoRect& rect, const NanoPoint& p)
246 {
247 return { { (lcdint_t)(rect.p1.x + p.x), (lcdint_t)(rect.p1.y + p.y) },
248 { (lcdint_t)(rect.p2.x + p.x), (lcdint_t)(rect.p2.y + p.y) } };
249 }
250 #endif
251
256 #endif
257
_NanoRect::crop
void crop(const _NanoRect &rect)
Definition: rect.h:116
_NanoRect::operator-
_NanoRect operator-(const _NanoPoint &p)
Definition: rect.h:194
_NanoRect::operator+
_NanoRect operator+(const _NanoPoint &p)
Definition: rect.h:204
_NanoPoint
Definition: point.h:40
_NanoRect::collisionY
bool collisionY(lcdint_t y) const
Definition: rect.h:137
_NanoRect
Definition: rect.h:42
_NanoRect::height
lcdint_t height() const
Definition: rect.h:63
point.h
lcdint_t
int lcdint_t
Definition: io.h:63
_NanoRect::addV
void addV(lcdint_t dy)
Definition: rect.h:92
_NanoRect::p2
NanoPoint p2
Definition: rect.h:48
io.h
_NanoRect::operator<<
_NanoRect operator<<(const uint8_t bits) const
Definition: rect.h:236
_NanoRect::above
bool above(const NanoPoint &p) const
Definition: rect.h:182
_NanoPoint::y
lcdint_t y
Definition: point.h:45
_NanoRect::collision
bool collision(const NanoPoint &p) const
Definition: rect.h:143
NanoRect
struct _NanoRect NanoRect
_NanoRect::operator>>
_NanoRect operator>>(const uint8_t bits) const
Definition: rect.h:227
_NanoRect::contains
bool contains(const NanoPoint &p) const
Definition: rect.h:153
_NanoRect::setRect
void setRect(lcdint_t l, lcdint_t t, lcdint_t r, lcdint_t b)
Definition: rect.h:105
_NanoRect::operator+=
_NanoRect & operator+=(const _NanoPoint &p)
Definition: rect.h:214
_NanoRect::addH
void addH(lcdint_t dx)
Definition: rect.h:83
_NanoRect::below
bool below(const NanoPoint &p) const
Definition: rect.h:188
_NanoRect::collisionX
bool collisionX(lcdint_t x) const
Definition: rect.h:128
_NanoRect::move
void move(lcdint_t dx, lcdint_t dy)
Definition: rect.h:73
_NanoRect::width
lcdint_t width() const
Definition: rect.h:51
_NanoRect::size
const NanoPoint size() const
Definition: rect.h:57
_NanoRect::contains
bool contains(const _NanoRect &r) const
Definition: rect.h:163
_NanoRect::containsPartOf
bool containsPartOf(const _NanoRect &r) const
Definition: rect.h:173
_NanoRect::p1
NanoPoint p1
Definition: rect.h:45
_NanoPoint::x
lcdint_t x
Definition: point.h:43