KeosRectangle.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 #include "KeosRectangle.h"
00022 
00023 namespace Keos
00024 {
00025 
00026   //=======================================================================
00027   // CRectangle implementation
00028   //=======================================================================
00029 
00030   //-----------------------------------------------------------------------
00031   CRectangle::CRectangle(const TVector2I& Start, const TVector2I& Size) :
00032       Origin(Start),
00033       End   (Start + Size)
00034   {}
00035 
00036   //-----------------------------------------------------------------------
00037   CRectangle::CRectangle(int nLeft, int nTop, int nWidth, int nHeight) :
00038       Origin(nLeft, nTop),
00039       End   (nLeft + nWidth, nTop + nHeight)
00040   {}
00041 
00042   //-----------------------------------------------------------------------
00043   void CRectangle::Set(int nLeft, int nTop, int nWidth, int nHeight)
00044   {
00045     Origin.Set(nLeft, nTop);
00046     End.Set(nLeft + nWidth, nTop + nHeight);
00047   }
00048 
00049   //-----------------------------------------------------------------------
00050   int CRectangle::Left() const
00051   {
00052     return Origin.x;
00053   }
00054 
00055   //-----------------------------------------------------------------------
00056   int CRectangle::Right() const
00057   {
00058     return End.x;
00059   }
00060 
00061   //-----------------------------------------------------------------------
00062   int CRectangle::Top() const
00063   {
00064     return Origin.y;
00065   }
00066 
00067   //-----------------------------------------------------------------------
00068   int CRectangle::Bottom() const
00069   {
00070     return End.y;
00071   }
00072 
00073   //-----------------------------------------------------------------------
00074   int CRectangle::Width() const
00075   {
00076     return End.x - Origin.x;
00077   }
00078 
00079   //-----------------------------------------------------------------------
00080   int CRectangle::Height() const
00081   {
00082     return End.y - Origin.y;
00083   }
00084 
00085   //-----------------------------------------------------------------------
00086   TVector2I CRectangle::Size() const
00087   {
00088     return End - Origin;
00089   }
00090 
00091   //-----------------------------------------------------------------------
00092   TIntersection CRectangle::Intersects(const TVector2I& Point) const
00093   {
00094     if ((Point.x >= Origin.x) && (Point.y >= Origin.y) && (Point.x <= End.x) && (Point.y <= End.y))
00095       return INT_IN;
00096     else
00097       return INT_OUT;
00098   }
00099 
00100   //-----------------------------------------------------------------------
00101   TIntersection CRectangle::Intersects(const CRectangle& Rect) const
00102   {
00103     // Intersection rectangle
00104     TVector2I Start(std::max(Origin.x, Rect.Origin.x), std::max(Origin.y, Rect.Origin.y));
00105     TVector2I End(std::min(End.x, Rect.End.x), std::min(End.y, Rect.End.y));
00106     CRectangle Overlap(Start, End - Start);
00107 
00108     if ((Start.x > End.x) || (Start.y > End.y))
00109       return INT_OUT;
00110     else if ((Overlap == *this) || (Overlap == Rect))
00111       return INT_IN;
00112     else
00113       return INT_INTERSECTS;
00114   }
00115 
00116   //-----------------------------------------------------------------------
00117   bool CRectangle::operator ==(const CRectangle& Rect) const
00118   {
00119     return (Origin == Rect.Origin) && (End == Rect.End);
00120   }
00121 
00122   //-----------------------------------------------------------------------
00123   bool CRectangle::operator !=(const CRectangle& Rect) const
00124   {
00125     return !(*this == Rect);
00126   }
00127 
00128   //-----------------------------------------------------------------------
00129   std::istream& operator >>(std::istream& Stream, CRectangle& Rect)
00130   {
00131     return Stream >> Rect.Origin >> Rect.End;
00132   }
00133 
00134   //-----------------------------------------------------------------------
00135   std::ostream& operator <<(std::ostream& Stream, const CRectangle& Rect)
00136   {
00137     return Stream << Rect.Origin << " " << Rect.End;
00138   }
00139 
00140 } // namespace Keos

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1