00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00032
00033 class Rectangle
00034 {
00035 public:
00036 int x, y;
00037 int w, h;
00038 Rectangle(int xin, int yin, int win, int hin)
00039 : x(xin), y(yin), w(win), h(hin)
00040 { }
00041 Rectangle() { }
00042
00043 int has_point(int px, int py) const
00044 { return (px >= x && px < x + w && py >= y && py < y + h); }
00045
00046
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;
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
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;
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
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)
00082 {
00083 x -= delta; y -= delta; w += 2*delta; h += 2*delta;
00084 return *this;
00085 }
00086 int distance(int px, int py)
00087
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