Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-stdwin/rect.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 4192e24ec562 |
| children |
| rev | line source |
|---|---|
| 5 | 1 # Module 'rect'. |
| 2 # | |
| 3 # Operations on rectangles. | |
| 4 # There is some normalization: all results return the object 'empty' | |
| 5 # if their result would contain no points. | |
| 6 | |
| 7 | |
| 8 # Exception. | |
| 9 # | |
| 10 error = 'rect.error' | |
| 11 | |
| 12 | |
| 13 # The empty rectangle. | |
| 14 # | |
| 15 empty = (0, 0), (0, 0) | |
| 16 | |
| 17 | |
| 18 # Check if a rectangle is empty. | |
| 19 # | |
|
1260
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
34
diff
changeset
|
20 def is_empty(r): |
|
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
34
diff
changeset
|
21 (left, top), (right, bottom) = r |
| 5 | 22 return left >= right or top >= bottom |
| 23 | |
| 24 | |
| 25 # Compute the intersection or two or more rectangles. | |
| 26 # This works with a list or tuple argument. | |
| 27 # | |
| 28 def intersect(list): | |
| 29 if not list: raise error, 'intersect called with empty list' | |
| 30 if is_empty(list[0]): return empty | |
| 31 (left, top), (right, bottom) = list[0] | |
| 32 for rect in list[1:]: | |
|
34
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
33 if is_empty(rect): |
|
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
34 return empty |
|
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
35 (l, t), (r, b) = rect |
|
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
36 if left < l: left = l |
|
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
37 if top < t: top = t |
|
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
38 if right > r: right = r |
|
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
39 if bottom > b: bottom = b |
|
1260
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
34
diff
changeset
|
40 if is_empty(((left, top), (right, bottom))): |
|
34
1d22900f77b8
Fix bug in rect.intersect(): empty rects beyond the first were
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
41 return empty |
| 5 | 42 return (left, top), (right, bottom) |
| 43 | |
| 44 | |
| 45 # Compute the smallest rectangle containing all given rectangles. | |
| 46 # This works with a list or tuple argument. | |
| 47 # | |
| 48 def union(list): | |
|
1335
4192e24ec562
XEvent.py: Added support for ExposeEvent.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1260
diff
changeset
|
49 (left, top), (right, bottom) = list[0] |
| 5 | 50 for (l, t), (r, b) in list[1:]: |
|
1260
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
34
diff
changeset
|
51 if not is_empty(((l, t), (r, b))): |
| 5 | 52 if l < left: left = l |
| 53 if t < top: top = t | |
| 54 if r > right: right = r | |
| 55 if b > bottom: bottom = b | |
| 56 res = (left, top), (right, bottom) | |
| 57 if is_empty(res): | |
| 58 return empty | |
| 59 return res | |
| 60 | |
| 61 | |
| 62 # Check if a point is in a rectangle. | |
| 63 # | |
| 64 def pointinrect((h, v), ((left, top), (right, bottom))): | |
| 65 return left <= h < right and top <= v < bottom | |
| 66 | |
| 67 | |
| 68 # Return a rectangle that is dh, dv inside another | |
| 69 # | |
| 70 def inset(((left, top), (right, bottom)), (dh, dv)): | |
| 71 left = left + dh | |
| 72 top = top + dv | |
| 73 right = right - dh | |
| 74 bottom = bottom - dv | |
| 75 r = (left, top), (right, bottom) | |
| 76 if is_empty(r): | |
| 77 return empty | |
| 78 else: | |
| 79 return r | |
| 80 | |
| 81 | |
| 82 # Conversions between rectangles and 'geometry tuples', | |
| 83 # given as origin (h, v) and dimensions (width, height). | |
| 84 # | |
| 85 def rect2geom((left, top), (right, bottom)): | |
| 86 return (left, top), (right-left, bottom-top) | |
| 87 | |
| 88 def geom2rect((h, v), (width, height)): | |
| 89 return (h, v), (h+width, v+height) |
