There's a nice Koch snowflake program in python on Wikipedia which is
cool because it makes a little LOGO-like turtle actually draw the
curve. I have a slight modification that I use occaisonally to
illustrate non-rectifiable curves:
{{{
import turtle
def draw_koch(n, track_length=False):
'''
Draws a Koch snowflake of depth n. Modified by Marshall Hampton
from code found on the wikipedia entry on the Koch curve.
EXAMPLE:
# The following commands should work from a python command-line
prompt.
from pykoch import *
turtle.up()
turtle.goto(-200,100) #so the snowflake fits; you might have to
modify that for your screen
draw_koch(5)
'''
turtle.down()
instruction_set = 'FRFRFR' # tells the turtle where to go: left,
right, or forward
length = 0.0 # for tracking the path length
for i in range(n):
instruction_set = instruction_set.replace("F","FLFRFLF")
for move in instruction_set:
if move is "F":
turtle.forward(100.0/3**(n-1))
if track_length: length += 100.0/3**(n-1)
if move is "L": turtle.left(60)
if move is "R": turtle.right(120)
if track_length: print "Total length at depth " + str(n) + " is: "
+ str(length)
}}}
To draw the figure in the book I hacked some code up that isn't quite
as nice, but it got the job done:
{{{
patd =
{'0':'0340','1':'1451','2':'2532','3':'3203','4':'4014','5':'5125'}
def koch(depth, cleared = False, scale = 1.0):
del0 = vector([1.0,0.0])*scale
del1 = vector([-.5,-0.8660254])*scale
del2 = vector([-.5,+.8660254])*scale
del3 = vector([.5,0.8660254])*scale
del4 = vector([.5,-.8660254])*scale
del5 = vector([-1.0,0.0])*scale
current = vector([-.5,0.8660254/3])*scale
path = '012'
klines = []
dlist = [del0,del1,del2,del3,del4, del5]
for q in range(depth):
sc = 3.0^(-q)
#current = current*sc
scdlist = [sc*x for x in dlist]
if cleared: klines = []
for x in path:
ind = ['0','1','2','3','4','5'].index(x)
deln = scdlist[ind]
klines.append(line([current, current+deln], rgbcolor =
(0,0,0)))
current = current + deln
newpath = ''
for x in path:
newpath = newpath + patd[x]
path = newpath
return klines
klinenest = []
circr =(.25+(.866/3.0)^2)^(.5)
circnest = []
for i in range(1,5):
iscale = (circr)^(4-i)
newk = sum(koch(i, cleared=True, scale = iscale))
klinenest.append(newk)
ncircr = circr*iscale
circnest.append(circle((0,0),ncircr, rgbcolor = (0,0,0)))
klinenest = sum(klinenest)
circnest = sum(circnest)
show(klinenest+circnest,axes = False, figsize = [6,6])
}}}
...I have no doubt that could be improved a lot.
For the Sierpinski triangle I did the following (again, its probably
not optimal):
{{{
nsq = n(3^(1/2))/2.0
tlist_old = [[[-1/2.0,0.0],[1/2.0,0.0],[0.0,nsq]]]
tlist_new = [x for x in tlist_old]
sierp = Graphics()
for ind in range(6):
for tri in tlist_old:
for p in tri:
new_tri = [[(p[0]+x[0])/2.0, (p[1]+x[1])/2.0] for x in
tri]
tlist_new.append(new_tri)
tlist_old = [x for x in tlist_new]
q = sum([line(x+[x[0]], rgbcolor = (0,0,0)) for x in tlist_old])
show(q, figsize = [6,6*nsq], axes = False)
}}}
Cheers,
Marshall
On Dec 5, 7:26 am, "David Joyner" <[EMAIL PROTECTED]> wrote:
> On Fri, Dec 5, 2008 at 8:00 AM, mhampton <[EMAIL PROTECTED]> wrote:
>
> > I am working on an edition with complete sage code for the figures.
> > Unfortunately, this project started almost as a joke and I wasn't very
> > organized about it at first, so it will take me a while to organize
> > the code. Since its a low priority project for me even now, I am not
> > sure how long it will take me to get it all together, probably between
> > a few weeks and a few months. But I can certainly dig out the code
> > for a particular figure if there is one that particularly interests
> > you.
>
> The Sierpinski gasket and the Koch snowflake, please:-)
>
>
>
> > -Marshall
>
> > On Dec 4, 6:39 pm, "David Joyner" <[EMAIL PROTECTED]> wrote:
> >> This is great Marshall!
>
> >> Do you have the Sage code to go along with the figures?
>
> >> On Thu, Dec 4, 2008 at 5:13 PM, mhampton <[EMAIL PROTECTED]> wrote:
>
> >> > I've been working on a mathematical coloring book, with the pictures
> >> > created using Sage. It still needs some work but I've put a
> >> > preliminary version up at lulu.com:
>
> >> >http://www.lulu.com/content/4858716
>
> >> > I am not making any money on it, the cost is what lulu.com charges to
> >> > print it. I have also made the download freely available. I would
> >> > appreciate feedback, especially from people with kids who try it out.
>
> >> > Eventually I hope to produce a nicer first edition with complete Sage
> >> > source code included as an appendix. I am thinking of releasing that
> >> > under a creative commons license, but I need to educate myself a
> >> > little more about that and other options.
>
> >> > -Marshall Hampton
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sage-edu" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-edu?hl=en
-~----------~----~----~----~------~----~------~--~---