-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
92 lines (73 loc) · 2.29 KB
/
example.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
86
87
88
89
90
91
92
# Small example
from treedraw import Tree
# Build a tree
T = Tree("a1")
b = T.addChild("b2")
c = T.addChild("c5")
d = T.addChild("d6")
e = b.addChild("e3")
f = b.addChild("f4")
g = d.addChild("g7")
h = d.addChild("h10")
i = d.addChild("i11")
j = g.addChild("j8")
k = g.addChild("k9")
l = h.addChild("l12")
m = i.addChild("m13")
n = i.addChild("n14")
o = i.addChild("o15")
p = i.addChild("p16")
# Calculate layout
T.walker(0.6)
# Print coordinates
print("Node:\t x,\ty")
for node in T.nodes:
p = node.position(origin=(0, 0), scalex=100, scaley=1)
print("%s:\t(%#4d,\t%d)" % (node.data, p[0], p[1]))
# Draw the tree (requires pygame)
import pygame
import pygame.gfxdraw
import sys
import time
width, height = 800, 400
sizex, sizey = 130, 60
rootpos = (width / 2 - 100, height / 2 - 100)
# Create the screen
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('tree drawing with pygame')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, (0, 0))
# Draw edges
for node in T.nodes:
if node.parent:
pygame.draw.aaline(
screen, (0, 0, 0), node.position(
rootpos, sizex, sizey), node.parent.position(
rootpos, sizex, sizey))
# Draw vertices
for node in T.nodes:
myfont = pygame.font.Font(None, 36)
label = myfont.render(node.data, 1, (0, 0, 0))
textrect = label.get_rect()
textrect.centerx = node.position(rootpos, sizex, sizey)[0]
textrect.centery = node.position(rootpos, sizex, sizey)[1]
p = textrect.copy().inflate(10, 10)
pygame.draw.ellipse(screen, (255, 255, 255), p)
pygame.gfxdraw.aaellipse(screen, p.centerx, p.centery, int(
p.width / 2), int(p.height / 2), (0, 255, 255))
screen.blit(label, textrect)
# Show everything
pygame.display.flip()
# Stop on q, Escape or "Close Window" button
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and (
event.unicode == '\x1b' or event.unicode == 'q')):
pygame.display.quit()
pygame.quit()
sys.exit()
time.sleep(0.1)