openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

Rect.h (2198B)


      1 #ifndef RECT_H
      2 #define RECT_H
      3 
      4 #include <stdio.h>
      5 #include "util.h"
      6 
      7 class Rect { 
      8 public:
      9 	inline Rect();
     10 	inline Rect(int left, int top, int right, int bottom);
     11 	inline void SetTo(int l, int t, int r, int b);
     12 	inline Rect& InsetBy(int, int);
     13 	inline Rect& OffsetBy(int, int);
     14 	inline Rect& OffsetTo(int, int);
     15 	inline bool Contains(int, int) const;	
     16  	inline bool Contains(const Rect&) const;
     17 	inline bool Intersects(const Rect&) const;
     18 	inline Rect& Intersect(const Rect&);	
     19 	inline bool Valid() const;
     20 	inline int Width() const;
     21 	inline int Height() const;
     22 	inline void Dump() const;
     23 
     24 	int left;
     25 	int top;
     26 	int right;
     27 	int bottom;
     28 };
     29 
     30 inline Rect::Rect()
     31 	:	left(0),
     32 		top(0),
     33 		right(0),
     34 		bottom(0)
     35 {
     36 }
     37 
     38 inline Rect::Rect(int l, int t, int r, int b)
     39 	:	left(l),
     40 		top(t),
     41 		right(r),
     42 		bottom(b)
     43 {
     44 }
     45 
     46 inline void Rect::SetTo(int l, int t, int r, int b)
     47 {
     48 	left = l;
     49 	top = t;
     50 	right = r;
     51 	bottom = b;
     52 }
     53 
     54 
     55 
     56 inline Rect& Rect::InsetBy(int h, int v)
     57 {
     58 	left += h;
     59 	right -= h;
     60 	top += v;
     61 	bottom -= v;
     62 	return *this;
     63 }
     64 
     65 inline Rect& Rect::OffsetBy(int h, int v)
     66 {
     67 	left += h;
     68 	right += h;
     69 	top += v;
     70 	bottom += v;	
     71 	return *this;
     72 }
     73 
     74 
     75 inline Rect& Rect::OffsetTo(int h, int v)
     76 {
     77 	right += (h - left);
     78 	bottom += (v - top);
     79 	left = h;
     80 	top = v;
     81 	return *this;
     82 }
     83 
     84 inline bool Rect::Intersects(const Rect &rect) const
     85 {
     86 	return max(left, rect.left) <= min(right, rect.right)
     87 		&& max(top, rect.top) <= min(bottom, rect.bottom);
     88 }
     89 
     90 inline bool Rect::Valid() const
     91 {
     92 	return right >= left && bottom >= top;
     93 }
     94 
     95 inline void Rect::Dump() const
     96 {
     97 	printf("Rect (%d, %d, %d, %d)\n", left, top, right, bottom);
     98 }
     99 
    100 
    101 inline int Rect::Width() const
    102 {
    103 	return right - left;
    104 }
    105 
    106 inline int Rect::Height() const
    107 {
    108 	return bottom - top;
    109 }
    110 
    111 inline bool Rect::Contains(int x, int y) const
    112 {
    113 	return (x >= left && x <= right && y >= top && y <= bottom);       
    114 }
    115 
    116 inline bool Rect::Contains(const Rect &rect) const
    117 {
    118 	return rect.left >= left && rect.right <= right 
    119 		&& rect.top >= top && rect.bottom <= bottom;
    120 }
    121 
    122 inline Rect& Rect::Intersect(const Rect &rect) 
    123 {
    124 	left = max(left, rect.left);
    125 	right = min(right, rect.right);
    126 	top = max(top, rect.top);
    127 	bottom = min(bottom, rect.bottom);
    128 	return *this;
    129 }
    130 
    131 #endif