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

FXRectangle.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                          R e c t a n g l e    C l a s s                       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1994,2010 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXRECTANGLE_H
00022 #define FXRECTANGLE_H
00023 
00024 
00025 #ifndef FXPOINT_H
00026 #include "FXPoint.h"
00027 #endif
00028 
00029 
00030 namespace FX {
00031 
00032 
00033 /// Rectangle
00034 class FXAPI FXRectangle {
00035 public:
00036   FXshort x;
00037   FXshort y;
00038   FXshort w;
00039   FXshort h;
00040 public:
00041 
00042   /// Constructors
00043   FXRectangle(){ }
00044   FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
00045   FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
00046   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){ }
00047 
00048   /// Test if empty
00049   FXbool empty() const { return w<=0 || h<=0; }
00050 
00051   /// Test if zero
00052   FXbool operator!() const { return x==0 && y==0 && w==0 && h==0; }
00053 
00054   /// Equality
00055   FXbool operator==(const FXRectangle& r) const { return x==r.x && y==r.y && w==r.w && h==r.h; }
00056   FXbool operator!=(const FXRectangle& r) const { return x!=r.x || y!=r.y || w!=r.w || h!=r.h; }
00057 
00058   /// Point in rectangle
00059   FXbool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
00060   FXbool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
00061 
00062   /// Rectangle properly contained in rectangle
00063   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; }
00064 
00065   /// Return moved rectangle
00066   FXRectangle& move(const FXPoint& p){ x+=p.x; y+=p.y; return *this; }
00067   FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
00068 
00069   /// Grow by amount
00070   FXRectangle& grow(FXshort margin);
00071   FXRectangle& grow(FXshort hormargin,FXshort vermargin);
00072   FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00073 
00074   /// Shrink by amount
00075   FXRectangle& shrink(FXshort margin);
00076   FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
00077   FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00078 
00079   /// Corners
00080   FXPoint tl() const { return FXPoint(x,y); }
00081   FXPoint tr() const { return FXPoint(x+w-1,y); }
00082   FXPoint bl() const { return FXPoint(x,y+h-1); }
00083   FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
00084 
00085   /// Assignment
00086   FXRectangle& operator=(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
00087 
00088   /// Set value from another rectangle
00089   FXRectangle& set(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
00090 
00091   /// Set from point and size
00092   FXRectangle& set(const FXPoint& p,const FXSize& s){ x=p.x; y=p.y; w=s.w; h=s.h; return *this; }
00093 
00094   /// Set from corners
00095   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; }
00096 
00097   /// Set value from components
00098   FXRectangle& set(FXshort xx,FXshort yy,FXshort ww,FXshort hh){ x=xx; y=yy; w=ww; h=hh; return *this; }
00099 
00100   /// Pieces of this rectangle after taking a bite out of it
00101   void bite(FXRectangle pieces[],const FXRectangle& b) const;
00102 
00103   /// Union and intersection with rectangle
00104   FXRectangle& operator+=(const FXRectangle &r);
00105   FXRectangle& operator*=(const FXRectangle &r);
00106 
00107   /// Union and intersection between rectangles
00108   FXRectangle operator+(const FXRectangle& r) const;
00109   FXRectangle operator*(const FXRectangle& r) const;
00110   };
00111 
00112 
00113 /// Rectangles overlap
00114 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; }
00115 
00116 /// Save object to a stream
00117 extern FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00118 
00119 /// Load object from a stream
00120 extern FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00121 
00122 }
00123 
00124 #endif

Copyright © 1997-2010 Jeroen van der Zijp