blob: 9e8d59fce8cf09e63fc9476b1ae7fa9f49a835a7 [file] [log] [blame]
Just470b5052000-01-16 20:37:11 +00001#
2# This module should move to a more appropriate location
3#
4
5import Numeric
6
7
8def 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
20def 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
34def normRect((l, t, r, b)):
35 """XXX doc"""
36 return min(l, r), min(t, b), max(l, r), max(t, b)
37
38def scaleRect((l, t, r, b), x, y):
39 return l * x, t * y, r * x, b * y
40
41def offsetRect((l, t, r, b), dx, dy):
42 return l+dx, t+dy, r+dx, b+dy
Justbe7163c2000-01-18 22:29:39 +000043
44def insetRect((l, t, r, b), dx, dy):
45 return l+dx, t+dy, r-dx, b-dy
46
47def 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
Just02a739a2000-01-23 19:10:27 +000053def 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
57def rectCenter((l, t, r, b)):
58 return (l+r)/2, (t+b)/2
59
Justb3026ba2000-01-22 00:26:07 +000060def 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))
Justbe7163c2000-01-18 22:29:39 +000065