Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 108 lines (78 sloc) 1.56 KB
#ifndef com_sleepless_rect_cpp
#define com_sleepless_rect_cpp
#include "math.cpp"
struct Rect
{
int x, y, w, h;
Rect()
{
reshape(0, 0, 0, 0);
}
Rect(int _x, int _y, int _w, int _h)
{
reshape(_x, _y, _w, _h);
}
Rect(Rect *r)
{
reshape(r);
}
void add(int _x, int _y, int _w, int _h)
{
int x1 = Math::lesser(x, _x);
int x2 = Math::greater(x + w, _x + _w);
int y1 = Math::lesser(y, _y);
int y2 = Math::greater(y + h, _y + _h);
x = x1;
y = y1;
w = x2 - x1;
h = y2 - y1;
}
// Make this rect big enough to enclose both itself and the rect passed in.
void add(Rect *r)
{
add(r->x, r->y, r->w, r->h);
}
// Reshape this rect to become the intersection of itself and the rect passed in.
void intersect(Rect *r)
{
int x1 = Math::greater(x, r->x);
int x2 = Math::lesser(x + w, r->x + r->w);
int y1 = Math::greater(y, r->y);
int y2 = Math::lesser(y + h, r->y + r->h);
reshape(x1, y1, x2 - x1, y2 - y1);
}
void intersect(int _x, int _y, int _w, int _h)
{
Rect r(_x, _y, _w, _h);
intersect(&r);
}
void reshape(int _x, int _y, int _w, int _h)
{
x = _x;
y = _y;
w = _w;
h = _h;
}
void reshape(Rect *r)
{
reshape(r->x, r->y, r->w, r->h);
}
bool contains(int px, int py)
{
if(px < x)
return false;
if(py < y)
return false;
if(px >= (x + w))
return false;
if(py >= (y + h))
return false;
return true;
}
void translate(int tx, int ty)
{
x += tx;
y += ty;
}
};
#endif // com_sleepless_rect_cpp