| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgeom/csrect.h

Go to the documentation of this file.

    00001 /*
    00002     Crystal Space Engine: rectangle class interface
    00003     Copyright (C) 2001 by Jorrit Tyberghein
    00004     Copyright (C) 1998,1999 by Andrew Zabolotny <bit@eltech.ru>
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License as published by the Free Software Foundation; either
    00009     version 2 of the License, or (at your option) any later version.
    00010
    00011     This library is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014     Library General Public License for more details.
    00015
    00016     You should have received a copy of the GNU Library General Public
    00017     License along with this library; if not, write to the Free
    00018     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00019 */
    00020
    00021 #ifndef __CS_RECT_H__
    00022 #define __CS_RECT_H__
    00023
    00031 #include "csextern.h"
    00032
    00053 class CS_CRYSTALSPACE_EXPORT csRect
    00054 {
    00055 public:
    00057   int xmin, ymin, xmax, ymax;
    00058
    00060   csRect ();
    00061
    00063   csRect (int ixmin, int iymin, int ixmax, int iymax);
    00064
    00066   csRect (const csRect &copy);
    00067
    00069   virtual ~csRect ();
    00070
    00072   void Intersect (int ixmin, int iymin, int ixmax, int iymax);
    00073
    00075   inline void Intersect (const csRect &other)
    00076   { Intersect (other.xmin, other.ymin, other.xmax, other.ymax); }
    00077
    00079   bool Intersects (const csRect &target) const;
    00080
    00085   void Union (int ixmin, int iymin, int ixmax, int iymax);
    00086
    00091   inline void Union (const csRect &other)
    00092   { Union (other.xmin, other.ymin, other.xmax, other.ymax); }
    00093
    00099   void Exclude (int ixmin, int iymin, int ixmax, int iymax);
    00100
    00102   inline void Exclude (const csRect &other)
    00103   { Exclude (other.xmin, other.ymin, other.xmax, other.ymax); }
    00104
    00109   void Subtract (const csRect &rect);
    00110
    00112   inline bool IsEmpty () const
    00113   { return (xmin >= xmax) || (ymin >= ymax); }
    00114
    00116   inline void MakeEmpty ()
    00117   { xmin = xmax = 0; }
    00118
    00120   inline void Set (int ixmin, int iymin, int ixmax, int iymax)
    00121   {
    00122     xmin = ixmin; xmax = ixmax;
    00123     ymin = iymin; ymax = iymax;
    00124   }
    00125
    00127   inline void Set (const csRect &target)
    00128   {
    00129     xmin = target.xmin; xmax = target.xmax;
    00130     ymin = target.ymin; ymax = target.ymax;
    00131   }
    00132
    00134   inline void SetPos (int x, int y)
    00135   {
    00136         int w = Width(), h=Height();
    00137
    00138         xmin = x; ymin = y;
    00139         xmax = xmin+w;
    00140         ymax = ymin+h;
    00141    }
    00142
    00144   inline void SetSize (int w, int h)
    00145   { xmax = xmin + w; ymax = ymin + h; }
    00146
    00148   inline void Move (int dX, int dY)
    00149   { xmin += dX; xmax += dX; ymin += dY; ymax += dY; }
    00150
    00152   inline int Width () const { return xmax - xmin; }
    00153
    00155   inline int Height () const { return ymax - ymin; }
    00156
    00158   inline bool Contains (int x, int y) const
    00159   { return (x >= xmin) && (x < xmax) && (y >= ymin) && (y < ymax); }
    00160
    00162   inline bool ContainsRel (int x, int y) const
    00163   { return (x >= 0) && (x < Width ()) && (y >= 0) && (y < Height ()); }
    00164
    00166   inline bool Equal (int ixmin, int iymin, int ixmax, int iymax) const
    00167   { return (xmin == ixmin) && (ymin == iymin) &&
    00168            (xmax == ixmax) && (ymax == iymax); }
    00170   inline bool Equal (const csRect &other) const
    00171   { return Equal (other.xmin, other.ymin, other.xmax, other.ymax); }
    00172
    00174   inline void Normalize ()
    00175   {
    00176     if (xmin > xmax) { int tmp = xmin; xmin = xmax; xmax = tmp; }
    00177     if (ymin > ymax) { int tmp = ymin; ymin = ymax; ymax = tmp; }
    00178   }
    00179
    00181   inline int Area () const
    00182   {
    00183     if (IsEmpty ())
    00184       return 0;
    00185     else
    00186       return Width () * Height ();
    00187   }
    00188
    00193   void AddAdjacent (const csRect &rect);
    00194
    00196   CS_DEPRECATED_METHOD_MSG("Misspelling; use AddAdjacent() instead")
    00197   void AddAdjanced (const csRect &rect)
    00198   { AddAdjacent (rect); }
    00199
    00201   inline bool operator == (const csRect& rect) const
    00202   {
    00203     return Equal (rect);
    00204   }
    00205
    00207   inline bool operator != (const csRect &rect) const
    00208   {
    00209     return !Equal (rect);
    00210   }
    00211
    00213   inline void Extend (int x, int y)
    00214   {
    00215     if (xmin > x) xmin = x; if (xmax < x+1) xmax = x+1;
    00216     if (ymin > y) ymin = y; if (ymax < y+1) ymax = y+1;
    00217   }
    00218
    00220   void Join (const csRect &rect);
    00221
    00223   void Outset(int n);
    00224
    00226   void Inset(int n);
    00227
    00235   bool ClipLineGeneral (int& x1, int& y1, int& x2, int& y2);
    00236
    00244   bool ClipLine (int& x1, int& y1, int& x2, int& y2);
    00245
    00253   bool ClipLineSafe (int& x1, int& y1, int& x2, int& y2);
    00254 };
    00255
    00258 #endif // __CS_RECT_H__
    00259