dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
rectangle.h
Go to the documentation of this file.
1 
13 /*
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, see <http://www.gnu.org/licenses/>
26 */
27 #ifndef RECTANGLE_H
28 #define RECTANGLE_H
29 #include "../core/size.h"
30 #include "math.h"
31 using namespace core;
32 namespace internals
33 {
34 struct Rectangle
35 {
36 
37  friend quint32 qHash(Rectangle const& rect);
38  friend bool operator==(Rectangle const& lhs,Rectangle const& rhs);
39 public:
40  static Rectangle Empty;
41  static Rectangle FromLTRB(qint32 left, qint32 top, qint32 right, qint32 bottom);
42  Rectangle(){x=0; y=0; width=0; height=0; }
43  Rectangle(qint32 x, qint32 y, qint32 width, qint32 height)
44  {
45  this->x = x;
46  this->y = y;
47  this->width = width;
48  this->height = height;
49  }
51  {
52  this->x = location.X();
53  this->y = location.Y();
54  this->width = size.Width();
55  this->height = size.Height();
56  }
58  return core::Point(x, y);
59  }
60  void SetLocation(const core::Point &value)
61  {
62  x = value.X();
63  y = value.Y();
64  }
65  qint32 X(){return x;}
66  qint32 Y(){return y;}
67  void SetX(const qint32 &value){x=value;}
68  void SetY(const qint32 &value){y=value;}
69  qint32 Width(){return width;}
70  void SetWidth(const qint32 &value){width=value;}
71  qint32 Height(){return height;}
72  void SetHeight(const qint32 &value){height=value;}
73  qint32 Left(){return x;}
74  qint32 Top(){return y;}
75  qint32 Right(){return x+width;}
76  qint32 Bottom(){return y+height;}
77  bool IsEmpty(){return (height==0 && width==0 && x==0 && y==0);}
78  bool operator==(const Rectangle &cSource)
79  {
80  return (cSource.x == x && cSource.y == y && cSource.width == width && cSource.height == height);
81  }
82 
83 
84  bool operator!=(const Rectangle &cSource){return !(*this==cSource);}
85  bool Contains(const qint32 &x, const qint32 &y)
86  {
87  return this->x<=x && x<this->x+this->width && this->y<=y && y<this->y+this->height;
88  }
89  bool Contains(const core::Point &pt)
90  {
91  return Contains(pt.X(),pt.Y());
92  }
93  bool Contains(const Rectangle &rect)
94  {
95  return (this->x <= rect.x) &&
96  ((rect.x + rect.width) <= (this->x + this->width)) &&
97  (this->y <= rect.y) &&
98  ((rect.y + rect.height) <= (this->y + this->height));
99  }
100 
101 
102  void Inflate(const qint32 &width,const qint32 &height)
103  {
104  this->x -= width;
105  this->y -= height;
106  this->width += 2*width;
107  this->height += 2*height;
108  }
109  void Inflate(Size &size)
110  {
111 
112  Inflate(size.Width(), size.Height());
113  }
114  static Rectangle Inflate(Rectangle rect, qint32 x, qint32 y);
115 
116  void Intersect(const Rectangle &rect)
117  {
118  Rectangle result = Rectangle::Intersect(rect, *this);
119 
120  this->x = result.X();
121  this->y = result.Y();
122  this->width = result.Width();
123  this->height = result.Height();
124  }
125  static Rectangle Intersect(Rectangle a, Rectangle b);
126  bool IntersectsWith(const Rectangle &rect)
127  {
128  return (rect.x < this->x + this->width) &&
129  (this->x < (rect.x + rect.width)) &&
130  (rect.y < this->y + this->height) &&
131  (this->y < rect.y + rect.height);
132  }
133  static Rectangle Union(const Rectangle &a,const Rectangle &b);
134  void Offset(const core::Point &pos)
135  {
136  Offset(pos.X(), pos.Y());
137  }
138 
139  void Offset(const qint32 &x,const qint32 &y)
140  {
141  this->x += x;
142  this->y += y;
143  }
144  QString ToString()
145  {
146  return "{X=" + QString::number(x) + ",Y=" + QString::number(y) +
147  ",Width=" + QString::number(width) +
148  ",Height=" +QString::number(height) +"}";
149  }
150 private:
151  qint32 x;
152  qint32 y;
153  qint32 width;
154  qint32 height;
155 };
156 }
157 #endif // RECTANGLE_H
qint64 X() const
Definition: point.h:51
Rectangle(core::Point location, core::Size size)
Definition: rectangle.h:50
qint64 Height() const
Definition: size.h:55
void SetY(const qint32 &value)
Definition: rectangle.h:68
bool operator==(Point const &lhs, Point const &rhs)
Definition: point.cpp:49
bool Contains(const core::Point &pt)
Definition: rectangle.h:89
static Rectangle Empty
Definition: rectangle.h:40
void Offset(const qint32 &x, const qint32 &y)
Definition: rectangle.h:139
void Inflate(Size &size)
Definition: rectangle.h:109
end a
Definition: OPPlots.m:98
void SetHeight(const qint32 &value)
Definition: rectangle.h:72
void SetLocation(const core::Point &value)
Definition: rectangle.h:60
void Offset(const core::Point &pos)
Definition: rectangle.h:134
bool operator==(const Rectangle &cSource)
Definition: rectangle.h:78
void SetWidth(const qint32 &value)
Definition: rectangle.h:70
qint64 Width() const
Definition: size.h:54
quint64 qHash(Point const &point)
Definition: point.cpp:45
Rectangle(qint32 x, qint32 y, qint32 width, qint32 height)
Definition: rectangle.h:43
bool Contains(const qint32 &x, const qint32 &y)
Definition: rectangle.h:85
core::Point GetLocation()
Definition: rectangle.h:57
bool operator!=(const Rectangle &cSource)
Definition: rectangle.h:84
void Inflate(const qint32 &width, const qint32 &height)
Definition: rectangle.h:102
x
Definition: OPPlots.m:100
bool IntersectsWith(const Rectangle &rect)
Definition: rectangle.h:126
void SetX(const qint32 &value)
Definition: rectangle.h:67
void Intersect(const Rectangle &rect)
Definition: rectangle.h:116
bool Contains(const Rectangle &rect)
Definition: rectangle.h:93
qint64 Y() const
Definition: point.h:52
y
Definition: OPPlots.m:101
QString ToString()
Definition: rectangle.h:144