Skip to content

Instantly share code, notes, and snippets.

@scjody
Last active Dec 17, 2015
Embed
What would you like to do?
Sierpinski triangle script for plot.ly, based on their Lindenmayer System demo
# Python 2.7.3 (default, Apr 20 2012, 23:04:22) [GCC 4.6.3]
# Use plot() to plot lists or arrays
# Use print to show results in the command line history
from numpy import sin, cos, pi
def f(s,itr):
xo=0
yo=0
a=0
b=pi/3
d=10
for i in xrange(itr):
ns=''
for mv in s:
if mv=='A':
ns+='B-A-B'
elif mv=='B':
ns+='A+B+A'
else:
ns+=mv
s=ns
print s
s=list(s)
print len(s)
mvs=[]
xa=[]
ya=[]
for mv in s:
if mv=='A' or mv=='B':
x=xo+d*cos(a)
y=yo+d*sin(a)
xa.append(x)
ya.append(y)
xo=x
yo=y
elif mv=='+': a+=b
elif mv=='-': a-=b
elif mv=='[' and len(mvs)!=0:
x,y=mvs.pop()
xo=x
yo=y
elif mv==']':
mvs.append((xo,yo))
plot(xa,ya,{'color':'black'})
#s='F[-F]F[+F][F]'
s='A'
f(s,10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment