Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved code - Part 1 #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 15 additions & 13 deletions trees/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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