00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef HEADER_SUPERTUX_MATH_RECTF_HPP
00018 #define HEADER_SUPERTUX_MATH_RECTF_HPP
00019
00020 #include <assert.h>
00021
00022 #include "math/vector.hpp"
00023 #include "object/anchor_point.hpp"
00024
00025 class Sizef;
00026
00032 class Rectf
00033 {
00034 public:
00035 Rectf() :
00036 p1(),
00037 p2()
00038 { }
00039
00040 Rectf(const Vector& np1, const Vector& np2) :
00041 p1(np1), p2(np2)
00042 {
00043 }
00044
00045 Rectf(float x1, float y1, float x2, float y2) :
00046 p1(x1, y1), p2(x2, y2)
00047 {
00048 assert(p1.x <= p2.x && p1.y <= p2.y);
00049 }
00050
00051 Rectf(const Vector& p1_, const Sizef& size);
00052
00053 float get_left() const
00054 { return p1.x; }
00055
00056 float get_right() const
00057 { return p2.x; }
00058
00059 float get_top() const
00060 { return p1.y; }
00061
00062 float get_bottom() const
00063 { return p2.y; }
00064
00065 float get_width() const
00066 { return p2.x - p1.x; }
00067
00068 float get_height() const
00069 { return p2.y - p1.y; }
00070
00071 Vector get_middle() const
00072 { return Vector((p1.x+p2.x)/2, (p1.y+p2.y)/2); }
00073
00074 void set_pos(const Vector& v)
00075 {
00076 move(v-p1);
00077 }
00078
00079 void set_height(float height)
00080 {
00081 p2.y = p1.y + height;
00082 }
00083 void set_width(float width)
00084 {
00085 p2.x = p1.x + width;
00086 }
00087 void set_size(float width, float height)
00088 {
00089 set_width(width);
00090 set_height(height);
00091 }
00092 Vector get_size()
00093 {
00094 return Vector(get_width(), get_height());
00095 }
00096
00097 void move(const Vector& v)
00098 {
00099 p1 += v;
00100 p2 += v;
00101 }
00102
00103 bool contains(const Vector& v) const
00104 {
00105 return v.x >= p1.x && v.y >= p1.y && v.x < p2.x && v.y < p2.y;
00106 }
00107 bool contains(const Rectf& other) const
00108 {
00109 if(p1.x >= other.p2.x || other.p1.x >= p2.x)
00110 return false;
00111 if(p1.y >= other.p2.y || other.p1.y >= p2.y)
00112 return false;
00113
00114 return true;
00115 }
00116
00117 float distance (const Vector& other, AnchorPoint ap = ANCHOR_MIDDLE) const
00118 {
00119 Vector v = get_anchor_pos (*this, ap);
00120 return ((v - other).norm ());
00121 }
00122
00123 float distance (const Rectf& other, AnchorPoint ap = ANCHOR_MIDDLE) const
00124 {
00125 Vector v1 = get_anchor_pos (*this, ap);
00126 Vector v2 = get_anchor_pos (other, ap);
00127
00128 return ((v1 - v2).norm ());
00129 }
00130
00131
00132
00133
00135 Vector p1;
00137 Vector p2;
00138 };
00139
00140 #endif
00141
00142