annotate window/rect.cc @ 48:ed6c21dde840

* use correct format (%p) for pointers
author thib
date Wed, 15 Apr 2009 20:28:19 +0000
parents 223b71206888
children 15a18fbe6f21
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 }
223b71206888 Initial import
thib
parents:
diff changeset
37 inline int MIN(int a, int b) {
223b71206888 Initial import
thib
parents:
diff changeset
38 if (a>b) return b;
223b71206888 Initial import
thib
parents:
diff changeset
39 return a;
223b71206888 Initial import
thib
parents:
diff changeset
40 }
223b71206888 Initial import
thib
parents:
diff changeset
41
223b71206888 Initial import
thib
parents:
diff changeset
42 Rect::Rect(int x1, int y1) {
223b71206888 Initial import
thib
parents:
diff changeset
43 lx = rx = x1;
223b71206888 Initial import
thib
parents:
diff changeset
44 ty = by = y1;
223b71206888 Initial import
thib
parents:
diff changeset
45 }
223b71206888 Initial import
thib
parents:
diff changeset
46 Rect::Rect(int x1, int y1, int x2, int y2) {
223b71206888 Initial import
thib
parents:
diff changeset
47 lx = MIN(x1,x2);
223b71206888 Initial import
thib
parents:
diff changeset
48 rx = MAX(x1,x2);
223b71206888 Initial import
thib
parents:
diff changeset
49 ty = MIN(y1,y2);
223b71206888 Initial import
thib
parents:
diff changeset
50 by = MAX(y1,y2);
223b71206888 Initial import
thib
parents:
diff changeset
51 }
223b71206888 Initial import
thib
parents:
diff changeset
52 Rect::Rect(const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
53 lx = r.lx;
223b71206888 Initial import
thib
parents:
diff changeset
54 rx = r.rx;
223b71206888 Initial import
thib
parents:
diff changeset
55 ty = r.ty;
223b71206888 Initial import
thib
parents:
diff changeset
56 by = r.by;
223b71206888 Initial import
thib
parents:
diff changeset
57 }
223b71206888 Initial import
thib
parents:
diff changeset
58
223b71206888 Initial import
thib
parents:
diff changeset
59 bool Rect::is_inner(const Rect& inner_rect) const {
223b71206888 Initial import
thib
parents:
diff changeset
60 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
61 r.intersect(inner_rect);
223b71206888 Initial import
thib
parents:
diff changeset
62 return r == inner_rect;
223b71206888 Initial import
thib
parents:
diff changeset
63 }
223b71206888 Initial import
thib
parents:
diff changeset
64 bool Rect::is_nearly_inner(const Rect& inner_rect, int delta) const {
223b71206888 Initial import
thib
parents:
diff changeset
65 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
66 r.lx -= delta;
223b71206888 Initial import
thib
parents:
diff changeset
67 r.ty -= delta;
223b71206888 Initial import
thib
parents:
diff changeset
68 r.rx += delta;
223b71206888 Initial import
thib
parents:
diff changeset
69 r.by += delta;
223b71206888 Initial import
thib
parents:
diff changeset
70 r.intersect(inner_rect);
223b71206888 Initial import
thib
parents:
diff changeset
71 return r == inner_rect;
223b71206888 Initial import
thib
parents:
diff changeset
72 }
223b71206888 Initial import
thib
parents:
diff changeset
73 bool Rect::is_crossed(const Rect& rect) const {
223b71206888 Initial import
thib
parents:
diff changeset
74 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
75 r.intersect(rect);
223b71206888 Initial import
thib
parents:
diff changeset
76 return !(r.empty());
223b71206888 Initial import
thib
parents:
diff changeset
77 }
223b71206888 Initial import
thib
parents:
diff changeset
78 void Rect::intersect(const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
79 if (lx > r.rx) rx = lx;
223b71206888 Initial import
thib
parents:
diff changeset
80 else if (rx < r.lx) lx = rx;
223b71206888 Initial import
thib
parents:
diff changeset
81 else {
223b71206888 Initial import
thib
parents:
diff changeset
82 lx = MAX(lx, r.lx);
223b71206888 Initial import
thib
parents:
diff changeset
83 rx = MIN(rx, r.rx);
223b71206888 Initial import
thib
parents:
diff changeset
84 }
223b71206888 Initial import
thib
parents:
diff changeset
85
223b71206888 Initial import
thib
parents:
diff changeset
86 if (ty > r.by) by = ty;
223b71206888 Initial import
thib
parents:
diff changeset
87 else if (by < r.ty) ty = by;
223b71206888 Initial import
thib
parents:
diff changeset
88 else {
223b71206888 Initial import
thib
parents:
diff changeset
89 ty = MAX(ty, r.ty);
223b71206888 Initial import
thib
parents:
diff changeset
90 by = MIN(by, r.by);
223b71206888 Initial import
thib
parents:
diff changeset
91 }
223b71206888 Initial import
thib
parents:
diff changeset
92 }
223b71206888 Initial import
thib
parents:
diff changeset
93 void Rect::join(const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
94 lx = MIN(lx, r.lx);
223b71206888 Initial import
thib
parents:
diff changeset
95 rx = MAX(rx, r.rx);
223b71206888 Initial import
thib
parents:
diff changeset
96 ty = MIN(ty, r.ty);
223b71206888 Initial import
thib
parents:
diff changeset
97 by = MAX(by, r.by);
223b71206888 Initial import
thib
parents:
diff changeset
98 }
223b71206888 Initial import
thib
parents:
diff changeset
99 void Rect::rmove(int add_x, int add_y) {
223b71206888 Initial import
thib
parents:
diff changeset
100 lx += add_x;
223b71206888 Initial import
thib
parents:
diff changeset
101 rx += add_x;
223b71206888 Initial import
thib
parents:
diff changeset
102 ty += add_y;
223b71206888 Initial import
thib
parents:
diff changeset
103 by += add_y;
223b71206888 Initial import
thib
parents:
diff changeset
104 }
223b71206888 Initial import
thib
parents:
diff changeset
105 void Rect::subtract(const Rect& rect, vector<Rect>& ret_array) const {
223b71206888 Initial import
thib
parents:
diff changeset
106 Rect r = *this;
223b71206888 Initial import
thib
parents:
diff changeset
107 r.intersect(rect);
223b71206888 Initial import
thib
parents:
diff changeset
108 if (r.empty()) { // not intersect the rects
223b71206888 Initial import
thib
parents:
diff changeset
109 ret_array.push_back(*this);
223b71206888 Initial import
thib
parents:
diff changeset
110 return;
223b71206888 Initial import
thib
parents:
diff changeset
111 }
223b71206888 Initial import
thib
parents:
diff changeset
112 if (r ==*this) { // identical; no rect rests
223b71206888 Initial import
thib
parents:
diff changeset
113 return;
223b71206888 Initial import
thib
parents:
diff changeset
114 }
223b71206888 Initial import
thib
parents:
diff changeset
115 // push top area
223b71206888 Initial import
thib
parents:
diff changeset
116 if (ty != r.ty) {
223b71206888 Initial import
thib
parents:
diff changeset
117 ret_array.push_back(Rect(lx, ty, rx, r.ty));
223b71206888 Initial import
thib
parents:
diff changeset
118 }
223b71206888 Initial import
thib
parents:
diff changeset
119 // push bottom area
223b71206888 Initial import
thib
parents:
diff changeset
120 if (by != r.by) {
223b71206888 Initial import
thib
parents:
diff changeset
121 ret_array.push_back(Rect(lx, r.by, rx, by));
223b71206888 Initial import
thib
parents:
diff changeset
122 }
223b71206888 Initial import
thib
parents:
diff changeset
123 // push left area
223b71206888 Initial import
thib
parents:
diff changeset
124 if (lx != r.lx) {
223b71206888 Initial import
thib
parents:
diff changeset
125 ret_array.push_back(Rect(lx, r.ty, r.lx, r.by));
223b71206888 Initial import
thib
parents:
diff changeset
126 }
223b71206888 Initial import
thib
parents:
diff changeset
127 // push right area
223b71206888 Initial import
thib
parents:
diff changeset
128 if (rx != r.rx) {
223b71206888 Initial import
thib
parents:
diff changeset
129 ret_array.push_back(Rect(r.rx, r.ty, rx, r.by));
223b71206888 Initial import
thib
parents:
diff changeset
130 }
223b71206888 Initial import
thib
parents:
diff changeset
131 return;
223b71206888 Initial import
thib
parents:
diff changeset
132 }