I made a tree drawing thing a couple of years when I saw this vid, I recommend checking out some of his other videos, they aren’t in python but it’s some really nice concepts and he explains them in terms that can translate to other languages
from ui import ImageContext, Path, set_color, fill_rect
from math import sin, cos, pi
from random import random
#help(random)
def r(x, y, t):
# t = t +(random() -0.5)*0.5
return x*cos(t) - y*sin(t), y*sin(t) + x*cos(t)
class tree:
def __init__(self):
self.startlen = 50
self.maxit = 9
self.th = 5*pi/4
self.thl = pi/12
self.thr = -pi/12
self.p = Path()
self.short = 0.85
self.size = 1000
def draw(self):
self.p = Path()
self.stopn = self.startlen * (self.short ** self.maxit)
with ImageContext(self.size, self.size) as cx:
set_color((255,255,255))
fill_rect(0,0,self.size, self.size)
set_color((0,0,0))
self.p.line_width = 0.9
self.p.move_to(self.size/2, self.size)
self.branch(self.startlen, self.size/2, self.size*4/5, 5*pi/4)
self.p.stroke()
cx.get_image().show()
def branch(self, len, x, y, t):
self.p.line_to(x, y)
if len > self.stopn:
self.p.move_to(x,y)
x1, y1 = r(len, len, self.thl+t)
self.branch(len*self.short, x+x1, y+y1, self.thl+t)
self.p.move_to(x,y)
x1, y1 = r(len, len, self.thr+t)
self.branch(len*self.short, x+x1, y+y1, self.thr+t)
#self.branch(len*self.short, x+x1, y+y1, self.thr+t)
self.p.move_to(x,y)
if __name__ == '__main__':
s = tree()
#s.thl += 0.25
#for i in range(100):
s.draw()