tbx  0.7.5
bbox.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all 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
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_BBOX_H_
26 #define TBX_BBOX_H_
27 
28 #include "point.h"
29 #include "size.h"
30 
31 namespace tbx
32 {
33 
37 class BBox
38 {
39 public:
43  BBox() {};
44 
48  BBox(int xmin, int ymin, int xmax, int ymax) : min(xmin,ymin), max(xmax,ymax) {}
49 
53  BBox(const BBox &other) : min(other.min), max(other.max) {}
54 
58  BBox(const Point &imin, const Point &imax) : min(imin), max(imax) {}
59 
63  BBox(const Point &imin, const Size &size) : min(imin), max(imin.x + size.width, imin.y + size.height) {}
64 
73 
79  const Point &bottom_left() const {return min;}
83  void bottom_left(const Point &pos) {min=pos;}
89  const Point &top_right() const {return max;}
93  void top_right(const Point &pos) {max = pos;}
94 
98  Point top_left() const {return Point(min.x,max.y);}
102  void top_left(const Point &pos) {min.x = pos.x; max.y = pos.y;}
106  Point bottom_right() const {return Point(max.x,min.y);}
110  void bottom_right(const Point &pos) {max.x = pos.x; min.y = pos.y;}
111 
115  BBox &operator=(const BBox &other) {min=other.min; max=other.max; return *this;}
116 
120  bool operator==(const BBox &other) const {return min==other.min && max == other.max;}
121 
125  bool operator!=(const BBox &other) const {return min!=other.min || max != other.max;}
126 
130  int width() const {return max.x - min.x;}
131 
135  int height() const {return max.y - min.y;}
136 
140  Size size() const {return max - min;}
141 
145  void size(int width, int height) {max.x = min.x + width; max.y = min.y + height;}
149  void size(const Size &size) {max.x = min.x + size.width; max.y = min.y + size.height;}
150 
154  void move(int bx, int by) {min.x+=bx; max.x+=bx; min.y+=by; max.y+=by;}
155 
159  void move_right(int bx) {min.x+=bx; max.x+=bx;}
160 
164  void move_left(int bx) {min.x-=bx; max.x-=bx;}
165 
169  void move_up(int by) {min.y+=by; max.y+=by;}
170 
174  void move_down(int by) {min.y-=by; max.y-=by;}
175 
182  void move_to(int x, int y)
183  {
184  max.x = x + max.x - min.x;
185  max.y = y + max.y - min.y;
186  min.x = x;
187  min.y = y;
188  }
189 
195  void move_to(const Point &pos)
196  {
197  max.x = pos.x + max.x - min.x;
198  max.y = pos.y + max.y - min.y;
199  min.x = pos.x;
200  min.y = pos.y;
201  }
202 
203 
207  void cover(const BBox &to_cover)
208  {
209  if (min.x > to_cover.min.x) min.x = to_cover.min.x;
210  if (min.y > to_cover.min.y) min.y = to_cover.min.y;
211  if (max.x < to_cover.max.x) max.x = to_cover.max.x;
212  if (max.y < to_cover.max.y) max.y = to_cover.max.y;
213  }
214 
218  void inflate(int amount)
219  {
220  min.x -= amount; max.x += amount;
221  min.y -= amount; max.y += amount;
222  }
223 
227  bool contains(const Point &pt) const
228  {
229  return (pt >= min && pt < max);
230  }
231 
235  bool contains(int x, int y) const
236  {
237  return (x >= min.x && x < max.x && y >= min.y && y < max.y);
238  }
239 
243  bool intersects(const BBox &other) const
244  {
245  return (max > other.min && min < other.max);
246  }
247 
251  void normalise()
252  {
253  if (min.x > max.x) {int t = min.x; min.x = max.x; max.x = t;}
254  if (min.y > max.y) {int t = min.y; min.y = max.y; max.y = t;}
255  }
256 
257 };
258 }
259 #endif /* BBOX_H_ */
void move_to(const Point &pos)
Move box to position without resizing.
Definition: bbox.h:195
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
int height
Height of size object.
Definition: size.h:58
BBox(const BBox &other)
Construct from another bounding box.
Definition: bbox.h:53
void move_to(int x, int y)
Move box to position without resizing.
Definition: bbox.h:182
Class to represent a two-dimensional size.
Definition: size.h:34
void size(int width, int height)
Resize the bounding box.
Definition: bbox.h:145
void top_right(const Point &pos)
Set the top right of the box.
Definition: bbox.h:93
int x
Definition: point.h:59
bool contains(const Point &pt) const
Check if point is in rectangle.
Definition: bbox.h:227
Class to represent a two dimensional bounding box.
Definition: bbox.h:37
void move_right(int bx)
Move box right.
Definition: bbox.h:159
void size(const Size &size)
Resize the bounding box.
Definition: bbox.h:149
int width
Width of size object.
Definition: size.h:57
bool operator!=(const BBox &other) const
Check if two bounding boxes are not equal.
Definition: bbox.h:125
void move_down(int by)
Move box down.
Definition: bbox.h:174
BBox(int xmin, int ymin, int xmax, int ymax)
Construct from the x and y coordinates.
Definition: bbox.h:48
Point min
Minimum coordinate of the bounding box.
Definition: bbox.h:68
void cover(const BBox &to_cover)
Increase bounds to cover given box.
Definition: bbox.h:207
int y
Definition: point.h:60
const Point & bottom_left() const
Get the bottom left of the box.
Definition: bbox.h:79
void normalise()
Ensure min.x <= max.x and min.y <= max.y.
Definition: bbox.h:251
Class to represent a position in two dimensional space.
Definition: point.h:36
int height() const
Return the height of the bounding box.
Definition: bbox.h:135
void top_left(const Point &pos)
Set top left of box.
Definition: bbox.h:102
Point max
Maximum coordinate of the bounding box.
Definition: bbox.h:72
Point top_left() const
Get top left of box.
Definition: bbox.h:98
BBox()
Uninitialised bounding box.
Definition: bbox.h:43
void bottom_left(const Point &pos)
Set the bottom left of the box.
Definition: bbox.h:83
void move_up(int by)
Move box up.
Definition: bbox.h:169
void bottom_right(const Point &pos)
Set bottom right of box.
Definition: bbox.h:110
const Point & top_right() const
Get the top right of the box.
Definition: bbox.h:89
void move(int bx, int by)
Move whole box by given amounts.
Definition: bbox.h:154
BBox & operator=(const BBox &other)
Assign value from another bounding box.
Definition: bbox.h:115
void move_left(int bx)
Move box left.
Definition: bbox.h:164
void inflate(int amount)
Inflate box by given amount on each side.
Definition: bbox.h:218
BBox(const Point &imin, const Size &size)
Construct from the minimum point and size of the box.
Definition: bbox.h:63
bool intersects(const BBox &other) const
Check if bounds intesect with another rect.
Definition: bbox.h:243
Size size() const
Return the width and height of the bounding box as a Size.
Definition: bbox.h:140
Point bottom_right() const
Get bottom right of box.
Definition: bbox.h:106
bool contains(int x, int y) const
Check if point is in rectangle.
Definition: bbox.h:235
BBox(const Point &imin, const Point &imax)
Construct from the minimum and maximum points of the box.
Definition: bbox.h:58
int width() const
Return the width of the bounding box.
Definition: bbox.h:130
bool operator==(const BBox &other) const
Check if two bounding boxes are equal.
Definition: bbox.h:120