This repository has been archived by the owner on Dec 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgfx.py
229 lines (217 loc) · 8.29 KB
/
gfx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Port of Adafruit GFX Arduino library to MicroPython.
# Based on: https://github.com/adafruit/Adafruit-GFX-Library
# Author: Tony DiCola (original GFX author Phil Burgess)
# License: MIT License (https://opensource.org/licenses/MIT)
class GFX:
def __init__(self, width, height, pixel, hline=None, vline=None):
# Create an instance of the GFX drawing class. You must pass in the
# following parameters:
# - width = The width of the drawing area in pixels.
# - height = The height of the drawing area in pixels.
# - pixel = A function to call when a pixel is drawn on the display.
# This function should take at least an x and y position
# and then any number of optional color or other parameters.
# You can also provide the following optional keyword argument to
# improve the performance of drawing:
# - hline = A function to quickly draw a horizontal line on the display.
# This should take at least an x, y, and width parameter and
# any number of optional color or other parameters.
# - vline = A function to quickly draw a vertical line on the display.
# This should take at least an x, y, and height paraemter and
# any number of optional color or other parameters.
self.width = width
self.height = height
self._pixel = pixel
# Default to slow horizontal & vertical line implementations if no
# faster versions are provided.
if hline is None:
self.hline = self._slow_hline
else:
self.hline = hline
if vline is None:
self.vline = self._slow_vline
else:
self.vline = vline
def _slow_hline(self, x0, y0, width, *args, **kwargs):
# Slow implementation of a horizontal line using pixel drawing.
# This is used as the default horizontal line if no faster override
# is provided.
if y0 < 0 or y0 > self.height or x0 < -width or x0 > self.width:
return
for i in range(width):
self._pixel(x0+i, y0, *args, **kwargs)
def _slow_vline(self, x0, y0, height, *args, **kwargs):
# Slow implementation of a vertical line using pixel drawing.
# This is used as the default vertical line if no faster override
# is provided.
if y0 < -height or y0 > self.height or x0 < 0 or x0 > self.width:
return
for i in range(height):
self._pixel(x0, y0+i, *args, **kwargs)
def rect(self, x0, y0, width, height, *args, **kwargs):
# Rectangle drawing function. Will draw a single pixel wide rectangle
# starting in the upper left x0, y0 position and width, height pixels in
# size.
if y0 < -height or y0 > self.height or x0 < -width or x0 > self.width:
return
self.hline(x0, y0, width, *args, **kwargs)
self.hline(x0, y0+height-1, width, *args, **kwargs)
self.vline(x0, y0, height, *args, **kwargs)
self.vline(x0+width-1, y0, height, *args, **kwargs)
def fill_rect(self, x0, y0, width, height, *args, **kwargs):
# Filled rectangle drawing function. Will draw a single pixel wide
# rectangle starting in the upper left x0, y0 position and width, height
# pixels in size.
if y0 < -height or y0 > self.height or x0 < -width or x0 > self.width:
return
for i in range(x0, x0+width):
self.vline(i, y0, height, *args, **kwargs)
def line(self, x0, y0, x1, y1, *args, **kwargs):
# Line drawing function. Will draw a single pixel wide line starting at
# x0, y0 and ending at x1, y1.
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx = x1 - x0
dy = abs(y1 - y0)
err = dx // 2
ystep = 0
if y0 < y1:
ystep = 1
else:
ystep = -1
while x0 <= x1:
if steep:
self._pixel(y0, x0, *args, **kwargs)
else:
self._pixel(x0, y0, *args, **kwargs)
err -= dy
if err < 0:
y0 += ystep
err += dx
x0 += 1
def circle(self, x0, y0, radius, *args, **kwargs):
# Circle drawing function. Will draw a single pixel wide circle with
# center at x0, y0 and the specified radius.
f = 1 - radius
ddF_x = 1
ddF_y = -2 * radius
x = 0
y = radius
self._pixel(x0, y0 + radius, *args, **kwargs)
self._pixel(x0, y0 - radius, *args, **kwargs)
self._pixel(x0 + radius, y0, *args, **kwargs)
self._pixel(x0 - radius, y0, *args, **kwargs)
while x < y:
if f >= 0:
y -= 1
ddF_y += 2
f += ddF_y
x += 1
ddF_x += 2
f += ddF_x
self._pixel(x0 + x, y0 + y, *args, **kwargs)
self._pixel(x0 - x, y0 + y, *args, **kwargs)
self._pixel(x0 + x, y0 - y, *args, **kwargs)
self._pixel(x0 - x, y0 - y, *args, **kwargs)
self._pixel(x0 + y, y0 + x, *args, **kwargs)
self._pixel(x0 - y, y0 + x, *args, **kwargs)
self._pixel(x0 + y, y0 - x, *args, **kwargs)
self._pixel(x0 - y, y0 - x, *args, **kwargs)
def fill_circle(self, x0, y0, radius, *args, **kwargs):
# Filled circle drawing function. Will draw a filled circule with
# center at x0, y0 and the specified radius.
self.vline(x0, y0 - radius, 2*radius + 1, *args, **kwargs)
f = 1 - radius
ddF_x = 1
ddF_y = -2 * radius
x = 0
y = radius
while x < y:
if f >= 0:
y -= 1
ddF_y += 2
f += ddF_y
x += 1
ddF_x += 2
f += ddF_x
self.vline(x0 + x, y0 - y, 2*y + 1, *args, **kwargs)
self.vline(x0 + y, y0 - x, 2*x + 1, *args, **kwargs)
self.vline(x0 - x, y0 - y, 2*y + 1, *args, **kwargs)
self.vline(x0 - y, y0 - x, 2*x + 1, *args, **kwargs)
def triangle(self, x0, y0, x1, y1, x2, y2, *args, **kwargs):
# Triangle drawing function. Will draw a single pixel wide triangle
# around the points (x0, y0), (x1, y1), and (x2, y2).
self.line(x0, y0, x1, y1, *args, **kwargs)
self.line(x1, y1, x2, y2, *args, **kwargs)
self.line(x2, y2, x0, y0, *args, **kwargs)
def fill_triangle(self, x0, y0, x1, y1, x2, y2, *args, **kwargs):
# Filled triangle drawing function. Will draw a filled triangle around
# the points (x0, y0), (x1, y1), and (x2, y2).
if y0 > y1:
y0, y1 = y1, y0
x0, x1 = x1, x0
if y1 > y2:
y2, y1 = y1, y2
x2, x1 = x1, x2
if y0 > y1:
y0, y1 = y1, y0
x0, x1 = x1, x0
a = 0
b = 0
y = 0
last = 0
if y0 == y2:
a = x0
b = x0
if x1 < a:
a = x1
elif x1 > b:
b = x1
if x2 < a:
a = x2
elif x2 > b:
b = x2
self.hline(a, y0, b-a+1, *args, **kwargs)
return
dx01 = x1 - x0
dy01 = y1 - y0
dx02 = x2 - x0
dy02 = y2 - y0
dx12 = x2 - x1
dy12 = y2 - y1
if dy01 == 0:
dy01 = 1
if dy02 == 0:
dy02 = 1
if dy12 == 0:
dy12 = 1
sa = 0
sb = 0
if y1 == y2:
last = y1
else:
last = y1-1
for y in range(y0, last+1):
a = x0 + sa // dy01
b = x0 + sb // dy02
sa += dx01
sb += dx02
if a > b:
a, b = b, a
self.hline(a, y, b-a+1, *args, **kwargs)
sa = dx12 * (y - y1)
sb = dx02 * (y - y0)
while y <= y2:
a = x1 + sa // dy12
b = x0 + sb // dy02
sa += dx12
sb += dx02
if a > b:
a, b = b, a
self.hline(a, y, b-a+1, *args, **kwargs)
y += 1