Numworks Epsilon  1.4.1
Graphing Calculator Operating System
rect.h
Go to the documentation of this file.
1 #ifndef KANDINSKY_RECT_H
2 #define KANDINSKY_RECT_H
3 
4 #include <kandinsky/coordinate.h>
5 #include <kandinsky/point.h>
6 #include <kandinsky/size.h>
7 
8 /* +-+-+-+-+-+
9  * |x| | | | |
10  * +-+-+-+-+-+
11  * | | | | | |
12  * +-+-+-+-+-+
13  * | | | | |o|
14  * +-+-+-+-+-+
15  *
16  * Kandinsky rectangles are rectangles made of pixels. We defined them by the
17  * coordinates of the top left pixel and the rectangle dimensions in pixels.
18  * The pixel top left (located by the 'x' on the example) is at the coordinates
19  * (left(), top()) = (x(), y()). The pixel bottom right (located by a 'o' on
20  * the example) is at the coordinates (right(), bottom()). Also, the dimension
21  * of the rectangle is then (right()-left()+1, bottom()-top()+1).
22  * The example dimensions are (5, 3).
23  *
24  * */
25 
26 class KDRect {
27 public:
30  m_x(x), m_y(y), m_width(width), m_height(height) {}
31 
32  KDRect(KDPoint p, KDSize s);
35 
36  KDCoordinate x() const { return m_x; }
37  KDCoordinate y() const { return m_y; }
38  KDPoint origin() const { return KDPoint(m_x, m_y); }
39  KDCoordinate width() const { return m_width; }
40  KDCoordinate height() const { return m_height; }
41  KDSize size() const { return KDSize(m_width, m_height); }
42  KDCoordinate top() const { return m_y; }
43  KDCoordinate right() const { return m_x+m_width-1; }
44  KDCoordinate bottom() const { return m_y+m_height-1; }
45  KDCoordinate left() const { return m_x; }
46 
47  KDPoint topLeft() const { return KDPoint(left(), top()); }
48  KDPoint bottomRight() const { return KDPoint(right(), bottom()); }
49 
50  bool operator ==(const KDRect &other) const {
51  return (m_x == other.m_x && m_y == other.m_y
52  && m_width == other.m_width && m_height == other.m_height);
53  }
54 
55  void setOrigin(KDPoint origin);
56  void setSize(KDSize size);
57 
58  KDRect translatedBy(KDPoint p) const;
59  KDRect movedTo(KDPoint p) const;
60  bool intersects(const KDRect & other) const;
61  KDRect intersectedWith(const KDRect & other) const;
62  KDRect unionedWith(const KDRect & other) const; // Returns the smallest rectangle containing r1 and r2
63  bool contains(KDPoint p) const;
64  bool isEmpty() const;
65 
66 private:
67  KDCoordinate m_x, m_y, m_width, m_height;
68 };
69 
70 constexpr KDRect KDRectZero = KDRect(0,0,0,0);
71 
72 #endif
KDRect translatedBy(KDPoint p) const
Definition: rect.cpp:108
bool operator==(const KDRect &other) const
Definition: rect.h:50
bool contains(KDPoint p) const
Definition: rect.cpp:104
KDCoordinate x() const
Definition: rect.h:36
KDPoint bottomRight() const
Definition: rect.h:48
int16_t KDCoordinate
Definition: coordinate.h:6
bool isEmpty() const
Definition: rect.cpp:116
KDCoordinate top() const
Definition: rect.h:42
constexpr KDRect(KDCoordinate x, KDCoordinate y, KDCoordinate width, KDCoordinate height)
Definition: rect.h:28
KDRect unionedWith(const KDRect &other) const
Definition: rect.cpp:71
Definition: point.h:6
Definition: size.h:6
KDCoordinate right() const
Definition: rect.h:43
void setOrigin(KDPoint origin)
Definition: rect.cpp:21
KDCoordinate left() const
Definition: rect.h:45
KDCoordinate y() const
Definition: rect.h:37
constexpr KDRect KDRectZero
Definition: rect.h:70
Definition: rect.h:26
KDSize size() const
Definition: rect.h:41
KDPoint topLeft() const
Definition: rect.h:47
KDPoint origin() const
Definition: rect.h:38
KDCoordinate width() const
Definition: rect.h:39
void setSize(KDSize size)
Definition: rect.cpp:22
KDRect movedTo(KDPoint p) const
Definition: rect.cpp:112
KDRect intersectedWith(const KDRect &other) const
Definition: rect.cpp:33
KDCoordinate height() const
Definition: rect.h:40
bool intersects(const KDRect &other) const
Definition: rect.cpp:24
KDCoordinate bottom() const
Definition: rect.h:44