Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 1 | # |
jvr | fc3879e | 2003-08-29 19:29:46 +0000 | [diff] [blame] | 2 | # Various array and rectangle tools, but mostly rectangles, hence the |
| 3 | # name of this module (not). |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 4 | # |
| 5 | |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 6 | |
| 7 | import math |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 8 | |
| 9 | def calcBounds(array): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 10 | """Return the bounding rectangle of a 2D points array as a tuple: |
| 11 | (xMin, yMin, xMax, yMax) |
| 12 | """ |
| 13 | if len(array) == 0: |
| 14 | return 0, 0, 0, 0 |
| 15 | xs = [x for x, y in array] |
| 16 | ys = [y for x, y in array] |
| 17 | return min(xs), min(ys), max(xs), max(ys) |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 18 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 19 | def updateBounds(bounds, p, min=min, max=max): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 20 | """Return the bounding recangle of rectangle bounds and point (x, y).""" |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 21 | (x, y) = p |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 22 | xMin, yMin, xMax, yMax = bounds |
| 23 | return min(xMin, x), min(yMin, y), max(xMax, x), max(yMax, y) |
jvr | 182a7ba | 2003-06-29 18:18:54 +0000 | [diff] [blame] | 24 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 25 | def pointInRect(p, rect): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 26 | """Return True when point (x, y) is inside rect.""" |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 27 | (x, y) = p |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 28 | xMin, yMin, xMax, yMax = rect |
| 29 | return (xMin <= x <= xMax) and (yMin <= y <= yMax) |
jvr | 182a7ba | 2003-06-29 18:18:54 +0000 | [diff] [blame] | 30 | |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 31 | def pointsInRect(array, rect): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 32 | """Find out which points or array are inside rect. |
| 33 | Returns an array with a boolean for each point. |
| 34 | """ |
| 35 | if len(array) < 1: |
| 36 | return [] |
| 37 | xMin, yMin, xMax, yMax = rect |
| 38 | return [(xMin <= x <= xMax) and (yMin <= y <= yMax) for x, y in array] |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 39 | |
Just | deb3b63 | 2000-01-26 19:32:45 +0000 | [diff] [blame] | 40 | def vectorLength(vector): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 41 | """Return the length of the given vector.""" |
| 42 | x, y = vector |
| 43 | return math.sqrt(x**2 + y**2) |
Just | deb3b63 | 2000-01-26 19:32:45 +0000 | [diff] [blame] | 44 | |
| 45 | def asInt16(array): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 46 | """Round and cast to 16 bit integer.""" |
| 47 | return [int(math.floor(i+0.5)) for i in array] |
| 48 | |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 49 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 50 | def normRect(rect): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 51 | """Normalize the rectangle so that the following holds: |
| 52 | xMin <= xMax and yMin <= yMax |
| 53 | """ |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 54 | (xMin, yMin, xMax, yMax) = rect |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 55 | return min(xMin, xMax), min(yMin, yMax), max(xMin, xMax), max(yMin, yMax) |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 56 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 57 | def scaleRect(rect, x, y): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 58 | """Scale the rectangle by x, y.""" |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 59 | (xMin, yMin, xMax, yMax) = rect |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 60 | return xMin * x, yMin * y, xMax * x, yMax * y |
Just | 470b505 | 2000-01-16 20:37:11 +0000 | [diff] [blame] | 61 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 62 | def offsetRect(rect, dx, dy): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 63 | """Offset the rectangle by dx, dy.""" |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 64 | (xMin, yMin, xMax, yMax) = rect |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 65 | return xMin+dx, yMin+dy, xMax+dx, yMax+dy |
Just | be7163c | 2000-01-18 22:29:39 +0000 | [diff] [blame] | 66 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 67 | def insetRect(rect, dx, dy): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 68 | """Inset the rectangle by dx, dy on all sides.""" |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 69 | (xMin, yMin, xMax, yMax) = rect |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 70 | return xMin+dx, yMin+dy, xMax-dx, yMax-dy |
Just | be7163c | 2000-01-18 22:29:39 +0000 | [diff] [blame] | 71 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 72 | def sectRect(rect1, rect2): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 73 | """Return a boolean and a rectangle. If the input rectangles intersect, return |
| 74 | True and the intersecting rectangle. Return False and (0, 0, 0, 0) if the input |
| 75 | rectangles don't intersect. |
| 76 | """ |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 77 | (xMin1, yMin1, xMax1, yMax1) = rect1 |
| 78 | (xMin2, yMin2, xMax2, yMax2) = rect2 |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 79 | xMin, yMin, xMax, yMax = (max(xMin1, xMin2), max(yMin1, yMin2), |
| 80 | min(xMax1, xMax2), min(yMax1, yMax2)) |
| 81 | if xMin >= xMax or yMin >= yMax: |
| 82 | return 0, (0, 0, 0, 0) |
| 83 | return 1, (xMin, yMin, xMax, yMax) |
Just | be7163c | 2000-01-18 22:29:39 +0000 | [diff] [blame] | 84 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 85 | def unionRect(rect1, rect2): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 86 | """Return the smallest rectangle in which both input rectangles are fully |
| 87 | enclosed. In other words, return the total bounding rectangle of both input |
| 88 | rectangles. |
| 89 | """ |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 90 | (xMin1, yMin1, xMax1, yMax1) = rect1 |
| 91 | (xMin2, yMin2, xMax2, yMax2) = rect2 |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 92 | xMin, yMin, xMax, yMax = (min(xMin1, xMin2), min(yMin1, yMin2), |
| 93 | max(xMax1, xMax2), max(yMax1, yMax2)) |
| 94 | return (xMin, yMin, xMax, yMax) |
Just | 02a739a | 2000-01-23 19:10:27 +0000 | [diff] [blame] | 95 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 96 | def rectCenter(rect0): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 97 | """Return the center of the rectangle as an (x, y) coordinate.""" |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 98 | (xMin, yMin, xMax, yMax) = rect0 |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 99 | return (xMin+xMax)/2, (yMin+yMax)/2 |
Just | 02a739a | 2000-01-23 19:10:27 +0000 | [diff] [blame] | 100 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 101 | def intRect(rect1): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 102 | """Return the rectangle, rounded off to integer values, but guaranteeing that |
| 103 | the resulting rectangle is NOT smaller than the original. |
| 104 | """ |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame^] | 105 | (xMin, yMin, xMax, yMax) = rect1 |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 106 | import math |
| 107 | xMin = int(math.floor(xMin)) |
| 108 | yMin = int(math.floor(yMin)) |
| 109 | xMax = int(math.ceil(xMax)) |
| 110 | yMax = int(math.ceil(yMax)) |
| 111 | return (xMin, yMin, xMax, yMax) |
Just | be7163c | 2000-01-18 22:29:39 +0000 | [diff] [blame] | 112 | |
jvr | fc3879e | 2003-08-29 19:29:46 +0000 | [diff] [blame] | 113 | |
jvr | 41144b9 | 2008-03-04 14:47:11 +0000 | [diff] [blame] | 114 | def _test(): |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 115 | """ |
| 116 | >>> import math |
| 117 | >>> calcBounds([]) |
| 118 | (0, 0, 0, 0) |
| 119 | >>> calcBounds([(0, 40), (0, 100), (50, 50), (80, 10)]) |
| 120 | (0, 10, 80, 100) |
| 121 | >>> updateBounds((0, 0, 0, 0), (100, 100)) |
| 122 | (0, 0, 100, 100) |
| 123 | >>> pointInRect((50, 50), (0, 0, 100, 100)) |
| 124 | True |
| 125 | >>> pointInRect((0, 0), (0, 0, 100, 100)) |
| 126 | True |
| 127 | >>> pointInRect((100, 100), (0, 0, 100, 100)) |
| 128 | True |
| 129 | >>> not pointInRect((101, 100), (0, 0, 100, 100)) |
| 130 | True |
| 131 | >>> list(pointsInRect([(50, 50), (0, 0), (100, 100), (101, 100)], (0, 0, 100, 100))) |
| 132 | [True, True, True, False] |
| 133 | >>> vectorLength((3, 4)) |
| 134 | 5.0 |
| 135 | >>> vectorLength((1, 1)) == math.sqrt(2) |
| 136 | True |
| 137 | >>> list(asInt16([0, 0.1, 0.5, 0.9])) |
| 138 | [0, 0, 1, 1] |
| 139 | >>> normRect((0, 10, 100, 200)) |
| 140 | (0, 10, 100, 200) |
| 141 | >>> normRect((100, 200, 0, 10)) |
| 142 | (0, 10, 100, 200) |
| 143 | >>> scaleRect((10, 20, 50, 150), 1.5, 2) |
| 144 | (15.0, 40, 75.0, 300) |
| 145 | >>> offsetRect((10, 20, 30, 40), 5, 6) |
| 146 | (15, 26, 35, 46) |
| 147 | >>> insetRect((10, 20, 50, 60), 5, 10) |
| 148 | (15, 30, 45, 50) |
| 149 | >>> insetRect((10, 20, 50, 60), -5, -10) |
| 150 | (5, 10, 55, 70) |
| 151 | >>> intersects, rect = sectRect((0, 10, 20, 30), (0, 40, 20, 50)) |
| 152 | >>> not intersects |
| 153 | True |
| 154 | >>> intersects, rect = sectRect((0, 10, 20, 30), (5, 20, 35, 50)) |
| 155 | >>> intersects |
| 156 | 1 |
| 157 | >>> rect |
| 158 | (5, 20, 20, 30) |
| 159 | >>> unionRect((0, 10, 20, 30), (0, 40, 20, 50)) |
| 160 | (0, 10, 20, 50) |
| 161 | >>> rectCenter((0, 0, 100, 200)) |
| 162 | (50, 100) |
| 163 | >>> rectCenter((0, 0, 100, 199.0)) |
| 164 | (50, 99.5) |
| 165 | >>> intRect((0.9, 2.9, 3.1, 4.1)) |
| 166 | (0, 2, 4, 5) |
| 167 | """ |
jvr | 41144b9 | 2008-03-04 14:47:11 +0000 | [diff] [blame] | 168 | |
jvr | fc3879e | 2003-08-29 19:29:46 +0000 | [diff] [blame] | 169 | if __name__ == "__main__": |
jvr | 91bca42 | 2012-10-18 12:49:22 +0000 | [diff] [blame] | 170 | import doctest |
| 171 | doctest.testmod() |