I want to generate a fractal tree using recursion. However the "branches" of the tree aren't being drawn at the correct angle (Each branch should be drawn coming off at 45 degrees to the branch below it). Is my maths wrong or is it my code?
I've gotten out a pad and pen and from what I can tell, just adding the angle once each time the function runs should give the right answer, however it gives the branches coming off at different degrees instead.
from tkinter import *
from random import *
import numpy as np
#GLOBAL VARIABLES
Width=1500
Height=1000
l=400
decay=0.67
linewidth=5
theta=np.pi/4
#DEFINITIONS
def drawobject(x,y,l,angle_r,angle_l,theta):
x1r= x + l*np.sin(angle_r)
y1r= y - l*np.cos(angle_r)
x1l= x + l*np.sin(angle_l)
y1l= y - l*np.cos(angle_l)
canvas.create_line(x,y,x1r,y1r,width=0.01*l,fill="black")
canvas.create_line(x,y,x1l,y1l,width=0.01*l,fill="black")
angle_r+=theta
angle_l-=theta
if l>3:
drawobject(x1r,y1r,decay*l,angle_r,angle_l,theta)
drawobject(x1l,y1l,decay*l,angle_r,angle_l,theta)
#MAINBODY
root=Tk()
canvas=Canvas(width=Width, height=Height, bg="white")
canvas.pack()
drawobject(Width/2,Height,l,np.pi/4,-np.pi/4,theta)
root.mainloop()
Each branch should shoot off at plus/minus theta to the branch below, however the second layer of branches is flat and the third layer comes off at a different angle entirely. I'm also open to any style critiques of my code as I'm quite new, all the best!
The angles I was inputting when I called the function inside itself were the same, when they should have been two different angles as suggested by Tom Karzes. See our comments for more details.