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,2009 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 * $Id: FXRectangle.h,v 1.27 2009/01/06 13:07:26 fox Exp $                       *
00022 ********************************************************************************/
00023 #ifndef FXRECTANGLE_H
00024 #define FXRECTANGLE_H
00025 
00026 
00027 #ifndef FXPOINT_H
00028 #include "FXPoint.h"
00029 #endif
00030 
00031 
00032 namespace FX {
00033 
00034 
00035 /// Rectangle
00036 class FXAPI FXRectangle {
00037 public:
00038   FXshort x;
00039   FXshort y;
00040   FXshort w;
00041   FXshort h;
00042 public:
00043 
00044   /// Constructors
00045   FXRectangle(){ }
00046   FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
00047   FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
00048   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){ }
00049 
00050   /// Test if empty
00051   FXbool empty() const { return w<=0 || h<=0; }
00052 
00053   /// Test if zero
00054   FXbool operator!() const { return x==0 && y==0 && w==0 && h==0; }
00055 
00056   /// Equality
00057   FXbool operator==(const FXRectangle& r) const { return x==r.x && y==r.y && w==r.w && h==r.h; }
00058   FXbool operator!=(const FXRectangle& r) const { return x!=r.x || y!=r.y || w!=r.w || h!=r.h; }
00059 
00060   /// Point in rectangle
00061   FXbool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
00062   FXbool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
00063 
00064   /// Rectangle properly contained in rectangle
00065   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; }
00066 
00067   /// Rectangles overlap
00068   friend inline FXbool overlap(const FXRectangle& a,const FXRectangle& b);
00069 
00070   /// Return moved rectangle
00071   FXRectangle& move(const FXPoint& p){ x+=p.x; y+=p.y; return *this; }
00072   FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
00073 
00074   /// Grow by amount
00075   FXRectangle& grow(FXshort margin);
00076   FXRectangle& grow(FXshort hormargin,FXshort vermargin);
00077   FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00078 
00079   /// Shrink by amount
00080   FXRectangle& shrink(FXshort margin);
00081   FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
00082   FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00083 
00084   /// Corners
00085   FXPoint tl() const { return FXPoint(x,y); }
00086   FXPoint tr() const { return FXPoint(x+w-1,y); }
00087   FXPoint bl() const { return FXPoint(x,y+h-1); }
00088   FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
00089 
00090   /// Assignment
00091   FXRectangle& operator=(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
00092 
00093   /// Set value from another rectangle
00094   FXRectangle& set(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
00095 
00096   /// Set from point and size
00097   FXRectangle& set(const FXPoint& p,const FXSize& s){ x=p.x; y=p.y; w=s.w; h=s.h; return *this; }
00098 
00099   /// Set from corners
00100   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; }
00101 
00102   /// Set value from components
00103   FXRectangle& set(FXshort xx,FXshort yy,FXshort ww,FXshort hh){ x=xx; y=yy; w=ww; h=hh; return *this; }
00104 
00105   /// Pieces of this rectangle after taking a bite out of it
00106   void bite(FXRectangle pieces[],const FXRectangle& b) const;
00107 
00108   /// Union and intersection with rectangle
00109   FXRectangle& operator+=(const FXRectangle &r);
00110   FXRectangle& operator*=(const FXRectangle &r);
00111 
00112   /// Union and intersection between rectangles
00113   FXRectangle operator+(const FXRectangle& r) const;
00114   FXRectangle operator*(const FXRectangle& r) const;
00115 
00116   /// Save object to a stream
00117   friend FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00118 
00119   /// Load object from a stream
00120   friend FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00121   };
00122 
00123 
00124 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; }
00125 
00126 extern FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00127 extern FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00128 
00129 }
00130 
00131 #endif

Copyright © 1997-2009 Jeroen van der Zijp