//##############################################################################
//------------------------------------------------------------------------------
function Rect(a, b, c, d)
{
    if(typeof(a) != 'undefined' && typeof(b) != 'undefined' && typeof(c) != 'undefined' && typeof(d) != 'undefined')
    {
        this.left = a;
        this.top = b;
        this.width = c;
        this.height = d;
    }
    else
    {
        this.left = 0;
        this.top = 0;
        this.width = 0;
        this.height = 0;
    }
}

//------------------------------------------------------------------------------
Rect.prototype.size = function()
{
	return new Point(this.width, this.height);
}

//------------------------------------------------------------------------------
Rect.prototype.getArea = function() 
{
	if(this.width <= 0 || this.height <= 0)
		return 0;

	return this.width * this.height;
}

//------------------------------------------------------------------------------
Rect.prototype.getCenter = function() 
{ 
	return new Point(this.left + (this.width / 2), this.top + (this.height / 2)); 
}

//------------------------------------------------------------------------------
Rect.prototype.getIntersection = function(r)
{
	var left = Math.max(this.left, r.left);
	var top = Math.max(this.top, r.top);
	var right = Math.min(this.left + this.width, r.left + r.width);
	var bottom = Math.min(this.top + this.height, r.top + r.height);                                
	
	return new Rect(left, top, right - left, bottom - top);
}    

//------------------------------------------------------------------------------
Rect.prototype.intersects = function(r)
{
	return this.getIntersection(r).getArea() > 0;
}    

//------------------------------------------------------------------------------
Rect.prototype.union = function(r)
{
	var left = Math.min(this.left, r.left);
	var top = Math.min(this.top, r.top);
	var right = Math.max(this.left + this.width, r.left + r.width);
	var bottom = Math.max(this.top + this.height, r.top + r.height);                                
	
	this.left = left;
	this.top = top;
	this.width = right - left;
	this.height = bottom - top;
}

//------------------------------------------------------------------------------
Rect.prototype.containsPoint = function(p)
{
	return (p.x > this.left && p.x < this.left + this.width
			 && p.y > this.top && p.y < this.top + this.height);
}
