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