comparison window/rect.cc @ 52:15a18fbe6f21

* Known bugs added to the README * Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author thib
date Sat, 18 Apr 2009 18:35:39 +0000
parents 223b71206888
children
comparison
equal deleted inserted replaced
51:cbb301016a4e 52:15a18fbe6f21
32 32
33 inline int MAX(int a, int b) { 33 inline int MAX(int a, int b) {
34 if (a>b) return a; 34 if (a>b) return a;
35 return b; 35 return b;
36 } 36 }
37
37 inline int MIN(int a, int b) { 38 inline int MIN(int a, int b) {
38 if (a>b) return b; 39 if (a>b) return b;
39 return a; 40 return a;
40 } 41 }
41 42
42 Rect::Rect(int x1, int y1) { 43 Rect::Rect(int x1, int y1) {
43 lx = rx = x1; 44 lx = rx = x1;
44 ty = by = y1; 45 ty = by = y1;
45 } 46 }
47
46 Rect::Rect(int x1, int y1, int x2, int y2) { 48 Rect::Rect(int x1, int y1, int x2, int y2) {
47 lx = MIN(x1,x2); 49 lx = MIN(x1,x2);
48 rx = MAX(x1,x2); 50 rx = MAX(x1,x2);
49 ty = MIN(y1,y2); 51 ty = MIN(y1,y2);
50 by = MAX(y1,y2); 52 by = MAX(y1,y2);
51 } 53 }
54
52 Rect::Rect(const Rect& r) { 55 Rect::Rect(const Rect& r) {
53 lx = r.lx; 56 lx = r.lx;
54 rx = r.rx; 57 rx = r.rx;
55 ty = r.ty; 58 ty = r.ty;
56 by = r.by; 59 by = r.by;
59 bool Rect::is_inner(const Rect& inner_rect) const { 62 bool Rect::is_inner(const Rect& inner_rect) const {
60 Rect r = *this; 63 Rect r = *this;
61 r.intersect(inner_rect); 64 r.intersect(inner_rect);
62 return r == inner_rect; 65 return r == inner_rect;
63 } 66 }
67
64 bool Rect::is_nearly_inner(const Rect& inner_rect, int delta) const { 68 bool Rect::is_nearly_inner(const Rect& inner_rect, int delta) const {
65 Rect r = *this; 69 Rect r = *this;
66 r.lx -= delta; 70 r.lx -= delta;
67 r.ty -= delta; 71 r.ty -= delta;
68 r.rx += delta; 72 r.rx += delta;
69 r.by += delta; 73 r.by += delta;
70 r.intersect(inner_rect); 74 r.intersect(inner_rect);
71 return r == inner_rect; 75 return r == inner_rect;
72 } 76 }
77
73 bool Rect::is_crossed(const Rect& rect) const { 78 bool Rect::is_crossed(const Rect& rect) const {
74 Rect r = *this; 79 Rect r = *this;
75 r.intersect(rect); 80 r.intersect(rect);
76 return !(r.empty()); 81 return !(r.empty());
77 } 82 }
83
78 void Rect::intersect(const Rect& r) { 84 void Rect::intersect(const Rect& r) {
79 if (lx > r.rx) rx = lx; 85 if (lx > r.rx) rx = lx;
80 else if (rx < r.lx) lx = rx; 86 else if (rx < r.lx) lx = rx;
81 else { 87 else {
82 lx = MAX(lx, r.lx); 88 lx = MAX(lx, r.lx);
88 else { 94 else {
89 ty = MAX(ty, r.ty); 95 ty = MAX(ty, r.ty);
90 by = MIN(by, r.by); 96 by = MIN(by, r.by);
91 } 97 }
92 } 98 }
99
93 void Rect::join(const Rect& r) { 100 void Rect::join(const Rect& r) {
94 lx = MIN(lx, r.lx); 101 lx = MIN(lx, r.lx);
95 rx = MAX(rx, r.rx); 102 rx = MAX(rx, r.rx);
96 ty = MIN(ty, r.ty); 103 ty = MIN(ty, r.ty);
97 by = MAX(by, r.by); 104 by = MAX(by, r.by);
98 } 105 }
106
99 void Rect::rmove(int add_x, int add_y) { 107 void Rect::rmove(int add_x, int add_y) {
100 lx += add_x; 108 lx += add_x;
101 rx += add_x; 109 rx += add_x;
102 ty += add_y; 110 ty += add_y;
103 by += add_y; 111 by += add_y;
104 } 112 }
113
105 void Rect::subtract(const Rect& rect, vector<Rect>& ret_array) const { 114 void Rect::subtract(const Rect& rect, vector<Rect>& ret_array) const {
106 Rect r = *this; 115 Rect r = *this;
107 r.intersect(rect); 116 r.intersect(rect);
108 if (r.empty()) { // not intersect the rects 117 if (r.empty()) { // not intersect the rects
109 ret_array.push_back(*this); 118 ret_array.push_back(*this);
126 } 135 }
127 // push right area 136 // push right area
128 if (rx != r.rx) { 137 if (rx != r.rx) {
129 ret_array.push_back(Rect(r.rx, r.ty, rx, r.by)); 138 ret_array.push_back(Rect(r.rx, r.ty, rx, r.by));
130 } 139 }
131 return;
132 } 140 }