kirby urner schrieb:
Some of you old timers may recall a project in May, 2004 to implement
Wolfram's minimalist cellular automaton experiments using a Tk canvas
and/or PIL. As I recall, John showed us how to speed it up a whole lot by passing some 'False' parameter in the GraphWin call. However, now that I'm testing the code, in preparation for this post, lo
these many years later (on a faster computer in a different version of
Python (2.5)), and with a newly downloaded graphics.py, I'm seeing
those rows of the Mayan Pyramid, associated with Wolfram's "Rule 30" [1],
run across the screen like some raster beam on an ultra slow TV.
Hi Kirby and all,
I'm not sure if I'm adding something useful or intersting at all, because I'm
currently following these threads only very superficially. I just wanted
to demonstrate, that the use of a simple canvas3.py - which uses Tkinter
directly -  can also speed up your ultra slow TV considerably.

See attachments. (nks2.py has only the import statement and the last
line of the next() method  added to nks.py. You can delete the last one
if you don't want to watch the pyramid growing)

Regards,
Gregor Lingl

"""
Used in support of nks.py
by K. Urner
"""

# from Dr. Zelle's graphics library
from Tkinter import Tk
from Tkinter import Canvas as TkCanvas

class Canvas(object):

    def __init__(self, width, rows, pixelsize):
        self.pixelsize = pixelsize
        self.c = Tk()
        self.cv = TkCanvas(width=width*pixelsize,
                           height=rows*pixelsize,
                           background='black')
        self.cv.pack()

    def drawcell(self, thepoint):
        pixelsize=self.pixelsize
        therow = thepoint[0]*pixelsize
        thecol = thepoint[1]*pixelsize
        self.cv.create_rectangle(therow, thecol,
                                 therow+pixelsize, thecol+pixelsize,
                                 fill='yellow', outline='yellow')

    def showimage(self):
        self.c.mainloop() # comment this out if using IDLE with -n switch
        pass
"""
by Kirby Urner, 4D Solutions

Version 0.3
Revised: May 10, 2004  9:10 PM -- split into 3 modules, canvas2 uses newer 
graphics.py by Dr. John Zelle
Revised: May  8, 2004  8:00 PM -- changed to rectangles in Tk, other fixes
Revised: May  8, 2004  5:48 PM -- added Tk output (see edu-sig of today)

Usage:
import nks
nks.t1(30,200,100,4)

or

"""

# uncomment one or the other, reload if switching

# from canvas1 import Canvas   # <-- PIL
# from canvas2 import Canvas # <-- graphics.py
from canvas3 import Canvas # <-- Tkinter.py

def base2(n,pad=8):
    output = []
    while n > 1:
        digit = n%2
        n //= 2
        output.append(str(digit)) 
    output.append(str(n))
    output.reverse()
    return (''.join(output)).zfill(pad)

def makerule(n):
    therule = {}
    output  = base2(n)
    for i in range(8):
        therule[base2(7-i,3)] = output[i]
    return therule

def sayrule(themap):
    for i in range(7,-1,-1): 
        thekey = base2(i,3)
        print "%s --> %s" % (thekey, themap[thekey])

class Pattern(object):
    
    def __init__(self, n, width=40, rows=20, pixelsize = 1):
        self.width = width
        self.rule = makerule(n)
        self.therow = ('0' * (width//2) +
                       '1' +
                       '0' * (width - width//2 - 1))
        self.rownum = 0
        # Canvas imported from either of 2 modules
        self.canvas = Canvas(width,rows,pixelsize)

    def next(self):
        while True:
            yield self.therow
            for i in range(len(self.therow)):
                if self.therow[i]=='1':
                    self.canvas.drawcell((i,self.rownum))
                    
            newrow = ['0'] * len(self.therow)
            for i in range(1,len(self.therow)-1):            
                thekey = (self.therow[i-1] +
                          self.therow[i] +
                          self.therow[i + 1])
                newrow[i] = self.rule[thekey]
            self.therow = ''.join(newrow)
            self.rownum += 1
            self.canvas.c.update()

    def showimage(self):
        self.canvas.showimage()
            
    def __iter__(self):
        return self.next()
        
def t1(rule, width, height, pixelsize=1):
    p = Pattern(rule ,width, height, pixelsize)
    g = p.next()
    for i in range(width/2):
        g.next()
    p.showimage()

if __name__ == '__main__':
    t1(30,200,100,4)
_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to