// Start a bbox with a point
bbox(const pair& z)
: empty(false), left(z.getx()), bottom(z.gety()),
right(z.getx()), top(z.gety())
{
}
bool nonempty() const {
return !empty;
}
// Add a point to a bbox
bbox add(const pair& z)
{
double x = z.getx(), y = z.gety();
if (empty) {
left = right = x;
top = bottom = y;
empty = false;
}
else {
if (x < left)
left = x;
else if (x > right)
right = x;
if (y < bottom)
bottom = y;
else if (y > top)
top = y;
}
return *this;
}
// Add a point to a nonempty bbox
void addnonempty(const pair& z)
{
double x = z.getx(), y = z.gety();
if (x < left)
left = x;
else if (x > right)
right = x;
if (y < bottom)
bottom = y;
else if (y > top)
top = y;
}
// Add a point to a nonempty bbox, updating bounding times
void addnonempty(const pair& z, bbox& times, double t)
{
double x = z.getx(), y = z.gety();
if (x < left) {
left = x;
times.left = t;
}
else if (x > right) {
right = x;
times.right = t;
}
if (y < bottom) {
bottom = y;
times.bottom = t;
}
else if (y > top) {
top = y;
times.top = t;
}
}
// Add one bounding box to another
void add(const bbox& b)
{
if (this->empty)
*this = b;
else if (!b.empty) {
left = min(left, b.left);
right = max(right, b.right);
bottom = min(bottom, b.bottom);
top = max(top, b.top);
}
}
bbox operator+= (const bbox& b)
{
add(b);
return *this;
}
void clip(const bbox& b) {
if(this->empty) return;
left = max(left, b.left);
right = min(right, b.right);
bottom = max(bottom, b.bottom);
top = min(top, b.top);
if(left > right || bottom > top)
*this=bbox();
}