gnpatterson on DeviantArthttps://www.deviantart.com/gnpatterson/art/Tree-569602849gnpatterson

Deviation Actions

Tree

By
Published:
182 Views

Description

Fractal tree, python program using PILLOW and images2gif

from PIL import Image, ImageDraw
from math import *
from images2gif import writeGif

#Lets set up parameters

x1 = (-100.0,1000.0)
x2 = (-100.0,600.0)
x3 = (0.0,600.0)
scale = 0.6
theta = pi/6 # pi/6 = 30 degrees
canvas = (500,500)
tsteps = 40
depth = 8
animation = []


def x_4(x1,x2,x3):
   return (x1[0]+x3[0]-x2[0],x1[1]+x3[1]-x2[1])

def mid_point(xa,xb):
   return ((xa[0]+xb[0])/2.0,(xa[1]+xb[1])/2.0 )

def take(xa,xb,scale):
   return ((xa[0]-xb[0])*scale,(xa[1] - xb[1])*scale)

def rot(x,theta):
   return (x[0]*cos(theta) - x[1]*sin(theta),x[0]*sin(theta) + x[1]*cos(theta) )

def vadd(xa,xb):
   return (xa[0]+xb[0],xa[1]+xb[1])

#print x_4(x1,x2,x3)

def new_branch(branch,theta,scale):
   #take the given coords and create a new set that are
   #extended from the branch given but rotated by the
   #angle and scale given the mid-point of x2 and x3 is the
   #location of the mid-point of the new x1 and x4
   x1=branch[0]
   x2=branch[1]
   x3=branch[2]
   x4=branch[3]
   mp = mid_point(x2,x3)
   xa=x2
   xb=(x2[0]+x2[0]-x1[0],x2[1]+x2[1]-x1[1])
   xc=(x3[0]+x3[0]-x4[0],x3[1]+x3[1]-x4[1])
   x1d=take(xa,mp,scale)
   x2d=take(xb,mp,scale)
   x3d=take(xc,mp,scale)
   x1d=rot(x1d,theta)
   x2d=rot(x2d,theta)
   x3d=rot(x3d,theta)
   x1=vadd(x1d,mp)
   x2=vadd(x2d,mp)
   x3=vadd(x3d,mp)
   x4=x_4(x1,x2,x3)
   return [x1,x2,x3,x4]


def old_branch(branch,theta,scale):
   reverse=[branch[1],branch[0],branch[3],branch[2]]
   return new_branch(reverse,-theta,1/scale)



x4=x_4(x1,x2,x3)
branch1 = [x1,x2,x3,x4]
branch2 = new_branch(branch1,theta,scale)

img = Image.new( 'P', canvas, "white")
draw = ImageDraw.Draw(img)
#draw.polygon(branch,fill="black",outline='red')
#draw.polygon(branch2,fill="black",outline='red')

ob= old_branch(branch2,theta,scale)

draw.polygon(ob,fill="black",outline='green')


def tree(draw,branch,level):
   draw.polygon(branch,fill="brown",outline='red')
   if level < 1:
       return
   branch2 = new_branch(branch,theta,scale)
   tree(draw,branch2,level-1)
   branch2 = new_branch(branch,-theta,scale)
   tree(draw,branch2,level-1)
   return
   
def toward(x1,x2,amt):
   return( x1[0]+amt*(x2[0]-x1[0]),x1[1]+amt*(x2[1]-x1[1]))

#tree(draw,branch,depth)

def getimage(t):
   img = Image.new( 'P', canvas, "white")
   draw = ImageDraw.Draw(img)
   amt = t / float(tsteps)
   branch=[toward(branch2[i],branch1[i],amt) for i in range(4)]
   if t > tsteps / 2:
       tree(draw,branch,depth+1)
   else:
       tree(draw,branch,depth)
   return img
   
   
for t in range(tsteps):
   animation.append(getimage(t))

writeGif("C:\\Users\\GrahamNew\\Documents\\tree.gif",animation,duration=0.1)

img.save("C:\\Users\\GrahamNew\\Documents\\test.bmp")
Image size
500x500px 1.69 MB
© 2015 - 2024 gnpatterson
Comments0
Join the community to add your comment. Already a deviant? Log In