rect.h

Go to the documentation of this file.
00001 /*
00002  *  rect.h - Rectangles.
00003  *
00004  *  Copyright (C) 1998-1999  Jeffrey S. Freedman
00005  *  Copyright (C) 2000-2001  The Exult Team
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #ifndef RECT_H
00023 #define RECT_H  1
00024 
00025 #ifdef WIN32
00026 #include <windows.h>
00027 #define Rectangle RECTX
00028 #endif
00029 
00030 /*
00031  *  A rectangle:
00032  */
00033 class Rectangle
00034   {
00035 public:         // Let's make it all public.
00036   int x, y;     // Position.
00037   int w, h;     // Dimensions.
00038   Rectangle(int xin, int yin, int win, int hin)
00039       : x(xin), y(yin), w(win), h(hin)
00040     {  }
00041   Rectangle() { }     // An uninitialized one.
00042           // Is this point in it?
00043   int has_point(int px, int py) const
00044     { return (px >= x && px < x + w && py >= y && py < y + h); }
00045           // Add another to this one to get
00046           //  a rect. that encloses both.
00047   Rectangle add(Rectangle& r2) const
00048     {
00049     int xend = x + w, yend = y + h;
00050     int xend2 = r2.x + r2.w, yend2 = r2.y + r2.h;
00051     Rectangle r;    // Return this.
00052     r.x = x < r2.x ? x : r2.x;
00053     r.y = y < r2.y ? y : r2.y;
00054     r.w = (xend > xend2 ? xend : xend2) - r.x;
00055     r.h = (yend > yend2 ? yend : yend2) - r.y;
00056     return (r);
00057     }
00058           // Intersect another with this.
00059   Rectangle intersect(Rectangle& r2) const
00060     {
00061     int xend = x + w, yend = y + h;
00062     int xend2 = r2.x + r2.w, yend2 = r2.y + r2.h;
00063     Rectangle r;    // Return this.
00064     r.x = x >= r2.x ? x : r2.x;
00065     r.y = y >= r2.y ? y : r2.y;
00066     r.w = (xend <= xend2 ? xend : xend2) - r.x;
00067     r.h = (yend <= yend2 ? yend : yend2) - r.y;
00068     return (r);
00069     }
00070           // Does it intersect another?
00071   int intersects(Rectangle r2) const
00072     {
00073     return (x >= r2.x + r2.w ? 0 : r2.x >= x + w ? 0 :
00074       y >= r2.y + r2.h ? 0 : r2.y >= y + h ? 0 : 1);
00075     }
00076   void shift(int deltax, int deltay)
00077     {
00078     x += deltax;
00079     y += deltay;
00080     }   
00081   Rectangle& enlarge(int delta) // Add delta in each dir.
00082     {
00083     x -= delta; y -= delta; w += 2*delta; h += 2*delta; 
00084     return *this;
00085     }
00086   int distance(int px, int py)  // Get distance from a point (max.
00087           //   dist. along x or y coord.)
00088     {
00089     int xdist = px <= x ? (x - px) : (px - x - w + 1);
00090     int ydist = py <= y ? (y - py) : (py - y - h + 1);
00091     int dist = xdist > ydist ? xdist : ydist;
00092     return dist < 0 ? 0 : dist;
00093     }
00094   };
00095 
00096 #endif

Generated on Mon Jul 9 14:42:49 2007 for ExultEngine by  doxygen 1.5.1