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,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXRectangle.h,v 1.19 2006/01/22 17:58:08 fox Exp $                       *
00023 ********************************************************************************/
00024 #ifndef FXRECTANGLE_H
00025 #define FXRECTANGLE_H
00026 
00027 
00028 #ifndef FXPOINT_H
00029 #include "FXPoint.h"
00030 #endif
00031 
00032 
00033 namespace FX {
00034 
00035 
00036 /// Rectangle
00037 class FXAPI FXRectangle {
00038 public:
00039   FXshort x;
00040   FXshort y;
00041   FXshort w;
00042   FXshort h;
00043 public:
00044 
00045   /// Constructors
00046   FXRectangle(){ }
00047   FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
00048   FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
00049   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){ }
00050 
00051   /// Test if empty
00052   bool empty() const { return w<=0 || h<=0; }
00053 
00054   /// Test if zero
00055   bool operator!() const { return x==0 && y==0 && w==0 && h==0; }
00056 
00057   /// Equality
00058   bool operator==(const FXRectangle& r) const { return x==r.x && y==r.y && w==r.w && h==r.h; }
00059   bool operator!=(const FXRectangle& r) const { return x!=r.x || y!=r.y || w!=r.w || h!=r.h; }
00060 
00061   /// Point in rectangle
00062   bool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
00063   bool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
00064 
00065   /// Rectangle properly contained in rectangle
00066   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; }
00067 
00068   /// Rectangles overlap
00069   friend inline bool overlap(const FXRectangle& a,const FXRectangle& b);
00070 
00071   /// Return moved rectangle
00072   FXRectangle& move(const FXPoint& p){ x+=p.x; y+=p.y; return *this; }
00073   FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
00074 
00075   /// Grow by amount
00076   FXRectangle& grow(FXshort margin);
00077   FXRectangle& grow(FXshort hormargin,FXshort vermargin);
00078   FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00079 
00080   /// Shrink by amount
00081   FXRectangle& shrink(FXshort margin);
00082   FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
00083   FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00084 
00085   /// Corners
00086   FXPoint tl() const { return FXPoint(x,y); }
00087   FXPoint tr() const { return FXPoint(x+w-1,y); }
00088   FXPoint bl() const { return FXPoint(x,y+h-1); }
00089   FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
00090 
00091   /// Assignment
00092   FXRectangle& operator=(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
00093 
00094   /// Set value from another rectangle
00095   FXRectangle& set(const FXRectangle& r){ x=r.x; y=r.y; w=r.w; h=r.h; return *this; }
00096 
00097   /// Set from point and size
00098   FXRectangle& set(const FXPoint& p,const FXSize& s){ x=p.x; y=p.y; w=s.w; h=s.h; return *this; }
00099 
00100   /// Set from corners
00101   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; }
00102 
00103   /// Set value from components
00104   FXRectangle& set(FXshort xx,FXshort yy,FXshort ww,FXshort hh){ x=xx; y=yy; w=ww; h=hh; return *this; }
00105 
00106   /// Union and intersection with rectangle
00107   FXRectangle& operator+=(const FXRectangle &r);
00108   FXRectangle& operator*=(const FXRectangle &r);
00109 
00110   /// Union and intersection between rectangles
00111   FXRectangle operator+(const FXRectangle& r) const;
00112   FXRectangle operator*(const FXRectangle& r) const;
00113 
00114   /// Save object to a stream
00115   friend FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00116 
00117   /// Load object from a stream
00118   friend FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00119   };
00120 
00121 
00122 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; }
00123 
00124 extern FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00125 extern FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00126 
00127 }
00128 
00129 #endif

Copyright © 1997-2005 Jeroen van der Zijp