|
| 1 | +import sys |
| 2 | + |
| 3 | +try: |
| 4 | + import framebuf |
| 5 | + from array import array |
| 6 | +except ImportError: |
| 7 | + print("SKIP") |
| 8 | + raise SystemExit |
| 9 | + |
| 10 | + |
| 11 | +# TODO: poly needs functions that aren't in dynruntime.h yet. |
| 12 | +if not hasattr(framebuf.FrameBuffer, "poly"): |
| 13 | + print("SKIP") |
| 14 | + raise SystemExit |
| 15 | + |
| 16 | + |
| 17 | +def print_buffer(buffer, width, height): |
| 18 | + for row in range(height): |
| 19 | + for col in range(width): |
| 20 | + val = buffer[(row * width) + col] |
| 21 | + sys.stdout.write(" {:02x}".format(val) if val else " ··") |
| 22 | + sys.stdout.write("\n") |
| 23 | + |
| 24 | + |
| 25 | +buf = bytearray(70 * 70) |
| 26 | + |
| 27 | +w = 30 |
| 28 | +h = 25 |
| 29 | +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8) |
| 30 | +col = 0xFF |
| 31 | +col_fill = 0x99 |
| 32 | + |
| 33 | +# This describes a arbitrary polygon (this happens to be a concave polygon in |
| 34 | +# the shape of an upper-case letter 'M'). |
| 35 | +poly = array( |
| 36 | + "h", |
| 37 | + ( |
| 38 | + 0, |
| 39 | + 20, |
| 40 | + 3, |
| 41 | + 20, |
| 42 | + 3, |
| 43 | + 10, |
| 44 | + 6, |
| 45 | + 17, |
| 46 | + 9, |
| 47 | + 10, |
| 48 | + 9, |
| 49 | + 20, |
| 50 | + 12, |
| 51 | + 20, |
| 52 | + 12, |
| 53 | + 3, |
| 54 | + 9, |
| 55 | + 3, |
| 56 | + 6, |
| 57 | + 10, |
| 58 | + 3, |
| 59 | + 3, |
| 60 | + 0, |
| 61 | + 3, |
| 62 | + ), |
| 63 | +) |
| 64 | +# This describes the same polygon, but the points are in reverse order |
| 65 | +# (it shouldn't matter if the polygon has clockwise or anti-clockwise |
| 66 | +# winding). Also defined as a bytes instead of array. |
| 67 | +poly_reversed = bytes( |
| 68 | + ( |
| 69 | + 0, |
| 70 | + 3, |
| 71 | + 3, |
| 72 | + 3, |
| 73 | + 6, |
| 74 | + 10, |
| 75 | + 9, |
| 76 | + 3, |
| 77 | + 12, |
| 78 | + 3, |
| 79 | + 12, |
| 80 | + 20, |
| 81 | + 9, |
| 82 | + 20, |
| 83 | + 9, |
| 84 | + 10, |
| 85 | + 6, |
| 86 | + 17, |
| 87 | + 3, |
| 88 | + 10, |
| 89 | + 3, |
| 90 | + 20, |
| 91 | + 0, |
| 92 | + 20, |
| 93 | + ) |
| 94 | +) |
| 95 | + |
| 96 | +# Draw the line polygon (at the origin) and the reversed-order polygon (offset). |
| 97 | +fbuf.fill(0) |
| 98 | +fbuf.poly(0, 0, poly, col) |
| 99 | +fbuf.poly(15, -2, poly_reversed, col) |
| 100 | +print_buffer(buf, w, h) |
| 101 | +print() |
| 102 | + |
| 103 | +# Same but filled. |
| 104 | +fbuf.fill(0) |
| 105 | +fbuf.poly(0, 0, poly, col_fill, True) |
| 106 | +fbuf.poly(15, -2, poly_reversed, col_fill, True) |
| 107 | +print_buffer(buf, w, h) |
| 108 | +print() |
| 109 | + |
| 110 | +# Draw the fill then the outline to ensure that no fill goes outside the outline. |
| 111 | +fbuf.fill(0) |
| 112 | +fbuf.poly(0, 0, poly, col_fill, True) |
| 113 | +fbuf.poly(0, 0, poly, col) |
| 114 | +fbuf.poly(15, -2, poly, col_fill, True) |
| 115 | +fbuf.poly(15, -2, poly, col) |
| 116 | +print_buffer(buf, w, h) |
| 117 | +print() |
| 118 | + |
| 119 | +# Draw the outline then the fill to ensure the fill completely covers the outline. |
| 120 | +fbuf.fill(0) |
| 121 | +fbuf.poly(0, 0, poly, col) |
| 122 | +fbuf.poly(0, 0, poly, col_fill, True) |
| 123 | +fbuf.poly(15, -2, poly, col) |
| 124 | +fbuf.poly(15, -2, poly, col_fill, True) |
| 125 | +print_buffer(buf, w, h) |
| 126 | +print() |
| 127 | + |
| 128 | +# Draw polygons that will go out of bounds at each of the edges. |
| 129 | +for x, y in ( |
| 130 | + ( |
| 131 | + -8, |
| 132 | + -8, |
| 133 | + ), |
| 134 | + ( |
| 135 | + 24, |
| 136 | + -6, |
| 137 | + ), |
| 138 | + ( |
| 139 | + 20, |
| 140 | + 12, |
| 141 | + ), |
| 142 | + ( |
| 143 | + -2, |
| 144 | + 10, |
| 145 | + ), |
| 146 | +): |
| 147 | + fbuf.fill(0) |
| 148 | + fbuf.poly(x, y, poly, col) |
| 149 | + print_buffer(buf, w, h) |
| 150 | + print() |
| 151 | + fbuf.fill(0) |
| 152 | + fbuf.poly(x, y, poly_reversed, col, True) |
| 153 | + print_buffer(buf, w, h) |
| 154 | + print() |
| 155 | + |
| 156 | +# Edge cases: These two lists describe self-intersecting polygons |
| 157 | +poly_hourglass = array("h", (0, 0, 9, 0, 0, 19, 9, 19)) |
| 158 | +poly_star = array("h", (7, 0, 3, 18, 14, 5, 0, 5, 11, 18)) |
| 159 | + |
| 160 | +# As before, fill then outline. |
| 161 | +fbuf.fill(0) |
| 162 | +fbuf.poly(0, 2, poly_hourglass, col_fill, True) |
| 163 | +fbuf.poly(0, 2, poly_hourglass, col) |
| 164 | +fbuf.poly(12, 2, poly_star, col_fill, True) |
| 165 | +fbuf.poly(12, 2, poly_star, col) |
| 166 | +print_buffer(buf, w, h) |
| 167 | +print() |
| 168 | + |
| 169 | +# Outline then fill. |
| 170 | +fbuf.fill(0) |
| 171 | +fbuf.poly(0, 2, poly_hourglass, col) |
| 172 | +fbuf.poly(0, 2, poly_hourglass, col_fill, True) |
| 173 | +fbuf.poly(12, 2, poly_star, col) |
| 174 | +fbuf.poly(12, 2, poly_star, col_fill, True) |
| 175 | +print_buffer(buf, w, h) |
| 176 | +print() |
| 177 | + |
| 178 | +# Edge cases: These are "degenerate" polygons. |
| 179 | +poly_empty = array("h") # Will draw nothing at all. |
| 180 | +poly_one = array("h", (20, 20)) # Will draw a single point. |
| 181 | +poly_two = array("h", (10, 10, 5, 5)) # Will draw a single line. |
| 182 | +poly_wrong_length = array("h", (2, 2, 4)) # Will round down to one point. |
| 183 | + |
| 184 | +fbuf.fill(0) |
| 185 | +fbuf.poly(0, 0, poly_empty, col) |
| 186 | +fbuf.poly(0, 0, poly_one, col) |
| 187 | +fbuf.poly(0, 0, poly_two, col) |
| 188 | +fbuf.poly(0, 0, poly_wrong_length, col) |
| 189 | +print_buffer(buf, w, h) |
| 190 | +print() |
| 191 | + |
| 192 | +# A shape with a horizontal overhang. |
| 193 | +poly_overhang = array("h", (0, 0, 0, 5, 5, 5, 5, 10, 10, 10, 10, 0)) |
| 194 | + |
| 195 | +fbuf.fill(0) |
| 196 | +fbuf.poly(0, 0, poly_overhang, col) |
| 197 | +fbuf.poly(0, 0, poly_overhang, col_fill, True) |
| 198 | +print_buffer(buf, w, h) |
| 199 | +print() |
| 200 | + |
| 201 | +fbuf.fill(0) |
| 202 | +fbuf.poly(0, 0, poly_overhang, col_fill, True) |
| 203 | +fbuf.poly(0, 0, poly_overhang, col) |
| 204 | +print_buffer(buf, w, h) |
| 205 | +print() |
| 206 | + |
| 207 | +# Triangles |
| 208 | +w = 70 |
| 209 | +h = 70 |
| 210 | +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8) |
| 211 | +t1 = array("h", [40, 0, 20, 68, 62, 40]) |
| 212 | +t2 = array("h", [40, 0, 0, 16, 20, 68]) |
| 213 | + |
| 214 | +fbuf.fill(0) |
| 215 | +fbuf.poly(0, 0, t1, 0xFF, False) |
| 216 | +fbuf.poly(0, 0, t2, 0xFF, False) |
| 217 | +print_buffer(buf, w, h) |
| 218 | + |
| 219 | +fbuf.fill(0) |
| 220 | +fbuf.poly(0, 0, t1, 0xFF, True) |
| 221 | +fbuf.poly(0, 0, t2, 0xFF, True) |
| 222 | +print_buffer(buf, w, h) |
0 commit comments