#import "rect.h" #import using namespace wgd; Rect::Rect (float x1, float y1, float x2, float y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} Rect::Rect (const Vector2D &p1, const Vector2D &p2) : x1(p1.x), y1(p1.y), x2(p2.x), y2(p2.y) {} Vector2D Rect::centre () const { return Vector2D((x1 + x2) / 2, (y1 + y2) / 2); } bool Rect::intersects (const Rect &r) const { return ! (r.x2 < x1 || r.x1 > x2 || r.y2 < y1 || r.y1 > y2); } bool Rect::contains (const Vector2D &p) const { return (p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2); } void Rect::translate (const Vector2D &t) { x1 += t.x; x2 += t.x; y1 += t.y; y2 += t.y; } void Rect::translate (float dx, float dy) { x1 += dx; x2 += dx; y1 += dy; y2 += dy; } void Rect::add (float x, float y) { if (x < x1) { x1 = x; } else if (x > x2) { x2 = x; } if (y < y1) { y1 = y; } else if (y > y2) { y2 = y; } } void Rect::add (const Rect &r) { add(r.x1, r.y1); add(r.x2, r.y2); } void Rect::add (const Vector2D &p) { add(p.x, p.y); }