-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrect.py
More file actions
executable file
·295 lines (245 loc) · 8.63 KB
/
Copy pathrect.py
File metadata and controls
executable file
·295 lines (245 loc) · 8.63 KB
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
import random
class Rect:
"""Define a pure and simple rectangle."""
# Class methods
@classmethod
def cross(cls, r1, r2):
"""Determine the rectangle resulting of the intersection of two rectangles."""
if r1.xmax < r2.xmin or r1.xmin > r2.xmax:
return
if r1.ymax < r2.ymin or r1.ymin > r2.ymax:
return
xmin = max(r1.xmin, r2.xmin)
ymin = max(r1.ymin, r2.ymin)
xmax = min(r1.xmax, r2.xmax)
ymax = min(r1.ymax, r2.ymax)
return Rect.createFromCorners(xmin, ymin, xmax, ymax)
@classmethod
def random(cls, borns=[-1, 1], borns_size=[0, 1]):
"""Create a random rect."""
x = random.uniform(*borns)
y = random.uniform(*borns)
sx = random.uniform(*borns_size)
sy = random.uniform(*borns_size)
return cls(x, y, sx, sy)
@classmethod
def createFromCorners(cls, *corners):
"""Create a rectangle."""
x, y, xm, ym = corners
w = xm - x
h = ym - y
return cls(x + w / 2, y + h / 2, w, h)
@classmethod
def createFromCoordinates(cls, *coordinates):
"""Create a rect using the coordinates."""
return cls(*coordinates)
@classmethod
def createFromRect(cls, *rect):
"""Create a rect from an unpacked pygame.rect"""
l, r, w, h = rect
return cls(l + w / 2, r + h / 2, w, h)
def __init__(self, x, y, w, h):
"""Create a rectangle using its x, y, width, and height, the
x and y components correspond to the center of the rectangle."""
self.components = [x, y, w, h]
def __getitem__(self, index):
return self.components[index]
def __setitem__(self, key, value):
self.components[key] = value
x = property(
lambda cls: cls.__getitem__(0),
lambda cls, value: cls.__setitem__(0, value),
doc="x component of the center",
)
y = property(
lambda cls: cls.__getitem__(1),
lambda cls, value: cls.__setitem__(1, value),
doc="y component of the center",
)
def getSize(self):
return [self.w, self.h]
def setSize(self, size):
self.w, self.height = size
size = property(getSize, setSize)
def getPosition(self):
return [self.x, self.y]
def setPosition(self, position):
self.x, self.y = position
center = position = property(getPosition, setPosition)
def __str__(self, n=2):
"""Return the string representation of a rect."""
r = self.__round__(n)
return (
"Rect(x="
+ str(r.x)
+ ",y="
+ str(r.y)
+ ",w="
+ str(r.w)
+ ",h="
+ str(r.h)
+ ")"
)
def __round__(self, n=2):
"""Round the components of the rect."""
x = round(self.x, n)
y = round(self.y, n)
w = round(self.w, n)
h = round(self.h, n)
return Rect(x, y, w, h)
def __contains__(self, position):
"""Determine if a position is in the rectangle."""
x, y = position
return (self.xmin <= x <= self.xmax) and (self.ymin <= y <= self.ymax)
def resize(self, n):
"""Allow the user to resize the rectangle."""
self.w *= n
self.h *= n
def __iter__(self):
self.iterator = 0
return self
def __next__(self):
if self.iterator < 4:
self.iterator += 1
return self.components[self.iterator - 1]
else:
raise StopIteration
# properties
# corners
def getCorners(self):
"""Return the corners of the rect."""
return [
self.x - self.w / 2,
self.y - self.h / 2,
self.x + self.w / 2,
self.y + self.h / 2,
]
def setCorners(self, corners):
"""Set the corners of the rect."""
x1, y1, x2, y2 = corners
self.w = x2 - x1
self.h = y2 - y1
self.x = x1 - self.w / 2
self.y = y1 - self.w / 2
# coordinates
def getCoordinates(self):
"""Return the coordinates of the rect."""
return [self.x, self.y, self.w, self.h]
def setCoordinates(self, coordinates):
"""Set the coordinates of the rect."""
self.position = coordinates[:2]
self.size = coordinates[2:]
# rect
def getRect(self):
"""Return the rect of the rectangle."""
return Rect.getRectFromCoordinates(self.getCoordinates())
def setRect(self, rect):
"""Set the rect of the rectangle."""
self.setCoordinates(Rect.getCoordinatesFromRect(rect))
# sx component
def getWidth(self):
"""Return the width."""
return self.components[2]
def setWidth(self, w):
"""Set the width."""
self.components[2] = w
# sy component
def getHeight(self):
"""Return the height."""
return self.components[3]
def setHeight(self, h):
"""Set the height."""
self.components[3] = h
# xmin component
def getXmin(self):
"""Return the minimum of the x component."""
return self.x - self.w / 2
def setXmin(self, xmin):
"""Set the minimum of the x component."""
self.x = xmin + w / 2
# ymin component
def getYmin(self):
"""Return the minimum of the y component."""
return self.y - self.h / 2
def setYmin(self, ymin):
"""Set the minimum of the y component."""
self.y = ymin + self.h / 2
# xmax component
def getXmax(self):
"""Return the maximum of the x component."""
return self.x + self.w / 2
def setXmax(self, xmax):
"""Set the maximum of the x component."""
self.x = xmax - self.w / 2
# ymax component
def getYmax(self):
"""Return the maximum of the y component."""
return self.y + self.h / 2
def setYmax(self, ymax):
"""Set the maximum of the y component."""
self.y = ymax - self.h / 2
corners = property(getCorners, setCorners, doc="Corners")
coordinates = property(getCoordinates, setCoordinates, doc="Center+Size")
w = sx = width = property(getWidth, setWidth, doc="Width")
h = sy = height = property(getHeight, setHeight, doc="Height")
xmin = x1 = left = l = property(getXmin, setXmin, doc="Left")
xmax = x2 = right = r = property(getXmax, setXmax, doc="Right")
ymin = y1 = bottom = b = property(getYmin, setYmin, doc="Bottom")
ymax = y2 = top = t = property(getYmax, setYmax, doc="Top")
# Static methods
@staticmethod
def getCornersFromCoordinates(coordinates):
"""Return the corners (top_left_corner,bottom_right_corner) using the coordinates (position+size)."""
"""[x,y,sx,sy] -> [mx,my,Mx,My]"""
x, y, sx, sy = coordinates
mx, my = x - sx / 2, y - sy / 2
Mx, My = x + sx / 2, y + sy / 2
return [mx, my, Mx, My]
@staticmethod
def getCoordinatesFromCorners(corners):
"""Return the coordinates (position+size) using the corners (top_left_corner,bottom_right_corner)."""
"""[mx,my,Mx,My] -> [x,y,sx,sy]"""
mx, my, Mx, My = corners
sx, sy = Mx - mx, My - my
x, y = mx + sx / 2, my + sy / 2
return [x, y, sx, sy]
@staticmethod
def getCoordinatesFromRect(rect):
"""Return the coordinates (position,size) using the rect (top_left_corner,size)."""
"""[x,y,sx,sy] -> [mx,my,sx,sy]"""
mx, my, sx, sy = rect
x, y = mx + sx / 2, my + sy / 2
return [x, y, sx, sy]
@staticmethod
def getRectFromCoordinates(coordinates):
"""Return the rect (top_left_corner,size) using the coordinates (position,size)."""
"""[mx,my,sx,sy] -> [x,y,sx,sy]"""
x, y, sx, sy = coordinates
mx, my = x - sx / 2, y - sy / 2
return [mx, my, sx, sy]
@staticmethod
def getRectFromCorners(corners):
"""Return the rect (top_left_corner,size) using the corners (top_left_corner,bottom_right_corner)."""
"""[mx,my,Mx,My] -> [mx,my,sx,sy]"""
mx, my, Mx, My = corners
sx, sy = Mx - mx, My - my
return [mx, my, sx, sy]
@staticmethod
def getCornersFromRect(rect):
"""Return the (top_left_corner,bottom_right_corner) using the corners rect (top_left_corner,size)."""
"""[mx,my,Mx,My] -> [mx,my,sx,sy]"""
mx, my, sx, sy = rect
Mx, My = mx + sx, my + sy
return [mx, my, Mx, My]
if __name__ == "__main__":
r1 = Rect.random()
r2 = Rect.random()
r1.x -= 1
print(r1, r2)
print(r1.corners)
print(r1.coordinates)
print(r1.x, r1.y)
print(r1.sx, r1.sy)
print(r1.width, r1.height)
r = Rect.cross(r1, r2)
print(*r1)