Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 1 | # |
| 2 | # This module should move to a more appropriate location |
| 3 | # |
| 4 | |
| 5 | import Numeric |
| 6 | |
| 7 | |
| 8 | def calcBounds(array): |
| 9 | """Calculate the bounding rectangle of a 2D array. |
| 10 | Returns a 4-tuple: |
| 11 | smallest x, smallest y, largest x, largest y. |
| 12 | """ |
| 13 | if len(array) == 0: |
| 14 | return 0, 0, 0, 0 |
| 15 | xmin, ymin = Numeric.minimum.reduce(array) |
| 16 | xmax, ymax = Numeric.maximum.reduce(array) |
| 17 | return xmin, ymin, xmax, ymax |
| 18 | |
| 19 | |
| 20 | def pointsInRect(array, rect): |
| 21 | """Find out which points or array are inside rect. |
| 22 | Returns an array with a boolean for each point. |
| 23 | """ |
| 24 | if len(array) < 1: |
| 25 | return [] |
| 26 | lefttop = rect[:2] |
| 27 | rightbottom = rect[2:] |
| 28 | condition = Numeric.logical_and( |
| 29 | Numeric.greater(array, lefttop), |
| 30 | Numeric.less(array, rightbottom)) |
| 31 | return Numeric.logical_and.reduce(condition, -1) |
| 32 | |
| 33 | |
| 34 | def normRect((l, t, r, b)): |
| 35 | """XXX doc""" |
| 36 | return min(l, r), min(t, b), max(l, r), max(t, b) |
| 37 | |
| 38 | def scaleRect((l, t, r, b), x, y): |
| 39 | return l * x, t * y, r * x, b * y |
| 40 | |
| 41 | def offsetRect((l, t, r, b), dx, dy): |
| 42 | return l+dx, t+dy, r+dx, b+dy |
Just | be7163c | 2000-01-18 22:29:39 +0000 | [diff] [blame] | 43 | |
| 44 | def insetRect((l, t, r, b), dx, dy): |
| 45 | return l+dx, t+dy, r-dx, b-dy |
| 46 | |
| 47 | def sectRect((l1, t1, r1, b1), (l2, t2, r2, b2)): |
| 48 | l, t, r, b = max(l1, l2), max(t1, t2), min(r1, r2), min(b1, b2) |
| 49 | if l >= r or t >= b: |
| 50 | return 0, (0, 0, 0, 0) |
| 51 | return 1, (l, t, r, b) |
| 52 | |
Just | 02a739a | 2000-01-23 19:10:27 +0000 | [diff] [blame] | 53 | def unionRect((l1, t1, r1, b1), (l2, t2, r2, b2)): |
| 54 | l, t, r, b = min(l1, l2), min(t1, t2), max(r1, r2), max(b1, b2) |
| 55 | return (l, t, r, b) |
| 56 | |
| 57 | def rectCenter((l, t, r, b)): |
| 58 | return (l+r)/2, (t+b)/2 |
| 59 | |
Just | b3026ba | 2000-01-22 00:26:07 +0000 | [diff] [blame] | 60 | def intRect(rect): |
| 61 | rect = Numeric.array(rect) |
| 62 | l, t = Numeric.floor(rect[:2]) |
| 63 | r, b = Numeric.ceil(rect[2:]) |
| 64 | return tuple(Numeric.array((l, t, r, b)).astype(Numeric.Int)) |
Just | be7163c | 2000-01-18 22:29:39 +0000 | [diff] [blame] | 65 | |