LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
rect.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2019, 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 "canvas_types.h"
33 
42 typedef struct _NanoRect
43 {
46 
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  const NanoPoint center() const
64  {
65  return {(p2.x + p1.x) / 2, (p2.y + p1.y) / 2};
66  }
67 
69  lcdint_t height() const
70  {
71  return p2.y - p1.y + 1;
72  }
73 
79  void move(lcdint_t dx, lcdint_t dy)
80  {
81  p1.x += dx;
82  p2.x += dx;
83  p1.y += dy;
84  p2.y += dy;
85  }
86 
91  void addH(lcdint_t dx)
92  {
93  p1.x += dx;
94  p2.x += dx;
95  }
96 
101  void addV(lcdint_t dy)
102  {
103  p1.y += dy;
104  p2.y += dy;
105  }
106 
115  {
116  p1.x = l;
117  p1.y = t;
118  p2.x = r;
119  p2.y = b;
120  }
121 
127  void crop(const _NanoRect &rect)
128  {
129  if ( p1.x < rect.p1.x )
130  p1.x = rect.p1.x;
131  if ( p1.y < rect.p1.y )
132  p1.y = rect.p1.y;
133  if ( p2.x > rect.p2.x )
134  p2.x = rect.p2.x;
135  if ( p2.y > rect.p2.y )
136  p2.y = rect.p2.y;
137  }
138 
143  bool collisionX(lcdint_t x) const
144  {
145  return (x >= p1.x) && (x <= p2.x);
146  }
147 
152  bool collisionY(lcdint_t y) const
153  {
154  return (y >= p1.y) && (y <= p2.y);
155  };
156 
161  bool collision(const NanoPoint &p) const
162  {
163  return collisionX(p.x) && collisionY(p.y);
164  }
165 
171  bool contains(const NanoPoint &p) const
172  {
173  return collision(p);
174  }
175 
181  bool contains(const _NanoRect &r) const
182  {
183  return contains(r.p1) && contains(r.p2);
184  }
185 
191  bool containsPartOf(const _NanoRect &r) const
192  {
193  return contains(r.p1) || contains(r.p2);
194  }
195 
200  bool above(const NanoPoint &p) const
201  {
202  return (p.y < p1.y);
203  };
204 
209  bool below(const NanoPoint &p) const
210  {
211  return (p.y > p2.y);
212  };
213 
219  {
220  return {{static_cast<lcdint_t>(p1.x - p.x), static_cast<lcdint_t>(p1.y - p.y)},
221  {static_cast<lcdint_t>(p2.x - p.x), static_cast<lcdint_t>(p2.y - p.y)}};
222  }
223 
229  {
230  return {{static_cast<lcdint_t>(p1.x + p.x), static_cast<lcdint_t>(p1.y + p.y)},
231  {static_cast<lcdint_t>(p2.x + p.x), static_cast<lcdint_t>(p2.y + p.y)}};
232  }
233 
239  {
240  p1.x += p.x;
241  p1.y += p.y;
242  p2.x += p.x;
243  p2.y += p.y;
244  return *this;
245  }
246 
251  _NanoRect operator>>(const uint8_t bits) const
252  {
253  return {p1 >> bits, p2 >> bits};
254  };
255 
260  _NanoRect operator<<(const uint8_t bits) const
261  {
262  return {p1 << bits, p2 << bits};
263  };
264 
265 } NanoRect;
266 
267 #ifndef DOXYGEN_SHOULD_SKIP_THIS
268 inline NanoRect operator+(const NanoRect &rect, const NanoPoint &p)
269 {
270  return {{(lcdint_t)(rect.p1.x + p.x), (lcdint_t)(rect.p1.y + p.y)},
271  {(lcdint_t)(rect.p2.x + p.x), (lcdint_t)(rect.p2.y + p.y)}};
272 }
273 #endif
274 
279 #endif
void crop(const _NanoRect &rect)
Crops rectangle to fit specified area.
Definition: rect.h:127
_NanoRect operator-(const _NanoPoint &p)
Returns true if specified point is above rectangle area.
Definition: rect.h:218
_NanoRect operator+(const _NanoPoint &p)
Add point to all points of rectangle.
Definition: rect.h:228
Describes point.
Definition: point.h:39
bool collisionY(lcdint_t y) const
Returns true if specified y position is between left and right borders.
Definition: rect.h:152
NanoRect structure describes rectangle area.
Definition: rect.h:42
lcdint_t height() const
returns height of NanoRect
Definition: rect.h:69
Point class.
void addV(lcdint_t dy)
Shifts rectangle area by dy pixels.
Definition: rect.h:101
NanoPoint p2
right-bottom point of the rectangle area
Definition: rect.h:48
int8_t lcdint_t
internal int type, used by the library.
Definition: canvas_types.h:77
_NanoRect operator<<(const uint8_t bits) const
Shifts left x,y value of the point by bits value.
Definition: rect.h:260
bool above(const NanoPoint &p) const
Returns true if specified point is above rectangle area.
Definition: rect.h:200
const NanoPoint center() const
returns center point of NanoRect
Definition: rect.h:63
lcdint_t y
y position in pixels
Definition: point.h:44
bool collision(const NanoPoint &p) const
Returns true if specified point is inside rectangle area.
Definition: rect.h:161
Basic structures of canvas gfx library.
_NanoRect operator>>(const uint8_t bits) const
Shifts right x,y value of the point by bits value.
Definition: rect.h:251
struct _NanoRect NanoRect
NanoRect structure describes rectangle area.
bool contains(const NanoPoint &p) const
Returns true of point belongs to rectangle area.
Definition: rect.h:171
void setRect(lcdint_t l, lcdint_t t, lcdint_t r, lcdint_t b)
Initializes NanoRect with specified values.
Definition: rect.h:114
_NanoRect & operator+=(const _NanoPoint &p)
Subtracts point to all points of rectangle.
Definition: rect.h:238
void addH(lcdint_t dx)
Shifts rectangle area by dx pixels.
Definition: rect.h:91
bool below(const NanoPoint &p) const
Returns true if specified point is below rectangle area.
Definition: rect.h:209
bool collisionX(lcdint_t x) const
Returns true if specified x position is between left and right borders.
Definition: rect.h:143
void move(lcdint_t dx, lcdint_t dy)
Shifts rectangle area by dx;dy pixels.
Definition: rect.h:79
lcdint_t width() const
returns width of NanoRect
Definition: rect.h:51
const NanoPoint size() const
returns size of NanoRect
Definition: rect.h:57
bool contains(const _NanoRect &r) const
Returns true if whole rectangle belongs to rectangle area.
Definition: rect.h:181
bool containsPartOf(const _NanoRect &r) const
Returns true if rectangle topleft or rightbottom points belong to rectangle area. ...
Definition: rect.h:191
NanoPoint p1
top-left point of the rectangle area
Definition: rect.h:45
lcdint_t x
x position in pixels
Definition: point.h:42