rect.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 /***************************************************************************
00023 
00024     Rectangle intersection code copied and modified from the guichan 0.4
00025     source, which is released under the BSD license.
00026 
00027     Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved.
00028 
00029     * Redistribution and use in source and binary forms, with or without modification,
00030       are permitted provided that the following conditions are met: 
00031       Redistributions of source code must retain the above copyright notice,
00032       this list of conditions and the following disclaimer.
00033 
00034     * Redistributions in binary form must reproduce the above copyright notice,
00035       this list of conditions and the following disclaimer in the documentation
00036       and/or other materials provided with the distribution.
00037 
00038     * Neither the name of the Guichan nor the names of its contributors may be used
00039       to endorse or promote products derived from this software without specific
00040       prior written permission.
00041 
00042  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
00043  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00044  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00045  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
00046  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00047  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00048  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00049  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00050  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00051 
00052     For more Information about guichan see: http://guichan.sourceforge.net
00053 
00054 ****************************************************************************/
00055 
00056 #ifndef FIFE_VIDEO_RECT_H
00057 #define FIFE_VIDEO_RECT_H
00058 
00059 // Standard C++ library includes
00060 #include <iostream>
00061 
00062 // 3rd party library includes
00063 
00064 // FIFE includes
00065 // These includes are split up in two parts, separated by one empty line
00066 // First block: files included from the FIFE root src directory
00067 // Second block: files included from the same folder
00068 #include "point.h"
00069 
00070 namespace FIFE {
00071 
00079     class Rect {
00080         public:
00083             int x;
00086             int y;
00089             int w;
00092             int h;
00093 
00098             explicit Rect(int x = 0, int y = 0, unsigned int width = 0, unsigned int height = 0);
00099 
00102             int right() const;
00103 
00106             int bottom() const;
00107 
00113             bool operator==(const Rect& rect ) const;
00114 
00120             bool contains( const Point& point ) const;
00121 
00129             bool intersects( const Rect& rect ) const;
00130 
00137             bool intersectInplace( const Rect& rect );
00138 
00139     };
00140 
00146     std::ostream& operator<<(std::ostream&, const Rect&);
00147 
00148 
00150 
00151     inline 
00152     int Rect::right() const {
00153         return x + w;
00154     }
00155 
00156     inline 
00157     int Rect::bottom() const {
00158         return y + h;
00159     }
00160 
00161 
00162     inline 
00163     bool Rect::operator==(const Rect& rect ) const {
00164         return
00165             x == rect.x && y == rect.y && w == rect.w && h == rect.h;
00166     }
00167 
00168 
00169     inline
00170     bool Rect::contains( const Point& point ) const {
00171         return
00172             (((point.x >= x) && (point.x <= x + w))
00173                 && ((point.y >= y) && (point.y <= y + h)));
00174     }
00175 
00176 
00177     inline
00178     bool Rect::intersectInplace( const Rect& rectangle ) {
00179         x = x - rectangle.x;
00180         y = y - rectangle.y;
00181 
00182     
00183         if (x < 0) {
00184             w += x;
00185             x = 0;
00186         }
00187     
00188         if (y < 0) {
00189             h += y;
00190             y = 0;
00191         }
00192     
00193         if (x + w > rectangle.w) {
00194             w = rectangle.w - x;
00195         }
00196     
00197         if (y + h > rectangle.h) {
00198             h = rectangle.h - y;
00199         }
00200     
00201         x += rectangle.x;
00202         y += rectangle.y;
00203 
00204         if (w <= 0 || h <= 0) {
00205             h = 0;
00206             w = 0;
00207             return false;
00208         }
00209         return true;
00210     }
00211 
00212 
00213     inline
00214     bool Rect::intersects( const Rect& rectangle ) const {
00215         int _x = x - rectangle.x;
00216         int _y = y - rectangle.y;
00217         int _w = w;
00218         int _h = h;
00219 
00220     
00221         if (_x < 0) {
00222             _w += _x;
00223             _x = 0;
00224         }
00225     
00226         if (_y < 0) {
00227             _h += _y;
00228             _y = 0;
00229         }
00230     
00231         if (_x + _w > rectangle.w) {
00232             _w = rectangle.w - _x;
00233         }
00234     
00235         if (_y + _h > rectangle.h) {
00236             _h = rectangle.h - _y;
00237         }
00238     
00239         if (_w <= 0 || _h <= 0) {
00240             return false;
00241         }
00242         return true;
00243     }
00244 
00245 }
00246 
00247 #endif