├── README.md
└── main.py
/README.md:
--------------------------------------------------------------------------------
1 | # BinaryTree
2 |
3 | Discussion on reddit
4 |
5 | YouTube SpeedCode
6 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from math import *
3 | from colour import Color
4 | import random
5 | import math
6 | import time
7 |
8 | finestra = Tk()
9 | finestra.geometry("600x600")
10 | finestra.title("BoaringProject")
11 | canvas = Canvas(finestra, width= 600, height= 600, bg="black")
12 | canvas.pack()
13 | green = Color("green")
14 |
15 | color= 0
16 |
17 | prob = 6 # prob to generate recursive: 0 => 0%, 10 => 100%
18 | length = 30.0 # length of first line
19 | nMax = 40 # max recursive level
20 |
21 | colors = list(green.range_to('violet', nMax + 1))
22 |
23 | def binary_prob_tree(x1, y1, level):
24 | nRand = random.randint(1, 10)
25 |
26 | if nRand > prob or level > nMax:
27 | return 0
28 |
29 | partial_length = length - level * 0.8
30 |
31 | x2_p1 = x1 - 1/2*length
32 | y2_p1 = y1 - 1/2*length
33 |
34 | x2_p2 = x1 + 1/2*length
35 | y2_p2 = y1 - 1/2*length
36 |
37 | """
38 | create 2 new smaller lines, on direction top left and top right
39 | from the given x1 and x2
40 | """
41 | canvas.create_line(x1, y1, x2_p1, y2_p1, fill=colors[level], width= 3)
42 | canvas.create_line(x1, y1, x2_p2, y2_p2, fill=colors[level], width= 3)
43 |
44 | finestra.update()
45 |
46 | binary_prob_tree(x2_p1, y2_p1, level+1)
47 | binary_prob_tree(x2_p2, y2_p2, level+1)
48 |
49 |
50 | canvas.create_line(300, 600, 300, 570, fill=colors[1], width= 5)
51 | binary_prob_tree(300, 570, 1)
52 |
53 |
54 | finestra.mainloop()
--------------------------------------------------------------------------------