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 |