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