00001 #ifndef _GEOM_RECTANGLE_HPP_
00002 #define _GEOM_RECTANGLE_HPP_
00003
00004 #include <jmath/jblas.hpp>
00005
00006 namespace jafar {
00007 namespace geom {
00008
00013 template <typename _T_>
00014 class Rectangle {
00015 public:
00016 typedef boost::numeric::ublas::bounded_vector< _T_, 2> vec2;
00017 public:
00018 inline Rectangle() {}
00019 inline Rectangle(const _T_ x, const _T_ y, const _T_ width, const _T_ height) {
00020 m_topLeft(0) = x; m_topLeft(1) = y;
00021 m_bottomRight(0) = x + width - 1; m_bottomRight(1) = y + height - 1;
00022 }
00023 inline Rectangle(const typename Rectangle::vec2& topLeft, const typename Rectangle::vec2& bottomRight) : m_topLeft(topLeft), m_bottomRight(bottomRight) {
00024
00025 }
00027 inline _T_ left() const { return m_topLeft(0); }
00029 inline void setLeft(_T_ v) { m_topLeft(0) = v; }
00031 inline void moveLeft(_T_ v) { _T_ diff = v - left(); setLeft(v); setRight( right() + v); }
00032
00034 inline _T_ top() const { return m_topLeft(1); }
00036 inline void setTop(_T_ v) { m_topLeft(1) = v; }
00038 inline void moveTop(_T_ v) { _T_ diff = v - top(); setTop(v); setBottom( bottom() + v); }
00039
00041 inline _T_ right() const { return m_bottomRight(0); }
00043 inline void setRight(_T_ v) { m_bottomRight(0) =v; }
00045 inline void moveRight(_T_ v) { _T_ diff = v - right(); setRight(v); setLeft( left() + v); }
00046
00048 inline _T_ bottom() const { return m_bottomRight(1); }
00050 inline void setBottom(_T_ v) { m_bottomRight(1) = v; }
00052 inline void moveBottom(_T_ v) { _T_ diff = v - bottom(); setBottom(v); setTop( top() + v); }
00053
00055 inline bool contains( _T_ x, _T_ y ) const
00056 {
00057 return ( x >= left() && x <= right() && y >= top() && y <= bottom() );
00058 }
00059
00061 inline bool contains( typename Rectangle::vec2 v ) const
00062 {
00063 return ( v(0) >= left() && v(0) <= right() && v(1) >= top() && v(1) <= bottom() );
00064 }
00066 inline bool strictlyContains( typename Rectangle::vec2 v ) const
00067 {
00068 return ( v(0) > left() && v(0) < right() && v(1) > top() && v(1) < bottom() );
00069 }
00070
00072 inline void extend( const typename Rectangle::vec2& v )
00073 {
00074 if( v(0) < left() ) setLeft(v(0));
00075 else if( v(0) > right() ) setRight(v(0));
00076 if( v(1) < top() ) setTop(v(1));
00077 else if( v(1) > bottom() ) setBottom(v(1));
00078 }
00079 inline void extend( const _T_ u, const _T_ v )
00080 {
00081 if( u < left() ) setLeft(u);
00082 else if( u > right() ) setRight(u);
00083 if( v < top() ) setTop(v);
00084 else if( v > bottom() ) setBottom(v);
00085 }
00086
00088 void addCoord(_T_ x1, _T_ y1, _T_ x2, _T_ y2)
00089 {
00090 setLeft( left() + x1); setRight( right() + x2 );
00091 setTop( top() + y1); setBottom( bottom() + y2 );
00092 }
00093 private:
00094 vec2 m_topLeft, m_bottomRight;
00095
00096 };
00097
00098 typedef Rectangle<double> RectangleDouble;
00099
00100 }
00101
00102 }
00103
00104 #endif