|
| 1 | +import displayio |
| 2 | + |
| 3 | +class Circle(displayio.TileGrid): |
| 4 | + def __init__(self, x0, y0, r, *, fill=None, outline=None): |
| 5 | + self._bitmap = displayio.Bitmap(r*2+1, r*2+1, 3) |
| 6 | + self._palette = displayio.Palette(3) |
| 7 | + self._palette.make_transparent(0) |
| 8 | + |
| 9 | + # set position to top left corner |
| 10 | + position = (x0-r, y0-r) |
| 11 | + # and now set that position to be (0,0) |
| 12 | + x0 = y0 = r |
| 13 | + |
| 14 | + if fill is not None: |
| 15 | + for i in range(x0-r, x0+r): # draw the center line |
| 16 | + self._bitmap[i, r] = 2 |
| 17 | + # ask our helper to fill in the circle |
| 18 | + self._helper(x0, y0, r, 0xF, 2, fill=True) |
| 19 | + self._palette[2] = fill |
| 20 | + else: |
| 21 | + self._palette.make_transparent(2) |
| 22 | + |
| 23 | + if outline is not None: |
| 24 | + self._palette[1] = outline |
| 25 | + # draw outline |
| 26 | + self._bitmap[x0, y0+r] = 1 |
| 27 | + self._bitmap[x0, y0-r] = 1 |
| 28 | + self._bitmap[x0+r, y0] = 1 |
| 29 | + self._bitmap[x0-r, y0] = 1 |
| 30 | + |
| 31 | + self._helper(x0, y0, r, 0xF, 1) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + super().__init__(self._bitmap, pixel_shader=self._palette, position=position) |
| 38 | + |
| 39 | + def _helper(self, x0, y0, r, cornerflags, color, *, fill=False): |
| 40 | + f = 1 - r |
| 41 | + ddF_x = 1 |
| 42 | + ddF_y = -2 * r |
| 43 | + x = 0 |
| 44 | + y = r |
| 45 | + |
| 46 | + while x < y: |
| 47 | + if f >= 0: |
| 48 | + y -= 1 |
| 49 | + ddF_y += 2 |
| 50 | + f += ddF_y |
| 51 | + x += 1 |
| 52 | + ddF_x += 2 |
| 53 | + f += ddF_x |
| 54 | + if cornerflags & 0x8: |
| 55 | + if fill: |
| 56 | + for w in range(x0-y, x0+y): |
| 57 | + self._bitmap[w, y0+x] = color |
| 58 | + for w in range(x0-x, x0+x): |
| 59 | + self._bitmap[w, y0+y] = color |
| 60 | + else: |
| 61 | + self._bitmap[x0-y, y0+x] = color |
| 62 | + self._bitmap[x0-x, y0+y] = color |
| 63 | + if cornerflags & 0x1: |
| 64 | + if fill: |
| 65 | + for w in range(x0-y, x0+y): |
| 66 | + self._bitmap[w, y0-x] = color |
| 67 | + for w in range(x0-x, x0+x): |
| 68 | + self._bitmap[w, y0-y] = color |
| 69 | + else: |
| 70 | + self._bitmap[x0-y, y0-x] = color |
| 71 | + self._bitmap[x0-x, y0-y] = color |
| 72 | + if cornerflags & 0x4: |
| 73 | + self._bitmap[x0+x, y0+y] = color |
| 74 | + self._bitmap[x0+y, y0+x] = color |
| 75 | + if cornerflags & 0x2: |
| 76 | + self._bitmap[x0+x, y0-y] = color |
| 77 | + self._bitmap[x0+y, y0-x] = color |
| 78 | + |
| 79 | + @property |
| 80 | + def x(self): |
| 81 | + return self.position[0] |
| 82 | + |
| 83 | + @x.setter |
| 84 | + def x(self, x): |
| 85 | + self.position = (x, self.position[1]) |
| 86 | + |
| 87 | + @property |
| 88 | + def y(self): |
| 89 | + return self.position[1] |
| 90 | + |
| 91 | + @x.setter |
| 92 | + def y(self, y): |
| 93 | + self.position = (self.position[0], y) |
0 commit comments