Repository navigation
Expand file tree
/
Copy pathVisualizer.py
More file actions
82 lines (66 loc) · 2.08 KB
/
Copy pathVisualizer.py
File metadata and controls
82 lines (66 loc) · 2.08 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
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
from ParserOOP import parseToTree
from Node import Node, Num, Var
import turtle
windowSizeX = 1700
windowSizeY = 1000
def drawNode(x, y, node, spacingX, spacingY):
if type(node) in [int, float, Num]:
toDraw = int(node)
else:
toDraw = node.op
fontSize = int(1.2 * circleR / (len(str(toDraw))))
pen.penup()
pen.setheading(0)
pen.setposition(x, y)
pen.pendown()
pen.pensize(5)
pen.circle(circleR)
pen.pensize(1)
pen.write(
str(toDraw).replace("/", "÷"), font=("Cascadia", fontSize, ""), align="center"
)
leftNodePos = [x - spacingX, y - spacingY]
rightNodePos = [x + spacingX, y - spacingY]
if type(node) not in [float, int, Num, Var]:
pen.penup()
pen.setposition(x - circleR * 1.5, y + circleR * 0.3)
pen.pendown()
pen.pensize(5)
pen.setposition(leftNodePos[0] + circleR, leftNodePos[1] + circleR * 2 / 1.1)
pen.pensize(1)
pen.penup()
drawNode(
leftNodePos[0], leftNodePos[1], node.left, spacingX / 1.5, spacingY * 1.5
)
pen.penup()
pen.setposition(x + circleR * 1.5, y + circleR * 0.3)
pen.pendown()
pen.pensize(5)
pen.setposition(rightNodePos[0] - circleR, rightNodePos[1] + circleR * 2 / 1.1)
pen.pensize(1)
pen.penup()
drawNode(
rightNodePos[0], rightNodePos[1], node.right, spacingX / 1.5, spacingY * 1.5
)
circleR = 40 # len(expr)/0.001
startX = 0
spacingX = circleR * 6 + 10
spacingY = circleR * 2 + 10
def visualizeExprTree(*, expr=None, obj=None):
global pen
global scr
pen = turtle.Turtle()
scr = turtle.Screen()
scr.bgcolor("#000000")
pen.pencolor("#ffffff")
pen.speed(0)
scr.setup(windowSizeX, windowSizeY, startx=100, starty=10)
if not obj:
obj = parseToTree(expr, False)
startY = windowSizeY / 2 - circleR * 2 - 10
drawNode(startX, startY, obj, spacingX, spacingY)
pen.hideturtle()
scr.exitonclick()
if __name__ == "__main__":
expr = input("Enter expr: ")
visualizeExprTree(expr=expr)