diff --git a/tree.png b/tree.png new file mode 100644 index 0000000..9f949fb Binary files /dev/null and b/tree.png differ diff --git a/trees/trees.py b/trees/trees.py index 06ef270..4ab53f9 100644 --- a/trees/trees.py +++ b/trees/trees.py @@ -2,16 +2,18 @@ from math import sin, cos from matplotlib import pyplot as plt -s=1 -d=[[0,1,0]] -plt.plot([0,0],[0,1]) -for i in range(5): - n=[] - for j in range(len(d)): #loop over d - n.append([d[j][0]+s*sin(d[j][2]-0.1), d[j][1]+s*cos(d[j][2]-0.1), d[j][2]-0.1]) - n.append([d[j][0]+s*sin(d[j][2]+0.1), d[j][1]+s*cos(d[j][2]+0.1), d[j][2]+0.1]) - plt.plot([d[j][0], n[-2][0]],[d[j][1], n[-2][1]]) - plt.plot([d[j][0], n[-1][0]],[d[j][1], n[-1][1]]) - d=n - s*=0.6 -plt.savefig('tree.png') + +branch_length = 1 # Length of the first branch +start_branch = [[0,1,0]] # List of initial root branches +branches_number = 5 +plt.plot([0,0],[0,1]) # Plots root +for branch in range(branches_number): # Iterates over the number of branches + end_branch = [] + for j in range(len(start_branch)): + end_branch.append([start_branch[j][0]+branch_length*sin(start_branch[j][2]-0.1), start_branch[j][1]+branch_length*cos(start_branch[j][2]-0.1), start_branch[j][2]-0.1]) + end_branch.append([start_branch[j][0]+branch_length*sin(start_branch[j][2]+0.1), start_branch[j][1]+branch_length*cos(start_branch[j][2]+0.1), start_branch[j][2]+0.1]) + plt.plot([start_branch[j][0], end_branch[-2][0]],[start_branch[j][1], end_branch[-2][1]]) # Plots branches + plt.plot([start_branch[j][0], end_branch[-1][0]],[start_branch[j][1], end_branch[-1][1]]) # Plots branches + start_branch = end_branch # Updating the list of branches + branch_length *= 0.6 # Making each progressive branch level shorter by given factor. +plt.savefig('tree.png') # Saving the plot