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

plrect.h

00001 /*
00002 /--------------------------------------------------------------------
00003 |
00004 |      $Id: plrect.h,v 1.10 2004/02/21 18:37:08 uzadow Exp $
00005 |
00006 |      Copyright (c) 1996-2002 Ulrich von Zadow
00007 |
00008 \--------------------------------------------------------------------
00009 */
00010 
00011 #ifndef INCL_PLRECT
00012 #define INCL_PLRECT
00013 
00014 #include "plpoint.h"
00015 
00016 //! Simple rectangle class.
00017 //! Contains all points from tl up to but not including br.
00018 class PLRect
00019 {
00020 public:
00021   //!
00022   PLPoint tl;
00023   //!
00024   PLPoint br;
00025 
00026   //!
00027   PLRect
00028   ();
00029 
00030   //!
00031   PLRect
00032   ( int left,
00033     int top,
00034     int right,
00035     int bottom
00036   );
00037 
00038   //!
00039   PLRect
00040   ( const PLPoint& TL,
00041     const PLPoint& BR
00042   );
00043 
00044   //!
00045   bool operator ==
00046   ( const PLRect & rect
00047   ) const;
00048 
00049   //!
00050   bool operator !=
00051   ( const PLRect & rect
00052   ) const;
00053 
00054   //!
00055   int Width 
00056   () const;
00057 
00058   //!
00059   int Height
00060   () const;
00061 
00062   //!
00063   void SetWidth
00064   (int width
00065   );
00066  
00067   //!
00068   void SetHeight
00069   (int height
00070   );
00071 
00072   //!
00073   bool Contains 
00074   ( const PLPoint& pt
00075   ) const;
00076 
00077   //!
00078   bool Contains 
00079   ( const PLRect& rect
00080   ) const;
00081 
00082   //!
00083   bool Intersects
00084   ( const PLRect& rect
00085   ) const;
00086 
00087   //!
00088   void Expand
00089   ( const PLRect& rect
00090   );
00091 
00092   //!
00093   void Intersect
00094   ( const PLRect& rect
00095   );
00096 };
00097 
00098 inline PLRect::PLRect
00099 ()
00100 {}
00101 
00102 inline PLRect::PLRect
00103   ( const PLPoint& TL,
00104     const PLPoint& BR
00105   ): tl(TL), br(BR)
00106 {}
00107 
00108 inline PLRect::PLRect
00109   ( int left,
00110     int top,
00111     int right,
00112     int bottom
00113   ) : tl(left, top), 
00114       br (right, bottom)
00115 {}
00116 
00117 inline bool PLRect::operator ==
00118 ( const PLRect & rect
00119 ) const
00120 {
00121   return (tl == rect.tl && br == rect.br);
00122 }
00123 
00124 inline bool PLRect::operator !=
00125 ( const PLRect & rect
00126 ) const
00127 {
00128   return !(rect==*this);
00129 }
00130 
00131 inline int PLRect::Width 
00132 () const
00133 {
00134   return br.x-tl.x;
00135 }
00136 
00137 inline int PLRect::Height
00138 () const
00139 {
00140   return br.y-tl.y;
00141 }
00142 
00143 inline void PLRect::SetWidth
00144 (int width
00145 )
00146 {
00147     br.x = tl.x+width;
00148 }
00149  
00150 inline void PLRect::SetHeight
00151 (int height
00152 )
00153 {
00154     br.y = tl.y+height;
00155 }
00156 
00157 inline bool PLRect::Contains 
00158 ( const PLPoint& pt
00159 ) const
00160 {
00161     return (pt.x >= tl.x && pt.x < br.x &&
00162         pt.y >= tl.y && pt.y < br.y);
00163 }
00164 
00165 inline bool PLRect::Contains 
00166 ( const PLRect& rect
00167 ) const
00168 {
00169     PLPoint brpt (rect.br.x-1, rect.br.y-1);
00170     return Contains(rect.tl) && Contains(brpt);
00171 }
00172 
00173 inline bool PLRect::Intersects
00174   ( const PLRect& rect
00175   ) const
00176 {   
00177     if (rect.br.x <= tl.x || rect.tl.x >= br.x ||
00178         rect.br.y <= tl.y || rect.tl.y >= br.y)
00179       return false;
00180     else
00181       return true;
00182 }
00183 
00184 #ifndef  min
00185 #define min(a, b)       ((a) < (b) ? (a) : (b))
00186 #endif
00187 
00188 #ifndef  max
00189 #define max(a, b)       ((a) < (b) ? (b) : (a))
00190 #endif
00191 
00192 inline void PLRect::Expand
00193   ( const PLRect& rect
00194   )
00195 {
00196     tl.x = min(tl.x, rect.tl.x);
00197     tl.y = min(tl.y, rect.tl.y);
00198     br.x = max(br.x, rect.br.x);
00199     br.y = max(br.y, rect.br.y);
00200 }
00201 
00202 inline void PLRect::Intersect
00203   ( const PLRect& rect
00204   )
00205 {
00206     tl.x = max(tl.x, rect.tl.x);
00207     tl.y = max(tl.y, rect.tl.y);
00208     br.x = min(br.x, rect.br.x);
00209     br.y = min(br.y, rect.br.y);
00210 }
00211 
00212 #undef min
00213 #undef max
00214 
00215 #endif
00216 
00217 /*
00218 /--------------------------------------------------------------------
00219 |
00220 |      $Log: plrect.h,v $
00221 |      Revision 1.10  2004/02/21 18:37:08  uzadow
00222 |      Fixed rect.Intersects() again.
00223 |
00224 |      Revision 1.8  2004/02/21 16:48:24  uzadow
00225 |      Fixed rect.Intersects()
00226 |
00227 |      Revision 1.7  2003/10/15 21:24:04  uzadow
00228 |      *** empty log message ***
00229 |
00230 |      Revision 1.6  2003/08/17 18:04:38  uzadow
00231 |      Added PLRect::Intersect()
00232 |
00233 |      Revision 1.5  2003/08/17 15:04:30  uzadow
00234 |      Added PLRect::Intersects() and Expand()
00235 |
00236 |      Revision 1.4  2003/03/19 14:33:14  uzadow
00237 |      Added Rect.Contains
00238 |
00239 |      Revision 1.3  2002/03/31 13:36:42  uzadow
00240 |      Updated copyright.
00241 |
00242 |      Revision 1.2  2001/09/28 19:50:56  uzadow
00243 |      Added some 24 bpp stuff & other minor features.
00244 |
00245 |      Revision 1.1  2001/09/24 14:24:52  uzadow
00246 |      Added PLRect.
00247 |
00248 |      Revision 1.4  2001/09/16 19:03:22  uzadow
00249 |      Added global name prefix PL, changed most filenames.
00250 |
00251 |      Revision 1.3  2000/11/21 20:20:36  uzadow
00252 |      Changed bool to bool.
00253 |
00254 |      Revision 1.2  2000/01/10 23:52:59  Ulrich von Zadow
00255 |      Changed formatting & removed tabs.
00256 |
00257 |      Revision 1.1  1999/12/09 16:35:58  Ulrich von Zadow
00258 |      Added PLRect.
00259 |
00260 |
00261 \--------------------------------------------------------------------
00262 */

Generated on Sun Jun 6 13:42:22 2004 for paintlib by doxygen 1.3.2