30 #include <math/vector2d.h>
33 #include <core/optional.h>
48 typedef typename Vec::coord_type coord_type;
49 typedef typename Vec::extended_type ecoord_type;
50 typedef std::numeric_limits<coord_type> coord_limits;
54 BOX2(
const Vec& aPos,
const Vec& aSize ) :
61 #ifdef WX_COMPATIBILITY
62 BOX2(
const wxRect& aRect ) :
64 m_Pos( aRect.GetPosition() ),
65 m_Size( aRect.GetSize() )
73 m_Pos.x = m_Pos.y = coord_limits::lowest() / 2 + coord_limits::epsilon();
74 m_Size.x = m_Size.y = coord_limits::max() - coord_limits::epsilon();
79 return Vec( m_Pos.x + ( m_Size.x / 2 ),
80 m_Pos.y + ( m_Size.y / 2 ) );
88 template <
class Container>
89 void Compute(
const Container& aPointList )
93 typename Container::const_iterator i;
95 if( !aPointList.size() )
98 vmin = vmax = aPointList[0];
100 for( i = aPointList.begin(); i != aPointList.end(); ++i )
103 vmin.x = std::min( vmin.x, p.x );
104 vmin.y = std::min( vmin.y, p.y );
105 vmax.x = std::max( vmax.x, p.x );
106 vmax.y = std::max( vmax.y, p.y );
110 SetSize( vmax - vmin );
118 void Move(
const Vec& aMoveVector )
120 m_Pos += aMoveVector;
131 m_Size.y = -m_Size.y;
137 m_Size.x = -m_Size.x;
151 Vec rel_pos = aPoint - m_Pos;
166 return ( rel_pos.x >= 0 ) && ( rel_pos.y >= 0 ) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
187 const Vec& GetSize()
const {
return m_Size; }
188 coord_type GetX()
const {
return m_Pos.x; }
189 coord_type GetY()
const {
return m_Pos.y; }
191 const Vec& GetOrigin()
const {
return m_Pos; }
192 const Vec& GetPosition()
const {
return m_Pos; }
193 const Vec GetEnd()
const {
return Vec( GetRight(), GetBottom() ); }
195 coord_type GetWidth()
const {
return m_Size.x; }
196 coord_type GetHeight()
const {
return m_Size.y; }
197 coord_type GetRight()
const {
return m_Pos.x + m_Size.x; }
198 coord_type GetBottom()
const {
return m_Pos.y + m_Size.y; }
201 coord_type GetLeft()
const {
return GetX(); }
202 coord_type GetTop()
const {
return GetY(); }
203 void MoveTopTo( coord_type aTop ) { m_Pos.y = aTop; }
204 void MoveBottomTo( coord_type aBottom ) { m_Size.y = aBottom - m_Pos.y; }
205 void MoveLeftTo( coord_type aLeft ) { m_Pos.x = aLeft; }
206 void MoveRightTo( coord_type aRight ) { m_Size.x = aRight - m_Pos.x; }
208 void SetOrigin(
const Vec& pos ) { m_Pos = pos; }
209 void SetOrigin( coord_type x, coord_type y ) { m_Pos.x = x; m_Pos.y = y; }
210 void SetSize(
const Vec& size ) { m_Size = size; }
211 void SetSize( coord_type w, coord_type h ) { m_Size.x = w; m_Size.y = h; }
212 void Offset( coord_type dx, coord_type dy ) { m_Pos.x += dx; m_Pos.y += dy; }
213 void Offset(
const Vec& offset )
215 m_Pos.x += offset.x; m_Pos.y +=
219 void SetX( coord_type val ) { m_Pos.x = val; }
220 void SetY( coord_type val ) { m_Pos.y = val; }
221 void SetWidth( coord_type val ) { m_Size.x = val; }
222 void SetHeight( coord_type val ) { m_Size.y = val; }
223 void SetEnd( coord_type x, coord_type y ) { SetEnd( Vec( x, y ) ); }
224 void SetEnd(
const Vec& pos )
226 m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
245 int left = std::max( me.m_Pos.x, rect.m_Pos.x );
247 int right = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
249 int top = std::max( me.m_Pos.y, aRect.m_Pos.y );
251 int bottom = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
254 if( left <= right && top <= bottom )
273 Vec topLeft, bottomRight;
275 topLeft.x = std::max( me.m_Pos.x, rect.m_Pos.x );
276 bottomRight.x = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
277 topLeft.y = std::max( me.m_Pos.y, rect.m_Pos.y );
278 bottomRight.y = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
280 if ( topLeft.x < bottomRight.x && topLeft.y < bottomRight.y )
281 return BOX2<Vec>( topLeft, bottomRight - topLeft );
283 return BOX2<Vec>( Vec( 0, 0 ), Vec( 0, 0 ) );
286 const std::string Format()
const
288 std::stringstream ss;
290 ss <<
"( box corner " << m_Pos.Format() <<
" w " << m_Size.x <<
" h " << m_Size.y <<
" )";
304 if( m_Size.x < -2 * dx )
307 m_Pos.x += m_Size.x / 2;
319 if( m_Size.x > -2 * dx )
322 m_Pos.x -= m_Size.x / 2;
335 if( m_Size.y < -2 * dy )
338 m_Pos.y += m_Size.y / 2;
350 if( m_Size.y > 2 * dy )
353 m_Pos.y -= m_Size.y / 2;
390 Vec rect_end = rect.GetEnd();
393 m_Pos.x = std::min( m_Pos.x, rect.m_Pos.x );
394 m_Pos.y = std::min( m_Pos.y, rect.m_Pos.y );
395 end.x = std::max( end.x, rect_end.x );
396 end.y = std::max( end.y, rect_end.y );
412 m_Pos.x = std::min( m_Pos.x, aPoint.x );
413 m_Pos.y = std::min( m_Pos.y, aPoint.y );
414 end.x = std::max( end.x, aPoint.x );
415 end.y = std::max( end.y, aPoint.y );
427 return (ecoord_type) GetWidth() * (ecoord_type) GetHeight();
437 return m_Size.EuclideanNorm();
440 ecoord_type SquaredDistance(
const Vec& aP )
const
442 ecoord_type x2 = m_Pos.x + m_Size.x;
443 ecoord_type y2 = m_Pos.y + m_Size.y;
444 ecoord_type xdiff = std::max( aP.x < m_Pos.x ? m_Pos.x - aP.x : m_Pos.x - x2, (ecoord_type) 0 );
445 ecoord_type ydiff = std::max( aP.y < m_Pos.y ? m_Pos.y - aP.y : m_Pos.y - y2, (ecoord_type) 0 );
446 return xdiff * xdiff + ydiff * ydiff;
449 ecoord_type Distance(
const Vec& aP )
const
451 return sqrt( SquaredDistance( aP ) );
464 if( aBox.m_Pos.x + aBox.m_Size.x < m_Pos.x )
466 ecoord_type d = aBox.m_Pos.x + aBox.m_Size.x - m_Pos.x;
469 else if( aBox.m_Pos.x > m_Pos.x + m_Size.x )
471 ecoord_type d = aBox.m_Pos.x - m_Size.x - m_Pos.x;
475 if( aBox.m_Pos.y + aBox.m_Size.y < m_Pos.y )
477 ecoord_type d = aBox.m_Pos.y + aBox.m_Size.y - m_Pos.y;
480 else if( aBox.m_Pos.y > m_Pos.y + m_Size.y )
482 ecoord_type d = aBox.m_Pos.y - m_Size.y - m_Pos.y;
497 return sqrt( SquaredDistance( aBox ) );
505 typedef OPT<BOX2I> OPT_BOX2I;