el h / SimpleGUI

Fork of SimpleGUI by Duncan McIntyre

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Rectangle.h Source File

Rectangle.h

00001 #ifndef SIMPLEGUI_RECTANGLE_H
00002 #define SIMPLEGUI_RECTANGLE_H
00003 
00004 #include <algorithm>
00005 #include "Point.h"
00006 
00007 class Rectangle
00008 {
00009 
00010 public:
00011 
00012     Rectangle(int _x, int _y, int _w, int _h) :
00013         x(_x), y(_y), width(_w), height(_h) {}
00014 
00015     void resize(const Rectangle &outer, int padding) {
00016         x = outer.x+padding;
00017         y = outer.y+padding;
00018         width = outer.width - 2 * padding;
00019         height = outer.height - 2 * padding;
00020     }
00021 
00022     bool contains(int pointX, int pointY) {
00023         return pointX >= x
00024                && pointX <= (x+width)
00025                && pointY >= y
00026                && pointY <= (y+height);
00027     }
00028 
00029     bool intersects(const Rectangle &r) {
00030         int x1 = std::max(x, r.x);
00031         int x2 = std::min(x+width, r.x + r.width);
00032         int y1 = std::max(y, r.y);
00033         int y2 = std::min(y+height, r.y + r.height);
00034         if((x2 < x1) || (y2 < y1)) {
00035             return false;
00036         }
00037         return true;
00038     }
00039     
00040     bool isLocatedAt(Point *p) {
00041         return p->x() == x && p->y() == y;
00042     }
00043 
00044     int x, y, width, height;
00045 };
00046 
00047 #endif