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