/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#include "KeosRectangle.h"

namespace Keos
{

    //=======================================================================
    // CRectangle implementation
    //=======================================================================
    
    //-----------------------------------------------------------------------
    CRectangle::CRectangle(const TVector2I& Start, const TVector2I& Size) :
    Origin(Start),
    End   (Start + Size)
    {

    }

    //-----------------------------------------------------------------------
    CRectangle::CRectangle(int nLeft, int nTop, int nWidth, int nHeight) :
    Origin(nLeft, nTop),
    End   (nLeft + nWidth, nTop + nHeight)
    {

    }

    //-----------------------------------------------------------------------
    void CRectangle::Set(int nLeft, int nTop, int nWidth, int nHeight)
    {
        Origin.Set(nLeft, nTop);
        End.Set(nLeft + nWidth, nTop + nHeight);
    }

    //-----------------------------------------------------------------------
    int CRectangle::Left() const
    {
        return Origin.x;
    }

    //-----------------------------------------------------------------------
    int CRectangle::Right() const
    {
        return End.x;
    }

    //-----------------------------------------------------------------------
    int CRectangle::Top() const
    {
        return Origin.y;
    }

    //-----------------------------------------------------------------------
    int CRectangle::Bottom() const
    {
        return End.y;
    }

    //-----------------------------------------------------------------------
    int CRectangle::Width() const
    {
        return End.x - Origin.x;
    }

    //-----------------------------------------------------------------------
    int CRectangle::Height() const
    {
        return End.y - Origin.y;
    }

    //-----------------------------------------------------------------------
    TVector2I CRectangle::Size() const
    {
        return End - Origin;
    }

    //-----------------------------------------------------------------------
    TIntersection CRectangle::Intersects(const TVector2I& Point) const
    {
        if ((Point.x >= Origin.x) && (Point.y >= Origin.y) && (Point.x <= End.x) && (Point.y <= End.y))
            return INT_IN;
        else
            return INT_OUT;
    }

    //-----------------------------------------------------------------------
    TIntersection CRectangle::Intersects(const CRectangle& Rect) const
    {
        // Intersection rectangle
        TVector2I Start(std::max(Origin.x, Rect.Origin.x), std::max(Origin.y, Rect.Origin.y));
        TVector2I End(std::min(End.x, Rect.End.x), std::min(End.y, Rect.End.y));
        CRectangle Overlap(Start, End - Start);

        if ((Start.x > End.x) || (Start.y > End.y))
            return INT_OUT;
        else if ((Overlap == *this) || (Overlap == Rect))
            return INT_IN;
        else
            return INT_INTERSECTS;
    }

    //-----------------------------------------------------------------------
    bool CRectangle::operator ==(const CRectangle& Rect) const
    {
        return (Origin == Rect.Origin) && (End == Rect.End);
    }

    //-----------------------------------------------------------------------
    bool CRectangle::operator !=(const CRectangle& Rect) const
    {
        return !(*this == Rect);
    }

    //-----------------------------------------------------------------------
    std::istream& operator >>(std::istream& Stream, CRectangle& Rect)
    {
        return Stream >> Rect.Origin >> Rect.End;
    }

    //-----------------------------------------------------------------------
    std::ostream& operator <<(std::ostream& Stream, const CRectangle& Rect)
    {
        return Stream << Rect.Origin << " " << Rect.End;
    }

} // namespace Keos
