Repository navigation
Expand file tree
/
Copy pathSwissCheese.pv
More file actions
48 lines (40 loc) · 1.62 KB
/
Copy pathSwissCheese.pv
File metadata and controls
48 lines (40 loc) · 1.62 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
"""
Boolean operations and Bezier paths.
This program makes swiss cheese by cutting out circles from another
circle. It demonstrates the use of boolean operations on paths.
"""
size(300, 300)
# First, create the "cheese" -- the central circle in the middle.
# But don't plot it yet, since we will be creating a new path
# with holes poked out of it.
cheese = arc(WIDTH/2, HEIGHT/2, 130, plot=False)
# Instead of the circle, we could use any arbitrary path.
# Try uncommenting the following lines to use a letter:
# font("Helvetica","bold", 400)
# cheese = textpath("S", 7, 291)
# Punch ten holes in the cheese
for i in range(10):
# Make a circle somewhere on the canvas, with a random size.
x = random(WIDTH)
y = random(HEIGHT)
sz = random(10, 60)
c = arc(x, y, sz, plot=False)
# Now comes the central part: create a new path out of the cheese
# that is the difference between the cheese and the newly created
# circle. Save this in cheese (overwriting the old value).
cheese = cheese.difference(c, 0.1)
# Another cool effect is to mirror the cutouts. Here, we create a
# mirrored copy of the circle, then cut it out of the cheese
# as well. (You might want to change the number of punchouts.)
# mx = WIDTH - x
# c = arc(mx, y, sz, plot=False)
# cheese = cheese.difference(c, 0.1)
# Now that the cheese is ready, draw it. First we pass it to the
# bezier() command with a dark fill color to draw a "shadow".
fill(0.1)
bezier(cheese)
# Once the shadow is drawn, translate the canvas a bit and draw the
# path again in glorious yellow:
translate(-3, -3)
fill(1, 0.9, 0.1, 0.8)
bezier(cheese)