Skip to content

Instantly share code, notes, and snippets.

@scjody
Last active December 17, 2015 03:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scjody/5544232 to your computer and use it in GitHub Desktop.
Save scjody/5544232 to your computer and use it in GitHub Desktop.
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