Berkelium

include/berkelium/Rect.hpp

Go to the documentation of this file.
00001 /*  Berkelium - Embedded Chromium
00002  *  Rect.hpp
00003  *
00004  *  Copyright (c) 2009, Daniel Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _BERKELIUM_RECT_HPP_
00034 #define _BERKELIUM_RECT_HPP_
00035 
00036 namespace Berkelium {
00037 
00038 struct Rect {
00039     int mLeft;
00040     int mTop;
00041     int mWidth;
00042     int mHeight;
00043 
00044     inline int y() const { return mTop; }
00045     inline int x() const { return mLeft; }
00046     inline int top() const { return mTop; }
00047     inline int left() const { return mLeft; }
00048     inline int width() const { return mWidth; }
00049     inline int height() const { return mHeight; }
00050     inline int right() const { return mLeft + mWidth; }
00051     inline int bottom() const { return mTop + mHeight; }
00052 
00053     template <class T>
00054     inline void setFromRect(const T&sourceRect) {
00055         mLeft = sourceRect.x();
00056         mTop = sourceRect.y();
00057         mWidth = sourceRect.width();
00058         mHeight = sourceRect.height();
00059     }
00060 
00061     inline bool contains(int x, int y) const {
00062         return (x >= left() && x < right() &&
00063                 y >= top() && y < bottom());
00064     }
00065     Rect intersect(const Rect &rect) const {
00066         int rx = rectmax(left(), rect.left());
00067         int ry = rectmax(top(), rect.top());
00068         int rr = rectmin(right(), rect.right());
00069         int rb = rectmin(bottom(), rect.bottom());
00070         if (rx >= rr || ry >= rb)
00071             rx = ry = rr = rb = 0;  // non-intersecting
00072         Rect ret;
00073         ret.mLeft = rx;
00074         ret.mTop = ry;
00075         ret.mWidth = rr-rx;
00076         ret.mHeight = rb-ry;
00077         return ret;
00078     }
00079 
00086     Rect translate(int dx, int dy) const {
00087         Rect ret = *this;
00088         ret.mLeft += dx;
00089         ret.mTop += dy;
00090         return ret;
00091     }
00092 
00093 private:
00094     static int rectmax(int a, int b) {
00095         return a>b?a:b;
00096     }
00097     static int rectmin(int a, int b) {
00098         return a<b?a:b;
00099     }
00100 };
00101 
00102 }
00103 
00104 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated on 22 Nov 2013