src/math/rectf.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   // leave these two public to save the headaches of set/get functions for such
00132   // simple things :)
00133 
00135   Vector p1;
00137   Vector p2;
00138 };
00139 
00140 #endif
00141 
00142 /* EOF */

Generated on Mon Jun 9 03:38:18 2014 for SuperTux by  doxygen 1.5.1