Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXRectangle.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * R e c t a n g l e C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1994,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXRectangle.h,v 1.19 2006/01/22 17:58:08 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXRECTANGLE_H
25 #define FXRECTANGLE_H
26 
27 
28 #ifndef FXPOINT_H
29 #include "FXPoint.h"
30 #endif
31 
32 
33 namespace FX {
34 
35 
36 /// Rectangle
38 public:
43 public:
44 
45  /// Constructors
47  FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
48  FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
49  FXRectangle(const FXPoint& topleft,const FXPoint& bottomright):x(topleft.x),y(topleft.y),w(bottomright.x-topleft.x+1),h(bottomright.y-topleft.y+1){ }
50 
51  /// Test if empty
52  bool empty() const { return w<=0 || h<=0; }
53 
54  /// Test if zero
55  bool operator!() const { return x==0 && y==0 && w==0 && h==0; }
56 
57  /// Equality
58  bool operator==(const FXRectangle& r) const { return x==r.x && y==r.y && w==r.w && h==r.h; }
59  bool operator!=(const FXRectangle& r) const { return x!=r.x || y!=r.y || w!=r.w || h!=r.h; }
60 
61  /// Point in rectangle
62  bool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
63  bool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
64 
65  /// Rectangle properly contained in rectangle
66  bool contains(const FXRectangle& r) const { return x<=r.x && y<=r.y && r.x+r.w<=x+w && r.y+r.h<=y+h; }
67 
68  /// Rectangles overlap
69  friend inline bool overlap(const FXRectangle& a,const FXRectangle& b);
70 
71  /// Return moved rectangle
72  FXRectangle& move(const FXPoint& p){ x+=p.x; y+=p.y; return *this; }
73  FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
74 
75  /// Grow by amount
76  FXRectangle& grow(FXshort margin);
77  FXRectangle& grow(FXshort hormargin,FXshort vermargin);
78  FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
79 
80  /// Shrink by amount
81  FXRectangle& shrink(FXshort margin);
82  FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
83  FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
84 
85  /// Corners
86  FXPoint tl() const { return FXPoint(x,y); }
87  FXPoint tr() const { return FXPoint(x+w-1,y); }
88  FXPoint bl() const { return FXPoint(x,y+h-1); }
89  FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
90 
91  /// Assignment
92  FXRectangle& operator=(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
93 
94  /// Set value from another rectangle
95  FXRectangle& set(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
96 
97  /// Set from point and size
98  FXRectangle& set(const FXPoint& p,const FXSize& s){ x=p.x; y=p.y; w=s.w; h=s.h; return *this; }
99 
100  /// Set from corners
101  FXRectangle& set(const FXPoint& topleft,const FXPoint& bottomright){ x=topleft.x; y=topleft.y; w=bottomright.x-topleft.x+1; h=bottomright.y-topleft.y+1; return *this; }
102 
103  /// Set value from components
104  FXRectangle& set(FXshort xx,FXshort yy,FXshort ww,FXshort hh){ x=xx; y=yy; w=ww; h=hh; return *this; }
105 
106  /// Union and intersection with rectangle
107  FXRectangle& operator+=(const FXRectangle &r);
108  FXRectangle& operator*=(const FXRectangle &r);
109 
110  /// Union and intersection between rectangles
111  FXRectangle operator+(const FXRectangle& r) const;
112  FXRectangle operator*(const FXRectangle& r) const;
113 
114  /// Save object to a stream
115  friend FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
116 
117  /// Load object from a stream
118  friend FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
119  };
120 
121 
122 inline bool overlap(const FXRectangle& a,const FXRectangle& b){ return b.x<a.x+a.w && b.y<a.y+a.h && a.x<b.x+b.w && a.y<b.y+b.h; }
123 
124 extern FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
125 extern FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
126 
127 }
128 
129 #endif
FXPoint br() const
Definition: FXRectangle.h:89
FXStream & operator>>(FXStream &store, FXDate &d)
FXPoint bl() const
Definition: FXRectangle.h:88
FXRectangle & set(const FXPoint &p, const FXSize &s)
Set from point and size.
Definition: FXRectangle.h:98
FXRectangle & set(const FXRectangle &r)
Set value from another rectangle.
Definition: FXRectangle.h:95
FXshort x
Definition: FXRectangle.h:39
Rectangle.
Definition: FXRectangle.h:37
short FXshort
Definition: fxdefs.h:388
FXRectangle & move(const FXPoint &p)
Return moved rectangle.
Definition: FXRectangle.h:72
FXRectangle & operator=(const FXRectangle &r)
Assignment.
Definition: FXRectangle.h:92
Size.
Definition: FXSize.h:32
FXRectangle & set(const FXPoint &topleft, const FXPoint &bottomright)
Set from corners.
Definition: FXRectangle.h:101
FXshort h
Definition: FXRectangle.h:42
#define FXAPI
Definition: fxdefs.h:122
FXPoint tl() const
Corners.
Definition: FXRectangle.h:86
FXshort h
Definition: FXSize.h:35
bool operator!=(const FXRectangle &r) const
Definition: FXRectangle.h:59
FXRectangle()
Constructors.
Definition: FXRectangle.h:46
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
FXshort y
Definition: FXPoint.h:38
Definition: FX4Splitter.h:31
FXshort x
Definition: FXPoint.h:37
FXDate operator+(const FXDate &d, FXint x)
Definition: FXDate.h:148
bool overlap(const FXExtentd &a, const FXExtentd &b)
bool operator==(const FXRectangle &r) const
Equality.
Definition: FXRectangle.h:58
FXshort y
Definition: FXRectangle.h:40
bool empty() const
Test if empty.
Definition: FXRectangle.h:52
bool contains(const FXRectangle &r) const
Rectangle properly contained in rectangle.
Definition: FXRectangle.h:66
Point.
Definition: FXPoint.h:35
FXRectangle & move(FXshort dx, FXshort dy)
Definition: FXRectangle.h:73
bool operator!() const
Test if zero.
Definition: FXRectangle.h:55
FXStream & operator<<(FXStream &store, const FXDate &d)
FXPoint tr() const
Definition: FXRectangle.h:87
FXRectangle(const FXPoint &p, const FXSize &s)
Definition: FXRectangle.h:48
FXshort w
Definition: FXRectangle.h:41
FXRectangle(FXshort xx, FXshort yy, FXshort ww, FXshort hh)
Definition: FXRectangle.h:47
bool contains(FXshort xx, FXshort yy) const
Definition: FXRectangle.h:63
FXshort w
Definition: FXSize.h:34
FXMat3d operator*(FXdouble x, const FXMat3d &a)
FXRectangle & set(FXshort xx, FXshort yy, FXshort ww, FXshort hh)
Set value from components.
Definition: FXRectangle.h:104
FXRectangle(const FXPoint &topleft, const FXPoint &bottomright)
Definition: FXRectangle.h:49
bool contains(const FXPoint &p) const
Point in rectangle.
Definition: FXRectangle.h:62

Copyright © 1997-2005 Jeroen van der Zijp