Skip to content
Snippets Groups Projects
Commit 4afb92ce authored by Alfred Burgess's avatar Alfred Burgess
Browse files

Added Rectangle class to maths

parent 0349c1af
Branches
No related merge requests found
......@@ -2,5 +2,6 @@
#define __MATHEMATICS_INCLUDE_H__
#include "maths/vec2.h"
#include "maths/rect.h"
#endif // __MATHEMATICS_INCLUDE_H__
#ifndef __MATHS_RECT_H__
#define __MATHS_RECT_H__
#include "maths/vec2.h"
class Rect
{
public:
Rect( float x, float y, float w, float h );
Rect( Vec2 pt, float w, float h );
~Rect(){}
bool isOverlap( Vec2 pt );
bool isOverlap( Rect rect );
bool overlap_horizontal( Rect rect );
bool overlap_vertical( Rect rect );
private:
Vec2 _pt;
float _halfWidth;
float _halfHeight;
};
#endif // __MATHS_RECT_H__
#include "maths/rect.h"
Rect::Rect( float x, float y, float w, float h )
: Rect( Vec2(x, y), w, h )
{}
Rect::Rect( Vec2 pt, float w, float h )
: _pt( pt ),
_halfWidth( w / 2 ),
_halfHeight( h / 2 )
{}
bool Rect::isOverlap( Vec2 pt )
{
return pt.x > ( pt.x - _halfWidth )
&& pt.x < ( pt.x + _halfWidth )
&& pt.y > ( pt.y - _halfHeight )
&& pt.y < ( pt.y + _halfHeight );
}
bool Rect::isOverlap( Rect rect )
{ return this->overlap_horizontal( rect ) && this->overlap_vertical( rect ); }
bool Rect::overlap_horizontal( Rect rect )
{
return abs( this->_pt.x - rect._pt.x )
< this->_halfWidth + rect._halfWidth;
}
bool Rect::overlap_vertical( Rect rect )
{
return abs( this->_pt.y - rect._pt.y )
< this->_halfHeight + rect._halfHeight;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment