Numworks Epsilon  1.4.1
Graphing Calculator Operating System
rect.cpp
Go to the documentation of this file.
1 #include <kandinsky/rect.h>
2 
4  m_x(p.x()), m_y(p.y()),
5  m_width(s.width()), m_height(s.height())
6 {
7 }
8 
10  m_x(x), m_y(y),
11  m_width(s.width()), m_height(s.height())
12 {
13 }
14 
16  m_x(p.x()), m_y(p.y()),
17  m_width(width), m_height(height)
18 {
19 }
20 
21 void KDRect::setOrigin(KDPoint p) { m_x = p.x(); m_y = p.y(); }
22 void KDRect::setSize(KDSize s) { m_width = s.width(); m_height = s.height(); }
23 
24 bool KDRect::intersects(const KDRect & other) const {
25  return (
26  other.right() >= left() &&
27  other.left() <= right() &&
28  other.top() <= bottom() &&
29  other.bottom() >= top()
30  );
31 }
32 
33 KDRect KDRect::intersectedWith(const KDRect & other) const {
34  if (!intersects(other)) {
35  return KDRectZero;
36  }
37 
38  KDCoordinate intersectionLeft = max(left(), other.left());
39  KDCoordinate intersectionRight = min(right(), other.right());
40  KDCoordinate intersectionTop = max(top(), other.top());
41  KDCoordinate intersectionBottom = min(bottom(), other.bottom());
42 
43  return KDRect(
44  intersectionLeft,
45  intersectionTop,
46  intersectionRight-intersectionLeft+1,
47  intersectionBottom-intersectionTop+1);
48 }
49 
51  KDCoordinate * outputMin, KDCoordinate * outputMax,
52  KDCoordinate min1, KDCoordinate min2,
53  KDCoordinate max1, KDCoordinate max2)
54 {
55  if (size1 != 0) {
56  if (size2 != 0) {
57  *outputMin = min(min1, min2);
58  *outputMax = max(max1, max2);
59  } else {
60  *outputMin = min1;
61  *outputMax = max1;
62  }
63  } else {
64  if (size2 != 0) {
65  *outputMin = min2;
66  *outputMax = max2;
67  }
68  }
69 }
70 
71 KDRect KDRect::unionedWith(const KDRect & other) const {
72  if (this->isEmpty()) {
73  return other;
74  }
75  if (other.isEmpty()) {
76  return *this;
77  }
78  /* We should ignore coordinate whose size is zero
79  * For example, if r1.height is zero, just ignore r1.y and r1.height. */
80 
81  KDCoordinate resultLeft = 0;
82  KDCoordinate resultTop = 0;
83  KDCoordinate resultRight = 0;
84  KDCoordinate resultBottom = 0;
85 
86  computeUnionBound(width(), other.width(),
87  &resultLeft, &resultRight,
88  left(), other.left(),
89  right(), other.right());
90 
91  computeUnionBound(height(), other.height(),
92  &resultTop, &resultBottom,
93  top(), other.top(),
94  bottom(), other.bottom());
95 
96  return KDRect(
97  resultLeft,
98  resultTop,
99  resultRight-resultLeft+1,
100  resultBottom-resultTop+1
101  );
102 }
103 
104 bool KDRect::contains(KDPoint p) const {
105  return (p.x() >= x() && p.x() <= right() && p.y() >= y() && p.y() <= bottom());
106 }
107 
109  return KDRect(x() + p.x(), y() + p.y(), width(), height());
110 }
111 
113  return KDRect(p.x(), p.y(), width(), height());
114 }
115 
116 bool KDRect::isEmpty() const {
117  return (width() == 0 || height() == 0);
118 }
void computeUnionBound(KDCoordinate size1, KDCoordinate size2, KDCoordinate *outputMin, KDCoordinate *outputMax, KDCoordinate min1, KDCoordinate min2, KDCoordinate max1, KDCoordinate max2)
Definition: rect.cpp:50
KDRect translatedBy(KDPoint p) const
Definition: rect.cpp:108
KDCoordinate y() const
Definition: point.h:11
bool contains(KDPoint p) const
Definition: rect.cpp:104
KDCoordinate x() const
Definition: rect.h:36
int16_t KDCoordinate
Definition: coordinate.h:6
constexpr KDCoordinate width() const
Definition: size.h:10
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
KDCoordinate width() const
Definition: rect.h:39
void setSize(KDSize size)
Definition: rect.cpp:22
KDRect movedTo(KDPoint p) const
Definition: rect.cpp:112
KDCoordinate x() const
Definition: point.h:10
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
constexpr KDCoordinate height() const
Definition: size.h:11