-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdrawing.py
More file actions
483 lines (416 loc) · 13.9 KB
/
Copy pathdrawing.py
File metadata and controls
483 lines (416 loc) · 13.9 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# encoding: utf-8
import unittest
from . import PlotDeviceTestCase, reference
from plotdevice import *
class DrawingTests(PlotDeviceTestCase):
@reference('drawing/paths-transform-pre.png')
def test_paths_transform_pre(self):
# tut/Bezier_Paths (1)
size(180, 180)
font("Dolly", "bold", 300)
path = textpath("e", 10, 150)
bezier(path, stroke='black', fill=None)
@reference('drawing/paths-transform-post.png')
def test_paths_transform_post(self):
# tut/Bezier_Paths (2)
size(180, 180)
font("Dolly", "bold", 300)
path = textpath("e", 10, 150)
curves = []
for curve in path:
curve.y += 20 # nudge the point downward
curves.append(curve)
# draw a new bezier (built from our list of Curve objects)
bezier(curves, stroke='black', fill=None)
@reference('drawing/paths-broken.jpg')
def test_paths_broken(self):
# tut/Bezier_Paths (3)
size(334, 87)
font("Dolly", "bold", 100)
path = textpath("broken", 0,80)
curves = []
for curve in path:
if curve.cmd == CURVETO:
curve.ctrl2.x += 5
curve.ctrl2.y -= 10
curve.y += 5
curves.append(curve)
with stroke(0), nofill():
bezier(curves)
@reference('drawing/pathmatics-contours.png')
def test_pathmatics_contours(self):
# tut/Bezier_Paths (4)
size(181, 70)
font("Dolly", "bold", 50)
with pen(2), nofill():
path = textpath("@#$&!", 10, 50)
for contour in path.contours:
stroke(HSV, random(), 1, .8)
bezier(contour)
@reference('drawing/paths-compound1.png')
def test_paths_compound1(self):
# tut/Bezier_Paths (5)
size(200, 270)
stroke(0, .3) # use a translucent black
pen(4, cap=ROUND)
# draw twelve overlapping lines separately
translate(5,5)
for i in range(12):
line(0, i*10, i*15, 120)
# draw them as a single compound-path
translate(0, 140)
with bezier():
for i in range(12):
line(0, i*10, i*15, 120)
@reference('drawing/paths-compound2.png')
def test_paths_compound2(self):
# tut/Bezier_Paths (6)
size(200, 200)
# capture the bezier into the `path` variable
with bezier(plot=False) as path:
for x,y in grid(10,10, 20,20):
shape = choice([rect, oval])
shape(x,y, 15,15)
# apply a gradient fill color to the compound path
fill('green', 'cyan')
bezier(path)
@reference('drawing/paths-flat-union.png')
def test_paths_flat_union(self):
# tut/Bezier_Paths (7)
size(175, 109)
path1 = arc( 50,50, 40, plot=False)
path2 = arc(100,50, 40, plot=False)
compound = path1.union(path2)
with stroke(0), nofill():
bezier(compound)
@reference('drawing/paths-flat-intersect.png')
def test_paths_flat_intersect(self):
# tut/Bezier_Paths (8)
size(175, 109)
path1 = arc( 50,50, 40, plot=False)
path2 = arc(100,50, 40, plot=False)
compound = path1.intersect(path2)
with stroke(0), nofill():
bezier(compound)
@reference('drawing/paths-flat-difference.png')
def test_paths_flat_difference(self):
# tut/Bezier_Paths (9)
size(175, 109)
path1 = arc( 50,50, 40, plot=False)
path2 = arc(100,50, 40, plot=False)
compound = path1.difference(path2)
with stroke(0), nofill():
bezier(compound)
@reference('drawing/paths-flat-xor.png')
def test_paths_flat_xor(self):
# tut/Bezier_Paths (10)
size(175, 109)
path1 = arc( 50,50, 40, plot=False)
path2 = arc(100,50, 40, plot=False)
compound = path1.xor(path2)
with stroke(0), fill(0.8):
bezier(compound)
@reference('drawing/color-gradients1.png')
def test_color_gradients1(self):
# tut/Color (11)
size(99, 275)
stroke('#aaa')
fill('black', 'white')
rect(20,20,75,75)
fill('black', 'white', steps=[.3,.6])
rect(20,110,75,75)
fill('black', 'red', 'white', steps=[0,.3,.6])
oval(20,200,75,75)
@reference('drawing/color-gradients2.png')
def test_color_gradients2(self):
# tut/Color (12)
size(97, 277)
stroke('#aaa')
fill('black', 'white', angle=0)
rect(20,20,75,75)
fill('black', 'white', angle=45, steps=[.3,.6])
rect(20,110,75,75)
fill('black', 'red', 'white', angle=180, steps=[0,.3,.6])
oval(20,200,75,75)
@reference('drawing/color-gradients3.png')
def test_color_gradients3(self):
# tut/Color (13)
size(200, 200)
background(None)
fill('black', ('black',0), center=[-1,-1])
rect(20,20,75,75)
fill('black', ('black',0), center=[1,-1])
rect(100,20,75,75)
fill('black', ('black',0), center=[-1,1])
rect(20,100,75,75)
fill('black', ('black',0), center=[1,1])
rect(100,100,75,75)
@reference('drawing/color-pattern.png')
def test_color_pattern(self):
# tut/Color (14)
size(150, 90)
background('tests/_in/macpaint-dark.png')
with fill('http://plotdevice.io/data/macpaint-tile.png'):
poly(45,45,25, sides=5)
with fill(image('tests/_in/macpaint-thatch.png')):
rect(80,20,50,50)
@reference('drawing/background.png')
def test_background(self):
# ref/Canvas/commands/background()
size(125, 125)
background(.2)
fill(1)
rect(10,10, 50,50)
@reference('drawing/clear.png')
def test_clear(self):
# ref/Canvas/commands/clear()
size(125, 125)
r = rect(0,0, 100,10) # add a rectangle
t = poly(50,50, 25) # add a square
c = arc(125,125, 50) # add a circle
clear(r, c) # remove the rectangle & circle
@reference('drawing/plot-delay.png')
def test_plot_delay(self):
# ref/Canvas/commands/plot()
size(125, 125)
# create a shape (but don't draw it immediately)
r = rect(20,20,40,40, plot=False)
# ...
# draw the saved shape (but override the canvas's fill color)
plot(r, fill='red')
@reference('drawing/plot-disable.png')
def test_plot_disable(self):
# ref/Canvas/commands/plot()
size(125, 125)
# the plot keyword arg prevents this from being drawn
o = oval(0,0,100,100, plot=False)
# the plot() command disables drawing for the entire block
with plot(False):
o = oval(0,0,100,100) # not drawn
s = rect(100,100,10,10) # same here
@unittest.skip("fix colors library first")
@reference('drawing/ximport-colors.png')
def test_ximport_colors(self):
# ref/Canvas/compat/ximport()
size(125, 125)
colors = ximport("colors")
background(colors.papayawhip())
fill(colors.chocolate())
rect(10, 10, 50, 50)
@reference('drawing/arcto-simple.png')
def test_arcto_simple(self):
# ref/Drawing/commands/arcto()
size(125, 125)
for i in range(9):
with bezier(50,120, stroke=0.2, fill=None):
arcto(100, 100-i*10)
@reference('drawing/arcto.png')
def test_arcto(self):
# ref/Drawing/commands/arcto()
size(125, 125)
with bezier(30, 50, stroke=0.2, fill=None):
arcto(55,100, 80,50, 10)
@reference('drawing/beginpath.png')
def test_beginpath(self):
# ref/Drawing/commands/bezier()
size(125, 125)
# define a path inside of a 'with' block
with bezier(10, 10, stroke=0.2) as path:
lineto(40, 10)
@reference('drawing/bezier.png')
def test_bezier(self):
# ref/Drawing/commands/bezier()
size(125, 125)
# define a list of x,y points for the path
points = [(10, 10), (50, 90), (120, 50), (60, 10), (60, 60)]
# draw the path twice; once with straight lines in light grey
# and again with smoothed lines in dark grey
nofill()
bezier(points, stroke=0.75)
bezier(points, stroke=0.25, smooth=True)
# draw red dots at the point coordinates
for x, y in points:
arc(x, y, radius=3, fill='red')
@reference('drawing/curveto.png')
def test_curveto(self):
# ref/Drawing/commands/curveto()
size(125, 125)
nofill()
stroke(0.2)
with bezier(10,50) as path:
curveto(10,0, 110,100, 110,50)
@reference('drawing/lineto.jpg')
def test_lineto(self):
# ref/Drawing/commands/lineto()
size(125, 125)
nofill()
with bezier(10, 10, stroke=0.2) as path:
lineto(40, 40)
lineto(80, 40, close=True)
@reference('drawing/moveto.jpg')
def test_moveto(self):
# ref/Drawing/commands/moveto()
size(125, 125)
with bezier(10, 10, stroke=0.2) as path:
lineto(50, 100)
moveto(60, 100)
lineto(100, 100)
@reference('drawing/beginpath.png')
def test_beginpath(self):
# ref/Drawing/compat/beginpath()
size(125, 125)
stroke(0.2)
beginpath(10, 10)
lineto(40, 10)
endpath()
@reference('drawing/drawpath.jpg')
def test_drawpath(self):
# ref/Drawing/compat/drawpath()
size(125, 125)
stroke(0.2)
beginpath(10, 10)
lineto(40, 10)
p = endpath(plot=False)
drawpath(p)
@reference('drawing/endpath.png')
def test_endpath(self):
# ref/Drawing/compat/endpath()
size(125, 125)
stroke(0.2)
beginpath(10, 10)
lineto(40, 10)
p = endpath(plot=False)
drawpath(p)
@reference('drawing/findpath.png')
def test_findpath(self):
# ref/Drawing/compat/findpath()
size(125, 125)
points = [(10, 10), (90, 90), (350, 200)]
for x, y in points:
oval(x-2, y-2, 4, 4)
nofill()
stroke(0.2)
autoclosepath(False)
path = findpath(points)
drawpath(path)
@reference('drawing/fill.png')
def test_fill(self):
# ref/Line+Color/commands/fill()
size(125, 125)
fill(1.0, 0.0, 0.5)
rect(10, 10, 25, 25)
fill(.3, 0.0, 0.4)
oval(40, 40, 40, 40)
@reference('drawing/strokewidth.jpg')
def test_strokewidth(self):
# ref/Line+Color/commands/pen()
size(125, 125)
nofill()
stroke(0.2)
pen(1.5)
rect(10, 10, 20, 40)
pen(3)
rect(40, 10, 20, 40)
@reference('drawing/capstyle.png')
def test_capstyle(self):
# ref/Line+Color/commands/pen()
size(125, 125)
nofill()
stroke(0)
pen(10, cap=BUTT)
line(20,20, 50,20)
with pen(cap=ROUND):
line(20,40, 50,40)
with pen(cap=SQUARE):
line(20,60, 50,60)
@reference('drawing/joinstyle.png')
def test_joinstyle(self):
# ref/Line+Color/commands/pen()
size(125, 125)
with nofill(), stroke(0), pen(10):
pen(join=MITER)
bezier([(20,20), (40,40), (60,20)])
pen(join=ROUND)
bezier([(20,50), (40,70), (60,50)])
pen(join=BEVEL)
bezier([(20,80), (40,100), (60,80)])
@reference('drawing/stroke.jpg')
def test_stroke(self):
# ref/Line+Color/commands/stroke()
size(125, 125)
nofill()
strokewidth(3)
stroke(0.3, 0.0, 0.4)
rect(10, 10, 20, 40)
stroke(1.0, 0.0, 0.5)
rect(40, 10, 20, 40)
@reference('drawing/capstyle.png')
def test_capstyle(self):
# ref/Line+Color/compat/capstyle()
size(125, 125)
fill(None)
stroke(0)
strokewidth(10)
capstyle(BUTT)
line(20,20, 50,20)
capstyle(ROUND)
line(20,40, 50,40)
capstyle(SQUARE)
line(20,60, 50,60)
@reference('drawing/colormode.jpg')
def test_colormode(self):
# ref/Line+Color/compat/colormode()
size(125, 125)
colormode(RGB)
fill(0.25, 0.25, 0.25)
rect(10, 10, 40, 40)
colormode(HSV)
fill(0, 0, 0.25)
rect(60, 10, 40, 40)
@reference('drawing/joinstyle.png')
def test_joinstyle(self):
# ref/Line+Color/compat/joinstyle()
size(125, 125)
fill(None)
stroke(0)
strokewidth(10)
joinstyle(MITER)
bezier([(20,20), (40,40), (60,20)])
joinstyle(ROUND)
bezier([(20,50), (40,70), (60,50)])
joinstyle(BEVEL)
bezier([(20,80), (40,100), (60,80)])
@reference('drawing/nofill.jpg')
def test_nofill(self):
# ref/Line+Color/compat/nofill()
size(125, 125)
strokewidth(1.5)
stroke(0.2)
fill(0.2)
rect(10, 10, 20, 40)
nofill()
rect(40, 10, 20, 40)
@reference('drawing/nostroke.png')
def test_nostroke(self):
# ref/Line+Color/compat/nostroke()
size(125, 125)
fill(0.2)
strokewidth(6)
stroke(1.0, 0.0, 0.5)
rect(10, 10, 20, 40)
nostroke()
rect(40, 10, 20, 40)
@reference('drawing/strokewidth.jpg')
def test_strokewidth(self):
# ref/Line+Color/compat/strokewidth()
size(125, 125)
nofill()
stroke(0.2)
strokewidth(1.5)
rect(10, 10, 20, 40)
strokewidth(3)
rect(40, 10, 20, 40)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(DrawingTests))
return suite