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,2024 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 FXRectangle& src):x(src.x),y(src.y),w(src.w),h(src.h){ }
44  FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
45  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){ }
46 
48  FXbool empty() const { return w<=0 || h<=0; }
49 
51  FXbool operator!() const { return x==0 && y==0 && w==0 && h==0; }
52 
54  FXbool operator==(const FXRectangle& r) const { return x==r.x && y==r.y && w==r.w && h==r.h; }
55  FXbool operator!=(const FXRectangle& r) const { return x!=r.x || y!=r.y || w!=r.w || h!=r.h; }
56 
58  FXbool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
59  FXbool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
60 
62  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; }
63 
65  FXRectangle& move(const FXPoint& p){ x+=p.x; y+=p.y; return *this; }
66  FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
67 
69  FXRectangle& grow(FXshort margin);
70  FXRectangle& grow(FXshort hormargin,FXshort vermargin);
71  FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
72 
74  FXRectangle& shrink(FXshort margin);
75  FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
76  FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
77 
79  FXPoint tl() const { return FXPoint(x,y); }
80  FXPoint tr() const { return FXPoint(x+w-1,y); }
81  FXPoint bl() const { return FXPoint(x,y+h-1); }
82  FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
83 
85  FXRectangle& operator=(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
86 
88  FXRectangle& set(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
89 
91  FXRectangle& set(const FXPoint& p,const FXSize& s){ x=p.x; y=p.y; w=s.w; h=s.h; return *this; }
92 
94  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; }
95 
97  FXRectangle& set(FXshort xx,FXshort yy,FXshort ww,FXshort hh){ x=xx; y=yy; w=ww; h=hh; return *this; }
98 
100  void bite(FXRectangle pieces[],const FXRectangle& b) const;
101 
103  FXRectangle& operator+=(const FXRectangle &r);
104  FXRectangle& operator*=(const FXRectangle &r);
105 
107  FXRectangle operator+(const FXRectangle& r) const;
108  FXRectangle operator*(const FXRectangle& r) const;
109  };
110 
111 
113 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; }
114 
116 extern FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
117 
119 extern FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
120 
121 }
122 
123 #endif
Rectangle.
Definition: FXRectangle.h:32
FXbool contains(const FXPoint &p) const
Point in rectangle.
Definition: FXRectangle.h:58
FXRectangle & move(const FXPoint &p)
Return moved rectangle.
Definition: FXRectangle.h:65
FXRectangle & operator=(const FXRectangle &r)
Assignment.
Definition: FXRectangle.h:85
Size.
Definition: FXSize.h:28
FXPoint tl() const
Corners.
Definition: FXRectangle.h:79
FXRectangle()
Constructors.
Definition: FXRectangle.h:41
FXbool empty() const
Test if empty.
Definition: FXRectangle.h:48
Definition: FX4Splitter.h:28
FXbool contains(const FXRectangle &r) const
Rectangle properly contained in rectangle.
Definition: FXRectangle.h:62
FXbool operator==(const FXRectangle &r) const
Equality.
Definition: FXRectangle.h:54
Point.
Definition: FXPoint.h:32
FXbool operator!() const
Test if zero.
Definition: FXRectangle.h:51

Copyright © 1997-2022 Jeroen van der Zijp