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

FXRectangle.h
1 /********************************************************************************
2 * *
3 * R e c t a n g l e C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1994,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (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 *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXRECTANGLE_H
22 #define FXRECTANGLE_H
23 
24 #ifndef FXPOINT_H
25 #include "FXPoint.h"
26 #endif
27 
28 namespace FX {
29 
30 
32 class FXAPI FXRectangle {
33 public:
34  FXshort x;
35  FXshort y;
36  FXshort w;
37  FXshort h;
38 public:
39 
42  FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
43  FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
44  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){ }
45 
47  FXbool empty() const { return w<=0 || h<=0; }
48 
50  FXbool operator!() const { return x==0 && y==0 && w==0 && h==0; }
51 
53  FXbool operator==(const FXRectangle& r) const { return x==r.x && y==r.y && w==r.w && h==r.h; }
54  FXbool operator!=(const FXRectangle& r) const { return x!=r.x || y!=r.y || w!=r.w || h!=r.h; }
55 
57  FXbool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
58  FXbool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
59 
61  FXbool contains(const FXRectangle& r) const { return x<=r.x && y<=r.y && r.x+r.w<=x+w && r.y+r.h<=y+h; }
62 
64  FXRectangle& move(const FXPoint& p){ x+=p.x; y+=p.y; return *this; }
65  FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
66 
68  FXRectangle& grow(FXshort margin);
69  FXRectangle& grow(FXshort hormargin,FXshort vermargin);
70  FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
71 
73  FXRectangle& shrink(FXshort margin);
74  FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
75  FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
76 
78  FXPoint tl() const { return FXPoint(x,y); }
79  FXPoint tr() const { return FXPoint(x+w-1,y); }
80  FXPoint bl() const { return FXPoint(x,y+h-1); }
81  FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
82 
84  FXRectangle& operator=(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
85 
87  FXRectangle& set(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
88 
90  FXRectangle& set(const FXPoint& p,const FXSize& s){ x=p.x; y=p.y; w=s.w; h=s.h; return *this; }
91 
93  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; }
94 
96  FXRectangle& set(FXshort xx,FXshort yy,FXshort ww,FXshort hh){ x=xx; y=yy; w=ww; h=hh; return *this; }
97 
99  void bite(FXRectangle pieces[],const FXRectangle& b) const;
100 
102  FXRectangle& operator+=(const FXRectangle &r);
103  FXRectangle& operator*=(const FXRectangle &r);
104 
106  FXRectangle operator+(const FXRectangle& r) const;
107  FXRectangle operator*(const FXRectangle& r) const;
108  };
109 
110 
112 inline FXbool 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; }
113 
115 extern FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
116 
118 extern FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
119 
120 }
121 
122 #endif

Copyright © 1997-2016 Jeroen van der Zijp