include/berkelium/Rect.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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;
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