annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28
223b71206888 Initial import
thib
parents:
diff changeset
29 #include "rect.h"
223b71206888 Initial import
thib
parents:
diff changeset
30
223b71206888 Initial import
thib
parents:
diff changeset
31 using namespace std;
223b71206888 Initial import
thib
parents:
diff changeset
32
223b71206888 Initial import
thib
parents:
diff changeset
33 inline int MAX(int a, int b) {
223b71206888 Initial import
thib
parents:
diff changeset
34 if (a>b) return a;
223b71206888 Initial import
thib
parents:
diff changeset
35 return b;
223b71206888 Initial import
thib
parents:
diff changeset
36 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
37
0
223b71206888 Initial import
thib
parents:
diff changeset
38 inline int MIN(int a, int b) {
223b71206888 Initial import
thib
parents:
diff changeset
39 if (a>b) return b;
223b71206888 Initial import
thib
parents:
diff changeset
40 return a;
223b71206888 Initial import
thib
parents:
diff changeset
41 }
223b71206888 Initial import
thib
parents:
diff changeset
42
223b71206888 Initial import
thib
parents:
diff changeset
43 Rect::Rect(int x1, int y1) {
223b71206888 Initial import
thib
parents:
diff changeset
44 lx = rx = x1;
223b71206888 Initial import
thib
parents:
diff changeset
45 ty = by = y1;
223b71206888 Initial import
thib
parents:
diff changeset
46 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
47
0
223b71206888 Initial import
thib
parents:
diff changeset
48 Rect::Rect(int x1, int y1, int x2, int y2) {
223b71206888 Initial import
thib
parents:
diff changeset
49 lx = MIN(x1,x2);
223b71206888 Initial import
thib
parents:
diff changeset
50 rx = MAX(x1,x2);
223b71206888 Initial import
thib
parents:
diff changeset
51 ty = MIN(y1,y2);
223b71206888 Initial import
thib
parents:
diff changeset
52 by = MAX(y1,y2);
223b71206888 Initial import
thib
parents:
diff changeset
53 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
54
0
223b71206888 Initial import
thib
parents:
diff changeset
55 Rect::Rect(const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
56 lx = r.lx;
223b71206888 Initial import
thib
parents:
diff changeset
57 rx = r.rx;
223b71206888 Initial import
thib
parents:
diff changeset
58 ty = r.ty;
223b71206888 Initial import
thib
parents:
diff changeset
59 by = r.by;
223b71206888 Initial import
thib
parents:
diff changeset
60 }
223b71206888 Initial import
thib
parents:
diff changeset
61
223b71206888 Initial import
thib
parents:
diff changeset
62 bool Rect::is_inner(const Rect& inner_rect) const {
223b71206888 Initial import
thib
parents:
diff changeset
63 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
64 r.intersect(inner_rect);
223b71206888 Initial import
thib
parents:
diff changeset
65 return r == inner_rect;
223b71206888 Initial import
thib
parents:
diff changeset
66 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
67
0
223b71206888 Initial import
thib
parents:
diff changeset
68 bool Rect::is_nearly_inner(const Rect& inner_rect, int delta) const {
223b71206888 Initial import
thib
parents:
diff changeset
69 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
70 r.lx -= delta;
223b71206888 Initial import
thib
parents:
diff changeset
71 r.ty -= delta;
223b71206888 Initial import
thib
parents:
diff changeset
72 r.rx += delta;
223b71206888 Initial import
thib
parents:
diff changeset
73 r.by += delta;
223b71206888 Initial import
thib
parents:
diff changeset
74 r.intersect(inner_rect);
223b71206888 Initial import
thib
parents:
diff changeset
75 return r == inner_rect;
223b71206888 Initial import
thib
parents:
diff changeset
76 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
77
0
223b71206888 Initial import
thib
parents:
diff changeset
78 bool Rect::is_crossed(const Rect& rect) const {
223b71206888 Initial import
thib
parents:
diff changeset
79 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
80 r.intersect(rect);
223b71206888 Initial import
thib
parents:
diff changeset
81 return !(r.empty());
223b71206888 Initial import
thib
parents:
diff changeset
82 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
83
0
223b71206888 Initial import
thib
parents:
diff changeset
84 void Rect::intersect(const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
85 if (lx > r.rx) rx = lx;
223b71206888 Initial import
thib
parents:
diff changeset
86 else if (rx < r.lx) lx = rx;
223b71206888 Initial import
thib
parents:
diff changeset
87 else {
223b71206888 Initial import
thib
parents:
diff changeset
88 lx = MAX(lx, r.lx);
223b71206888 Initial import
thib
parents:
diff changeset
89 rx = MIN(rx, r.rx);
223b71206888 Initial import
thib
parents:
diff changeset
90 }
223b71206888 Initial import
thib
parents:
diff changeset
91
223b71206888 Initial import
thib
parents:
diff changeset
92 if (ty > r.by) by = ty;
223b71206888 Initial import
thib
parents:
diff changeset
93 else if (by < r.ty) ty = by;
223b71206888 Initial import
thib
parents:
diff changeset
94 else {
223b71206888 Initial import
thib
parents:
diff changeset
95 ty = MAX(ty, r.ty);
223b71206888 Initial import
thib
parents:
diff changeset
96 by = MIN(by, r.by);
223b71206888 Initial import
thib
parents:
diff changeset
97 }
223b71206888 Initial import
thib
parents:
diff changeset
98 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
99
0
223b71206888 Initial import
thib
parents:
diff changeset
100 void Rect::join(const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
101 lx = MIN(lx, r.lx);
223b71206888 Initial import
thib
parents:
diff changeset
102 rx = MAX(rx, r.rx);
223b71206888 Initial import
thib
parents:
diff changeset
103 ty = MIN(ty, r.ty);
223b71206888 Initial import
thib
parents:
diff changeset
104 by = MAX(by, r.by);
223b71206888 Initial import
thib
parents:
diff changeset
105 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
106
0
223b71206888 Initial import
thib
parents:
diff changeset
107 void Rect::rmove(int add_x, int add_y) {
223b71206888 Initial import
thib
parents:
diff changeset
108 lx += add_x;
223b71206888 Initial import
thib
parents:
diff changeset
109 rx += add_x;
223b71206888 Initial import
thib
parents:
diff changeset
110 ty += add_y;
223b71206888 Initial import
thib
parents:
diff changeset
111 by += add_y;
223b71206888 Initial import
thib
parents:
diff changeset
112 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
113
0
223b71206888 Initial import
thib
parents:
diff changeset
114 void Rect::subtract(const Rect& rect, vector<Rect>& ret_array) const {
223b71206888 Initial import
thib
parents:
diff changeset
115 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
116 r.intersect(rect);
223b71206888 Initial import
thib
parents:
diff changeset
117 if (r.empty()) { // not intersect the rects
223b71206888 Initial import
thib
parents:
diff changeset
118 ret_array.push_back(*this);
223b71206888 Initial import
thib
parents:
diff changeset
119 return;
223b71206888 Initial import
thib
parents:
diff changeset
120 }
223b71206888 Initial import
thib
parents:
diff changeset
121 if (r ==*this) { // identical; no rect rests
223b71206888 Initial import
thib
parents:
diff changeset
122 return;
223b71206888 Initial import
thib
parents:
diff changeset
123 }
223b71206888 Initial import
thib
parents:
diff changeset
124 // push top area
223b71206888 Initial import
thib
parents:
diff changeset
125 if (ty != r.ty) {
223b71206888 Initial import
thib
parents:
diff changeset
126 ret_array.push_back(Rect(lx, ty, rx, r.ty));
223b71206888 Initial import
thib
parents:
diff changeset
127 }
223b71206888 Initial import
thib
parents:
diff changeset
128 // push bottom area
223b71206888 Initial import
thib
parents:
diff changeset
129 if (by != r.by) {
223b71206888 Initial import
thib
parents:
diff changeset
130 ret_array.push_back(Rect(lx, r.by, rx, by));
223b71206888 Initial import
thib
parents:
diff changeset
131 }
223b71206888 Initial import
thib
parents:
diff changeset
132 // push left area
223b71206888 Initial import
thib
parents:
diff changeset
133 if (lx != r.lx) {
223b71206888 Initial import
thib
parents:
diff changeset
134 ret_array.push_back(Rect(lx, r.ty, r.lx, r.by));
223b71206888 Initial import
thib
parents:
diff changeset
135 }
223b71206888 Initial import
thib
parents:
diff changeset
136 // push right area
223b71206888 Initial import
thib
parents:
diff changeset
137 if (rx != r.rx) {
223b71206888 Initial import
thib
parents:
diff changeset
138 ret_array.push_back(Rect(r.rx, r.ty, rx, r.by));
223b71206888 Initial import
thib
parents:
diff changeset
139 }
223b71206888 Initial import
thib
parents:
diff changeset
140 }