92 lines
1.7 KiB
C++
92 lines
1.7 KiB
C++
//------------------------------------------------------------------------------
|
|
// Author: Gero Mueller <gero.mueller@cloo.de>
|
|
// Copyright: (c) 2006 Gero Mueller
|
|
// License: MIT License
|
|
//------------------------------------------------------------------------------
|
|
|
|
#ifndef BLUECORE_RECTANGLE_H
|
|
#define BLUECORE_RECTANGLE_H
|
|
|
|
#include "Point.h"
|
|
|
|
namespace BlueCore
|
|
{
|
|
template<typename type>
|
|
struct Rectangle2DTemplate
|
|
{
|
|
type _x, _y, _width, _height;
|
|
|
|
Rectangle2DTemplate() :
|
|
_x(0),
|
|
_y(0),
|
|
_width(0),
|
|
_height(0)
|
|
{
|
|
}
|
|
|
|
Rectangle2DTemplate( int x, int y, int width, int height ) :
|
|
_x(x),
|
|
_y(y),
|
|
_width(width),
|
|
_height(height)
|
|
{
|
|
}
|
|
|
|
Rectangle2DTemplate( const Point2DTemplate<type> &topLeft, const Point2DTemplate<type> &bottomRight ) :
|
|
_x(topLeft._x),
|
|
_y(topLeft._y),
|
|
_width(bottomRight._x - topLeft._x),
|
|
_height(bottomRight._y - topLeft._y)
|
|
{
|
|
}
|
|
|
|
Rectangle2DTemplate &operator=(const Rectangle2DTemplate<type> &rect)
|
|
{
|
|
_x = rect._x;
|
|
_y = rect._y;
|
|
_width = rect._width;
|
|
_height = rect._height;
|
|
|
|
return *this;
|
|
}
|
|
|
|
bool overlaps( const Rectangle2DTemplate<type> &rect )
|
|
{
|
|
if( (rect._x + rect._width) < _x )
|
|
return false;
|
|
|
|
if( rect._x > (_x + _width) )
|
|
return false;
|
|
|
|
if( rect._y < (_y - _height) )
|
|
return false;
|
|
|
|
if( (rect._y - rect._height) > _y )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool contains( const Point2DTemplate<type> &point ) const
|
|
{
|
|
if( point._x < _x )
|
|
return false;
|
|
|
|
if( point._y < _y )
|
|
return false;
|
|
|
|
if( point._x > (_x + _width) )
|
|
return false;
|
|
|
|
if( point._y > (_y + _height) )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
typedef Rectangle2DTemplate<int> Rectangle2D;
|
|
}
|
|
|
|
#endif
|