-
Notifications
You must be signed in to change notification settings - Fork 34
/
knuthdraw.py
46 lines (37 loc) · 1.11 KB
/
knuthdraw.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
from demo_trees import trees
from knuth import layout
from PIL import Image, ImageDraw
t = layout(trees[5])
DIAMETER = 30
SPACING_VERTICAL = DIAMETER * 1.5
SPACING_HORIZONTAL = DIAMETER * 1.5
def drawt(draw, root, depth):
draw.ellipse(
[
root.x * SPACING_HORIZONTAL,
depth * SPACING_VERTICAL,
root.x * SPACING_HORIZONTAL + DIAMETER,
depth * SPACING_VERTICAL + DIAMETER,
],
fill=(225),
outline=(0),
)
for child in root.children:
drawt(draw, child, depth + 1)
def drawconn(draw, root, depth):
for child in root.children:
draw.line(
[
root.x * SPACING_HORIZONTAL + (DIAMETER / 2),
depth * SPACING_VERTICAL + (DIAMETER / 2),
child.x * SPACING_HORIZONTAL + (DIAMETER / 2),
(depth + 1) * SPACING_VERTICAL + (DIAMETER / 2),
],
fill=(0),
)
drawconn(draw, child, depth + 1)
im = Image.new("L", (1000, 500), (255))
draw = ImageDraw.Draw(im)
drawconn(draw, t, 0)
drawt(draw, t, 0)
im.save("knuthdraw.png")