Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 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 | # |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 20 | def is_empty(r): |
| 21 | (left, top), (right, bottom) = r |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 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:]: |
Guido van Rossum | 124eb94 | 1990-10-26 13:44:32 +0000 | [diff] [blame] | 33 | if is_empty(rect): |
| 34 | return empty |
| 35 | (l, t), (r, b) = rect |
| 36 | if left < l: left = l |
| 37 | if top < t: top = t |
| 38 | if right > r: right = r |
| 39 | if bottom > b: bottom = b |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 40 | if is_empty(((left, top), (right, bottom))): |
Guido van Rossum | 124eb94 | 1990-10-26 13:44:32 +0000 | [diff] [blame] | 41 | return empty |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 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): |
| 49 | (left, top), (right, bottom) = empty |
| 50 | for (l, t), (r, b) in list[1:]: |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 51 | if not is_empty(((l, t), (r, b))): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 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) |