198 |
198 |
199 class Rect: |
199 class Rect: |
200 |
200 |
201 """Rectangle is defined by coordinates and size.""" |
201 """Rectangle is defined by coordinates and size.""" |
202 |
202 |
203 def __init__(self, x=0, y=0, w=0, h=0): |
203 def __init__(self, *args, **kwargs): |
204 self.x = x |
204 self.x = 0 |
205 self.y = y |
205 self.y = 0 |
206 self.w = w |
206 self.w = 0 |
207 self.h = h |
207 self.h = 0 |
208 |
208 if len(args) == 4: |
209 @classmethod |
209 self.x, self.y, self.w, self.h = args |
210 def _make(cls, origin, size): |
210 elif len(args) == 2: |
211 """Make new Rect instance with origin and size as specified. |
211 self.set_origin_and_size(*args) |
|
212 for key, val in kwargs.items(): |
|
213 setattr(self, key, val) |
|
214 |
|
215 def set_origin_and_size(self, origin, size): |
|
216 """Set rect origin and size as specified. |
212 |
217 |
213 `origin` should be Point or pair of coordinates, |
218 `origin` should be Point or pair of coordinates, |
214 `size` should be Size or pair of integers |
219 `size` should be Size or pair of integers |
215 |
220 |
216 """ |
221 """ |
217 x, y = origin |
222 self.x, self.y = origin |
218 w, h = size |
223 self.w, self.h = size |
219 return Rect(x, y, w, h) |
|
220 |
224 |
221 def __repr__(self): |
225 def __repr__(self): |
222 return '{0.__class__.__name__}(x={0.x}, y={0.y}, w={0.w}, h={0.h})'.format(self) |
226 return '{0.__class__.__name__}(x={0.x}, y={0.y}, w={0.w}, h={0.h})'.format(self) |
223 |
227 |
224 def __contains__(self, point): |
228 def __contains__(self, point): |