1 #ifndef __RECTANGLE_H__
2 #define __RECTANGLE_H__
10 /** This class represents a rectangle.
11 * (Implementation Note) We're using upper left and lower right point instead of
12 * upper left and width/height here, because that makes the collision dectection
13 * a little bit efficienter.
21 Rectangle(const Vector& np1, const Vector& np2)
26 Rectangle(float x1, float y1, float x2, float y2)
27 : p1(x1, y1), p2(x2, y2)
29 assert(p1.x <= p2.x && p1.y <= p2.y);
32 float get_width() const
33 { return p2.x - p1.x; }
35 float get_height() const
36 { return p2.y - p1.y; }
38 Vector get_middle() const
39 { return Vector((p1.x+p2.x)/2, (p1.y+p2.y)/2); }
41 void set_pos(const Vector& v)
46 void set_height(float height)
50 void set_width(float width)
54 void set_size(float width, float height)
60 void move(const Vector& v)
66 bool inside(const Vector& v) const
68 return v.x >= p1.x && v.y >= p1.y && v.x < p2.x && v.y < p2.y;
70 bool inside(const Rectangle& other) const
72 if(p1.x >= other.p2.x || other.p1.x >= p2.x)
74 if(p1.y >= other.p2.y || other.p1.y >= p2.y)
80 // leave these 2 public to safe the headaches of set/get functions for such