pygame / pygame / rect.py

aholkner afab292 











aholkner 00c2125 





















aholkner afab292 








aholkner feb6f7d 


aholkner afab292 










aholkner 0f3a0f7 
aholkner 00c2125 








aholkner 6160987 
aholkner 0f3a0f7 
aholkner 00c2125 










aholkner afab292 









aholkner 0f3a0f7 

aholkner afab292 














aholkner 6160987 
aholkner afab292 
aholkner 6160987 
aholkner afab292 




































aholkner b2fb6e0 
aholkner fed4366 
aholkner b2fb6e0 
aholkner e53dd7a 
aholkner afab292 
aholkner e53dd7a 
aholkner afab292 
aholkner e53dd7a 
aholkner afab292 
aholkner f135a2b 
aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 

aholkner afab292 
aholkner e53dd7a 
aholkner afab292 
aholkner e53dd7a 
aholkner afab292 
aholkner e53dd7a 
aholkner 00c2125 
aholkner e53dd7a 
aholkner afab292 
aholkner e53dd7a 
aholkner 00c2125 
aholkner e53dd7a 
aholkner afab292 
aholkner e53dd7a 
aholkner 00c2125 
aholkner e53dd7a 
aholkner afab292 


aholkner 00c2125 



aholkner afab292 










aholkner 0f3a0f7 
aholkner afab292 
aholkner 0f3a0f7 
aholkner afab292 


aholkner f6c1a23 

aholkner afab292 

aholkner f6c1a23 

aholkner 0f3a0f7 

aholkner afab292 










aholkner 0f3a0f7 
aholkner afab292 
aholkner 0f3a0f7 
aholkner afab292 

aholkner 0f3a0f7 
aholkner afab292 

aholkner 0f3a0f7 
aholkner afab292 







aholkner 0f3a0f7 
aholkner afab292 








aholkner 0f3a0f7 
aholkner afab292 
aholkner 0f3a0f7 
aholkner afab292 

aholkner 0f3a0f7 
aholkner afab292 










aholkner 0f3a0f7 
aholkner afab292 
aholkner 0f3a0f7 
aholkner afab292 

aholkner 0f3a0f7 
aholkner afab292 
























aholkner 0f3a0f7 
aholkner afab292 
aholkner 0f3a0f7 
aholkner afab292 

aholkner 0f3a0f7 
aholkner afab292 
















aholkner 0305db9 




aholkner afab292 
aholkner 0f3a0f7 
aholkner afab292 



aholkner 0f3a0f7 


aholkner afab292 
aholkner 9754ddb 




aholkner afab292 




aholkner 0f3a0f7 
aholkner afab292 






























aholkner 0f3a0f7 



aholkner afab292 


aholkner b2fb6e0 
aholkner f6c1a23 




  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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python

'''Pygame object for storing rectangular coordinates.
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

import copy

import SDL.video

class _RectProxy(object):
    '''Proxy for SDL_Rect that can handle negative size.'''

    __slots__ = ['x', 'y', 'w', 'h']

    def __init__(self, r):
        if isinstance(r, SDL.SDL_Rect) or isinstance(r, Rect):
            self.x = r.x
            self.y = r.y
            self.w = r.w
            self.h = r.h
        else:
            self.x = r[0]
            self.y = r[1]
            self.w = r[2]
            self.h = r[3]

    def _get_as_parameter_(self):
        return SDL.SDL_Rect(self.x, self.y, self.w, self.h)

    _as_parameter_ = property(_get_as_parameter_)

class Rect(object):
    __slots__ = ['_r']

    def __init__(self, *args):
        if len(args) == 1:
            arg = args[0]
            if isinstance(arg, Rect):
                object.__setattr__(self, '_r', copy.copy(arg._r))
                return
            elif isinstance(arg, SDL.SDL_Rect):
                object.__setattr__(self, '_r', copy.copy(arg))
                return
            elif hasattr(arg, 'rect'):
                arg = arg.rect
                if callable(arg):
                    arg = arg()
                self.__init__(arg)
                return
            elif hasattr(arg, '__len__'):
                args = arg
            else:
                raise TypeError, 'Argument must be rect style object'
        if len(args) == 4:
            if args[2] < 0 or args[3] < 0:
                object.__setattr__(self, '_r', _RectProxy((int(args[0]),
                                                          int(args[1]),
                                                          int(args[2]),
                                                          int(args[3]))))
            else:
                object.__setattr__(self, '_r', SDL.SDL_Rect(int(args[0]),
                                                            int(args[1]),
                                                            int(args[2]),
                                                            int(args[3])))
        elif len(args) == 2 and len(args[0]) == 2 and len(args[1]) == 2:
            if args[1][0] < 0 or args[1][1] < 0:
                object.__setattr__(self, '_r', 
                                   _RectProxy((int(args[0][0]), 
                                              int(args[0][1]), 
                                              int(args[1][0]), 
                                              int(args[1][1]))))
            else:
                object.__setattr__(self, '_r', 
                                   SDL.SDL_Rect(int(args[0][0]), 
                                                int(args[0][1]), 
                                                int(args[1][0]), 
                                                int(args[1][1])))
        else:
            raise TypeError, 'Argument must be rect style object'

    def __copy__(self):
        return Rect(self)

    def __repr__(self):
        return '<rect(%d, %d, %d, %d)>' % \
            (self._r.x, self._r.y, self._r.w, self._r.h)

    def __cmp__(self, *other):
        other = _rect_from_object(other)

        if self._r.x != other._r.x:
            return cmp(self._r.x, other._r.x)
        if self._r.y != other._r.y:
            return cmp(self._r.y, other._r.y)
        if self._r.w != other._r.w:
            return cmp(self._r.w, other._r.w)
        if self._r.h != other._r.h:
            return cmp(self._r.h, other._r.h)
        return 0

    def __nonzero__(self):
        return self._r.w != 0 and self._r.h != 0

    def __getattr__(self, name):
        if name in ('top', 'y'):
            return self._r.y
        elif name in ('left', 'x'):
            return self._r.x
        elif name == 'bottom':
            return self._r.y + self._r.h
        elif name == 'right':
            return self._r.x + self._r.w
        elif name == 'topleft':
            return self._r.x, self._r.y
        elif name == 'bottomleft':
            return self._r.x, self._r.y + self._r.h
        elif name == 'topright':
            return self._r.x + self._r.w, self._r.y
        elif name == 'bottomright':
            return self._r.x + self._r.w, self._r.y + self._r.h
        elif name == 'midtop':
            return self._r.x + self._r.w / 2, self._r.y
        elif name == 'midleft':
            return self._r.x, self._r.y + self._r.h / 2
        elif name == 'midbottom':
            return self._r.x + self._r.w / 2, self._r.y + self._r.h
        elif name == 'midright':
            return self._r.x + self._r.w, self._r.y + self._r.h / 2
        elif name == 'center':
            return self._r.x + self._r.w / 2, self._r.y + self._r.h / 2
        elif name == 'centerx':
            return self._r.x + self._r.w / 2
        elif name == 'centery':
            return self._r.y + self._r.h / 2
        elif name == 'size':
            return self._r.w, self._r.h
        elif name == 'width':
            return self._r.w
        elif name == 'height':
            return self._r.h
        else:
            raise AttributeError, name

    def __setattr__(self, name, value):
        if name == 'top' or name == 'y':
            self._r.y = int(value)
        elif name == 'left' or name == 'x':
            self._r.x = int(value)
        elif name == 'bottom':
            self._r.y = int(value) - self._r.h
        elif name == 'right':
            self._r.x = int(value) - self._r.w
        elif name == 'topleft':
            self._r.x, self._r.y = int(value[0]), int(value[1])
        elif name == 'bottomleft':
            self._r.x = int(value[0])
            self._r.y = int(value[1]) - self._r.h
        elif name == 'topright':
            self._r.x = int(value[0]) - self._r.w
            self._r.y = int(value[1])
        elif name == 'bottomright':
            self._r.x = int(value[0]) - self._r.w
            self._r.y = int(value[1]) - self._r.h
        elif name == 'midtop':
            self._r.x = int(value[0]) - self._r.w / 2
            self._r.y = int(value[1])
        elif name == 'midleft':
            self._r.x = int(value[0])
            self._r.y = int(value[1]) - self._r.h / 2
        elif name == 'midbottom':
            self._r.x = int(value[0]) - self._r.w / 2
            self._r.y = int(value[1]) - self._r.h
        elif name == 'midright':
            self._r.x = int(value[0]) - self._r.w
            self._r.y = int(value[1]) - self._r.h / 2
        elif name == 'center':
            self._r.x = int(value[0]) - self._r.w / 2
            self._r.y = int(value[1]) - self._r.h / 2
        elif name == 'centerx':
            self._r.x = int(value) - self._r.w / 2
        elif name == 'centery':
            self._r.y = int(value) - self._r.h / 2
        elif name == 'size':
            if int(value[0]) < 0 or int(value[1]) < 0:
                self._ensure_proxy()
            self._r.w, self._r.h = int(value)
        elif name == 'width':
            if int(value) < 0:
                self._ensure_proxy()
            self._r.w = int(value)
        elif name == 'height':
            if int(value) < 0:
                self._ensure_proxy()
            self._r.h = int(value)
        else:
            raise AttributeError, name

    def _ensure_proxy(self):
        if not isinstance(self._r, _RectProxy):
            object.__setattr__(self, '_r', _RectProxy(self._r))

    def __len__(self):
        return 4

    def __getitem__(self, key):
        return (self._r.x, self._r.y, self._r.w, self._r.h)[key]

    def __setitem__(self, key, value):
        r = [self._r.x, self._r.y, self._r.w, self._r.h]
        r[key] = value
        self._r.x, self._r.y, self._r.w, self._r.h = r

    def __coerce__(self, *other):
        try:
            return self, Rect(*other)
        except TypeError:
            return None

    def move(self, *pos):
        x, y = _two_ints_from_args(pos)
        return Rect(self._r.x + x, self._r.y + y, self._r.w, self._r.h)

    def move_ip(self, *pos):
        x, y = _two_ints_from_args(pos)
        self._r.x += x
        self._r.y += y

    def inflate(self, x, y):
        return Rect(self._r.x - x / 2, self._r.y - y / 2, 
                    self._r.w + x, self._r.h + y)

    def inflate_ip(self, x, y):
        self._r.x -= x / 2
        self._r.y -= y / 2
        self._r.w += x
        self._r.h += y

    def clamp(self, *other):
        r = Rect(self)
        r.clamp_ip(*other)
        return r

    def clamp_ip(self, *other):
        other = _rect_from_object(other)._r
        if self._r.w >= other.w:
            x = other.x + other.w / 2 - self._r.w / 2
        elif self._r.x < other.x:
            x = other.x
        elif self._r.x + self._r.w > other.x + other.w:
            x = other.x + other.w - self._r.w
        else:
            x = self._r.x

        if self._r.h >= other.h:
            y = other.y + other.h / 2 - self._r.h / 2
        elif self._r.y < other.y:
            y = other.y
        elif self._r.y + self._r.h > other.y + other.h:
            y = other.y + other.h - self._r.h
        else:
            y = self._r.y

        self._r.x, self._r.y = x, y

    def clip(self, *other):
        r = Rect(self)
        r.clip_ip(*other)
        return r

    def clip_ip(self, *other):
        other = _rect_from_object(other)._r
        x = max(self._r.x, other.x)
        w = min(self._r.x + self._r.w, other.x + other.w) - x
        y = max(self._r.y, other.y)
        h = min(self._r.y + self._r.h, other.y + other.h) - y

        if w <= 0 or h <= 0:
            self._r.w, self._r.h = 0, 0
        else:
            self._r.x, self._r.y, self._r.w, self._r.h = x, y, w, h

    def union(self, *other):
        r = Rect(self)
        r.union_ip(*other)
        return r

    def union_ip(self, *other):
        other = _rect_from_object(other)._r
        x = min(self._r.x, other.x)
        y = min(self._r.y, other.y)
        w = max(self._r.x + self._r.w, other.x + other.w) - x
        h = max(self._r.y + self._r.h, other.y + other.h) - y
        self._r.x, self._r.y, self._r.w, self._r.h = x, y, w, h

    def unionall(self, others):
        r = Rect(self)
        r.unionall_ip(others)
        return r

    def unionall_ip(self, others):
        l = self._r.x
        r = self._r.x + self._r.w
        t = self._r.y
        b = self._r.y + self._r.h
        for other in others:
            other = _rect_from_object(other)._r
            l = min(l, other.x)
            r = max(r, other.x + other.w)
            t = min(t, other.y)
            b = max(b, other.y + other.h)
        self._r.x, self._r.y, self._r.w, self._r.h = l, t, r - l, b - t

    def fit(self, *other):
        r = Rect(self)
        r.fit_ip(*other)
        return r
    
    def fit_ip(self, *other):
        other = _rect_from_object(other)._r

        xratio = self._r.w / float(other.w)
        yratio = self._r.h / float(other.h)
        maxratio = max(xratio, yratio)
        self._r.w = int(self._r.w / maxratio)
        self._r.h = int(self._r.h / maxratio)
        self._r.x = other.x + (other.w - self._r.w) / 2
        self._r.y = other.y + (other.h - self._r.h) / 2

    def normalize(self):
        if self._r.w < 0:
            self._r.x += self._r.w
            self._r.w = -self._r.w
        if self._r.h < 0:
            self._r.y += self._r.h
            self._r.h = -self._r.h
        if isinstance(self._r, _RectProxy):
            object.__setattr__(self, '_r', SDL.SDL_Rect(self._r.x,
                                                        self._r.y,
                                                        self._r.w,
                                                        self._r.h))

    def contains(self, *other):
        other = _rect_from_object(other)._r
        return self._r.x <= other.x and \
               self._r.y <= other.y and \
               self._r.x + self._r.w >= other.x + other.w and \
               self._r.y + self._r.h >= other.y + other.h and \
               self._r.x + self._r.w > other.x and \
               self._r.y + self._r.h > other.y

    def collidepoint(self, *args):
        if len(args) == 1:
            x, y = args[0]
        else:
            x, y = args
        return x >= self._r.x and \
               y >= self._r.y and \
               x < self._r.x + self._r.w and \
               y < self._r.y + self._r.h
    
    def colliderect(self, *other):
        return _rect_collide(self._r, _rect_from_object(other)._r)
        
    def collidelist(self, others):
        for i in range(len(others)):
            if _rect_collide(self._r, _rect_from_object(others[i])._r):
                return i
        return -1

    def collidelistall(self, others):
        matches = []
        for i in range(len(others)):
            if _rect_collide(self._r, _rect_from_object(others[i])._r):
                matches.append(i)
        return matches

    def collidedict(self, d):
        for key, other in d.items():
            if _rect_collide(self._r, _rect_from_object(other)._r):
                return key, other
        return None

    def collidedictall(self, d):
        matches = []
        for key, other in d.items():
            if _rect_collide(self._r, _rect_from_object(other)._r):
                matches.append((key, other))
        return matches

def _rect_from_object(obj):
    if isinstance(obj, Rect):
        return obj
    if type(obj) in (tuple, list):
        return Rect(*obj)
    else:
        return Rect(obj)

def _rect_collide(a, b):
    return a.x + a.w > b.x and b.x + b.w > a.x and \
           a.y + a.h > b.y and b.y + b.h > a.y

def _two_ints_from_args(arg):
    if len(arg) == 1:
        return _two_ints_from_args(arg[0])
    else:
        return arg[:2]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.