comparison window/rect.cc @ 0:223b71206888

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