-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrect.py
executable file
·311 lines (268 loc) · 10.9 KB
/
rect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# ----------------------------------------------------------------------------
# cocos2d
# Copyright (c) 2008-2012 Daniel Moisset, Ricardo Quesada, Rayentray Tappa,
# Lucio Torre
# Copyright (c) 2009-2014 Richard Jones, Claudio Canepa
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of cocos2d nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
from __future__ import division, print_function, unicode_literals
__docformat__ = 'restructuredtext'
class Rect(object):
'''Define a rectangular area.
Many convenience handles and other properties are also defined - all of
which may be assigned to which will result in altering the position
and sometimes dimensions of the Rect:
- top -- y pixel extent
- bottom -- y pixel extent
- left -- x pixel extent
- right -- x pixel extent
- position -- (x, y) of bottom-left corner pixel
- origin -- (x, y) of bottom-left corner pixel
- center -- (x, y) of center pixel
- topleft -- (x, y) of top-left corner pixel
- topright -- (x, y) of top-right corner pixel
- bottomleft -- (x, y) of bottom-left corner pixel
- bottomright -- (x, y) of bottom-right corner pixel
- midtop -- (x, y) of middle of top side pixel
- midbottom -- (x, y) of middle of bottom side pixel
- midleft -- (x, y) of middle of left side pixel
- midright -- (x, y) of middle of right side pixel
- size -- (width, height) of rect
The Rect area includes the bottom and left borders but not the top and
right borders.
'''
def __init__(self, x, y, width, height):
'''Create a Rect with the bottom-left corner at (x, y) and
dimensions (width, height).
'''
self._x, self._y = x, y
self._width, self._height = width, height
def __nonzero__(self):
return bool(self.width and self.height)
def __repr__(self):
return 'Rect(xy=%.4g,%.4g; wh=%.4g,%.4g)'%(self.x, self.y,
self.width, self.height)
def __eq__(self, other):
'''Compare the two rects.
>>> r1 = Rect(0, 0, 10, 10)
>>> r1 == Rect(0, 0, 10, 10)
True
>>> r1 == Rect(1, 0, 10, 10)
False
>>> r1 == Rect(0, 1, 10, 10)
False
>>> r1 == Rect(0, 0, 11, 10)
False
>>> r1 == Rect(0, 0, 10, 11)
False
'''
return (self.x == other.x and self.y == other.y and
self.width == other.width and self.height == other.height)
# py3 compatiblity: obj that defines __eq__ needs to define __hash__ to be
# hashable, and we need that class RectCell(Rect, Cell) be hashable
__hash__ = object.__hash__
def __ne__(self, other):
'''Compare the two rects.
>>> r1 = Rect(0, 0, 10, 10)
>>> r1 != Rect(0, 0, 10, 10)
False
>>> r1 != Rect(1, 0, 10, 10)
True
>>> r1 != Rect(0, 1, 10, 10)
True
>>> r1 != Rect(0, 0, 11, 10)
True
>>> r1 != Rect(0, 0, 10, 11)
True
'''
return not (self == other)
def corners(self):
return [self.left, self.bottom, self.right, self.top]
def copy(self):
return self.__class__(self.x, self.y, self.width, self.height)
# the following four properties will most likely be overridden in a
# subclass
def set_x(self, value): self._x = value
x = property(lambda self: self._x, set_x)
def set_y(self, value): self._y = value
y = property(lambda self: self._y, set_y)
def set_width(self, value): self._width = value
width = property(lambda self: self._width, set_width)
def set_height(self, value): self._height = value
height = property(lambda self: self._height, set_height)
def contains(self, x, y):
'''Return boolean whether the point defined by x, y is inside the
rect area.
'''
if x < self.x or x > self.x + self.width: return False
if y < self.y or y > self.y + self.height: return False
return True
def intersects(self, other):
'''Return boolean whether the "other" rect (an object with .x, .y,
.width and .height attributes) overlaps this Rect in any way.
'''
if self.x + self.width < other.x: return False
if other.x + other.width < self.x: return False
if self.y + self.height < other.y: return False
if other.y + other.height < self.y: return False
return True
def clippedBy(self, other):
'''Determine whether this rect is clipped by the other rect.
>>> r1 = Rect(0, 0, 10, 10)
>>> r2 = Rect(1, 1, 9, 9)
>>> r2.clippedBy(r1) # r2 fits inside r1
False
>>> r1.clippedBy(r2) # r1 is clipped by r2
True
>>> r2 = Rect(1, 1, 11, 11)
>>> r1.intersect(r2)
Rect(xy=1,1; wh=9,9)
>>> r1.clippedBy(r2)
True
>>> r2.intersect(r1)
Rect(xy=1,1; wh=9,9)
>>> r2.clippedBy(r1)
True
>>> r2 = Rect(11, 11, 1, 1)
>>> r1.clippedBy(r2)
True
'''
if self.intersects(other): return True
if i.x > self.x: return True
if i.y > self.y: return True
if i.width < self.width: return True
if i.height < self.height: return True
return False
def intersect(self, other):
'''Find the intersection of two Rects.
>>> r1 = Rect(0, 51, 200, 17)
>>> r2 = Rect(0, 64, 200, 55)
>>> r1.intersect(r2)
Rect(xy=0,64; wh=200,4)
>>> r1 = Rect(0, 64, 200, 55)
>>> r2 = Rect(0, 0, 200, 17)
>>> print r1.intersect(r2)
None
>>> r1 = Rect(10, 10, 10, 10)
>>> r2 = Rect(20, 20, 10, 10)
>>> print r1.intersect(r2)
None
>>> bool(Rect(0, 0, 1, 1))
True
>>> bool(Rect(0, 0, 1, 0))
False
>>> bool(Rect(0, 0, 0, 1))
False
>>> bool(Rect(0, 0, 0, 0))
False
'''
s_tr_x, s_tr_y = self.topright
o_tr_x, o_tr_y = other.topright
bl_x = max(self.x, other.x)
bl_y = max(self.y, other.y)
tr_x = min(s_tr_x, o_tr_x)
tr_y = min(s_tr_y, o_tr_y)
w, h = max(0, tr_x-bl_x), max(0, tr_y-bl_y)
if not w or not h:
return None
return self.__class__(bl_x, bl_y, w, h)
def set_position(self, value): self._x, self._y = value
position = property(lambda self: (self._x, self._y), set_position)
def set_size(self, value): self._width, self._height = value
size = property(lambda self: (self._width, self._height), set_size)
def get_origin(self): return self.x, self.y
def set_origin(self, origin): self.x, self.y = origin
origin = property(get_origin, set_origin)
def get_top(self): return self.y + self.height
def set_top(self, y): self.y = y - self.height
top = property(get_top, set_top)
# r/w, in pixels, y extent
def get_bottom(self): return self.y
def set_bottom(self, y): self.y = y
bottom = property(get_bottom, set_bottom)
def get_left(self): return self.x
def set_left(self, x): self.x = x
left = property(get_left, set_left)
def get_right(self): return self.x + self.width
def set_right(self, x): self.x = x - self.width
right = property(get_right, set_right)
def get_center(self):
return (self.x + self.width//2, self.y + self.height//2)
def set_center(self, center):
x, y = center
self.position = (x - self.width//2, y - self.height//2.0)
center = property(get_center, set_center)
def get_midtop(self):
return (self.x + self.width//2, self.y + self.height)
def set_midtop(self, midtop):
x, y = midtop
self.position = (x - self.width//2, y - self.height)
midtop = property(get_midtop, set_midtop)
def get_midbottom(self):
return (self.x + self.width//2, self.y)
def set_midbottom(self, midbottom):
x, y = midbottom
self.position = (x - self.width//2, y)
midbottom = property(get_midbottom, set_midbottom)
def get_midleft(self):
return (self.x, self.y + self.height//2)
def set_midleft(self, midleft):
x, y = midleft
self.position = (x, y - self.height//2)
midleft = property(get_midleft, set_midleft)
def get_midright(self):
return (self.x + self.width, self.y + self.height//2)
def set_midright(self, midright):
x, y = midright
self.position = (x - self.width, y - self.height//2)
midright = property(get_midright, set_midright)
def get_topleft(self):
return (self.x, self.y + self.height)
def set_topleft(self, position):
x, y = position
self.position = (x, y - self.height)
topleft = property(get_topleft, set_topleft)
def get_topright(self):
return (self.x + self.width, self.y + self.height)
def set_topright(self, position):
x, y = position
self.position = (x - self.width, y - self.height)
topright = property(get_topright, set_topright)
def get_bottomright(self):
return (self.x + self.width, self.y)
def set_bottomright(self, position):
x, y = position
self.position = (x - self.width, y)
bottomright = property(get_bottomright, set_bottomright)
def get_bottomleft(self):
return (self.x, self.y)
def set_bottomleft(self, position):
self.x, self.y = position
bottomleft = property(get_bottomleft, set_bottomleft)