-
Notifications
You must be signed in to change notification settings - Fork 34
/
figure6.py
85 lines (70 loc) · 2.21 KB
/
figure6.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
from gen import Tree
from math import atan, cos, sin, pi
from demo_trees import trees
from buchheim import buchheim as layout
from PIL import Image, ImageDraw
tree = trees[4][1]
tree[0][1].children.append(Tree("a"))
tree[0][1].children.append(Tree("b"))
t = layout(trees[4][1])
DIAMETER = 30
SPACING_VERTICAL = DIAMETER * 1.5
SPACING_HORIZONTAL = DIAMETER * 2
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)
def dottedline(draw, x1, y1, x2, y2):
segment = 5
if x2 - x1 > 0:
theta = atan(float(y2 - y1) / float(x2 - x1))
else:
theta = pi + atan(float(y2 - y1) / float(x2 - x1))
dx = cos(theta) * segment
dy = sin(theta) * segment
xdir = x1 < x2
ydir = y1 < y2
while 1:
if xdir != (x1 < x2) or ydir != (y1 < y2):
break
draw.line([x1, y1, x1 + dx, y1 + dy], fill=(0))
x1, y1 = x1 + 2 * dx, y1 + 2 * dy
def drawthreads(draw, root, depth):
for child in root.children:
c = child.thread
if c:
dottedline(
draw,
child.x * SPACING_HORIZONTAL + (DIAMETER / 2),
(depth + 1) * SPACING_VERTICAL + (DIAMETER / 2),
c.x * SPACING_HORIZONTAL + (DIAMETER / 2),
(depth + 2) * SPACING_VERTICAL + (DIAMETER / 2),
)
drawthreads(draw, child, depth + 1)
im = Image.new("L", (1000, 500), (255))
draw = ImageDraw.Draw(im)
drawconn(draw, t, 0)
drawthreads(draw, t, 0)
drawt(draw, t, 0)
im.save("figure6.png")