Skip to content

Instantly share code, notes, and snippets.

@ptweir
Last active November 12, 2015 22:14
Show Gist options
  • Save ptweir/10549af4ae79f7358a84 to your computer and use it in GitHub Desktop.
Save ptweir/10549af4ae79f7358a84 to your computer and use it in GitHub Desktop.
A quick script to play with recursion, fractals, and pyqtgraph
import pyqtgraph as pg
import numpy as np
import pyqtgraph.exporters
def make_tree(order, theta, sz, basePosition, heading, data):
trunk_ratio = 0.55
trunk = sz * trunk_ratio
deltaX = trunk * np.cos(heading)
deltaY = trunk * np.sin(heading)
newpos = (basePosition[0] + deltaX, basePosition[1] + deltaY)
data[0].extend([basePosition[0], newpos[0], np.nan])
data[1].extend([basePosition[1], newpos[1], np.nan])
if order > 0:
newsz = sz*(1 - trunk_ratio)
make_tree(order-1, theta, newsz+newsz*np.mod(order+1,2), newpos, heading-theta, data)
make_tree(order-1, theta, newsz+newsz*np.mod(order,2), newpos, heading+theta, data)
return data
win = pg.GraphicsWindow()
win.setWindowTitle('fractal')
win.setGeometry(100,100,800,900)
plot = win.addPlot()
plot.setYRange(0, 45)
plot.setXRange(-15, 13)
plot.hideAxis('left')
plot.hideAxis('bottom')
order = 0
curves = []
def update():
global order, timer, curves
if order >= 15 or order <=-1 :
timer.stop()
data = make_tree(order, .4, 10., (0,0), np.pi/2+.4/2, [[],[]])
curves.append(plot.plot())
curves[-1].setZValue(16-order)
curves[-1].setPen(7-order/2, 7)
curves[-1].setData(data[0], data[1], connect="finite")
order += 2
exporter = pg.exporters.ImageExporter(plot)
exporter.export('fractal'+str(order)+'.png')
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(1000)
@ptweir
Copy link
Author

ptweir commented Nov 12, 2015

Based on code from http://openbookproject.net/thinkcs/python/english3e/recursion.html and the pyqtgraph examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment