-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSorting.pv
More file actions
42 lines (33 loc) · 1.33 KB
/
Copy pathSorting.pv
File metadata and controls
42 lines (33 loc) · 1.33 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
"""
Primitives can be modified even after being "drawn" to the canvas.
This example shows how you can create interesting effects by
messing with the internals of the `canvas` object after shapes
have already been drawn to it.
"""
size(550, 300)
pen(1.5)
fill(1, 0.8)
# First, generate some rectangles all over the canvas, rotated randomly.
for i in range(3000):
grob = rect(random(WIDTH)-25, random(HEIGHT)-25,50, 50)
grob.rotate(random(360))
# Now comes the "clever" part:
# We want to sort the objects on the canvas using a custom sorting
# method; in this case, their vertical position.
# The bounds property returns the following: ( (x, y), (width, height) )
# The second arg in the call to ordered() sorts by y position
sorted_grobs = ordered(canvas, 'bounds.y')
# Now that we have all the graphic objects ("grobs") sorted,
# traverse them in order and change their properties.
t = 0.0 # a counter going from 0.0 to 1.0
d = 1.0 / len(sorted_grobs) # the delta amount added each step
for grob in sorted_grobs:
# Grobs will get bigger
grob.scale(t)
# Grobs's stroke will get darker
grob.stroke = (0.6 - t, 0.50)
t += d
# This is seriously hack-y, but you can replace the internal
# grob list of the canvas with your own to change the Z-ordering.
# Comment out this line to get a different effect:
canvas._grobs = sorted_grobs