Skip to content

Commit 1a4c7dc

Browse files
committed
circle isa roundrect
1 parent 1984725 commit 1a4c7dc

2 files changed

Lines changed: 16 additions & 104 deletions

File tree

adafruit_display_shapes/circle.py

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,6 @@
11
import displayio
2+
from adafruit_display_shapes.roundrect import RoundRect
23

3-
class Circle(displayio.TileGrid):
4+
class Circle(RoundRect):
45
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)
6+
super().__init__(x0-r, y0-r, 2*r+1, 2*r+1, r, fill=fill, outline=outline)

adafruit_display_shapes/roundrect.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ def __init__(self, x, y, width, height, r, *, fill=None, outline=None):
66
self._palette = displayio.Palette(3)
77
self._palette.make_transparent(0)
88

9-
"""
109
if fill is not None:
11-
#for i in range(x0-r, x0+r): # draw the center line
12-
# self._bitmap[i, r] = 2
13-
# ask our helper to fill in the circle
14-
self._helper(x0, y0, r, 0xF, 2, fill=True)
10+
for i in range(0, width): # draw the center chunk
11+
for j in range(r, height-r): # draw the center chunk
12+
self._bitmap[i, j] = 2
13+
self._helper(r, r, r, color=2, fill=True,
14+
x_offset=width-2*r-1, y_offset=height-2*r-1)
1515
self._palette[2] = fill
1616
else:
1717
self._palette.make_transparent(2)
18-
"""
1918

2019
if outline is not None:
2120
self._palette[1] = outline
@@ -27,11 +26,11 @@ def __init__(self, x, y, width, height, r, *, fill=None, outline=None):
2726
self._bitmap[0, h] = 1
2827
self._bitmap[width-1, h] = 1
2928
# draw round corners
30-
self._helper(r, r, r, x_offset=width-2*r-1, y_offset=height-2*r-1, cornerflags=0xF)
29+
self._helper(r, r, r, color=1, x_offset=width-2*r-1, y_offset=height-2*r-1)
3130

3231
super().__init__(self._bitmap, pixel_shader=self._palette, position=(x, y))
3332

34-
def _helper(self, x0, y0, r, *, x_offset=0, y_offset=0, cornerflags=0xF, color=1, fill=False):
33+
def _helper(self, x0, y0, r, *, color, x_offset=0, y_offset=0, cornerflags=0xF, fill=False):
3534
f = 1 - r
3635
ddF_x = 1
3736
ddF_y = -2 * r
@@ -48,18 +47,18 @@ def _helper(self, x0, y0, r, *, x_offset=0, y_offset=0, cornerflags=0xF, color=1
4847
f += ddF_x
4948
if cornerflags & 0x8:
5049
if fill:
51-
for w in range(x0-y, x0+y):
52-
self._bitmap[w, y0+x] = color
53-
for w in range(x0-x, x0+x):
54-
self._bitmap[w, y0+y] = color
50+
for w in range(x0-y, x0+y+x_offset):
51+
self._bitmap[w, y0+x+y_offset] = color
52+
for w in range(x0-x, x0+x+x_offset):
53+
self._bitmap[w, y0+y+y_offset] = color
5554
else:
5655
self._bitmap[x0-y, y0+x+y_offset] = color
5756
self._bitmap[x0-x, y0+y+y_offset] = color
5857
if cornerflags & 0x1:
5958
if fill:
60-
for w in range(x0-y, x0+y):
59+
for w in range(x0-y, x0+y+x_offset):
6160
self._bitmap[w, y0-x] = color
62-
for w in range(x0-x, x0+x):
61+
for w in range(x0-x, x0+x+x_offset):
6362
self._bitmap[w, y0-y] = color
6463
else:
6564
self._bitmap[x0-y, y0-x] = color

0 commit comments

Comments
 (0)