[Edu-sig] Re: Teaching graphics with Python (was Introductoryhigh school programming)

Kirby Urner urnerk at qwest.net
Sat May 8 20:52:00 EDT 2004


> Kirby, I'm wondering if you have tried any of the cellular automata
> stuff using my graphics package. I suspect it would be dead simple,
> probably much eaiser than with POV-Ray or PIL. I haven't actually
> compared, so that's just a hunch.
> 

I think that's an excellent idea.  I've been wanting to include Tk in the
mix for sure.

Your idea to incorporate your stereo mode in the VPython stuff is also very
interesting.  I'm fantasizing about handing out stereographic glasses at
OSCON.

So just now I cut and pasted my PIL code and wrote it slightly differently
for graphics.py.  The graphics.py code is indeed somewhat simpler.

This business about pixelsize (below) is a bit confusing, but basically I'm
using "virtual pixels" with a canvas made up of those.  This is because
actual pixels tend to be rather tiny, and I often want to use "fat pixels"
that are actually 2x2 or 3x3 or 4x4 regular pixels.  Instead of using
rectanglar fills, I just fill in a 3x3 pixel by hitting all 9 actual pixels
individually.  I should try it with rectangles.

What I find with the current setup is that the PIL graphic, in building off
screen until view time, develops quickly.  The Tk-based implementation, on
the other hand, in painting pixel by pixel, crawls along one row at a time,
taking 15 minutes versus a few seconds.  On the other hand, slow crawling
has its appeal, i.e. one has time to think about how one row of cells
determines the next, and so on.

Here are my two canvas objects (PIL and graphics.py).  I'll post the
complete code for doing these 'New Kind of Science' cell maps once I clean
it up a bit.  Oh heck, I'll just toss nks.py to 
www.4dsolutions.net/ocn/python and revise it online.  

(for color coded view, in browser, use:
http://www.4dsolutions.net/cgi-bin/py2html.cgi?script=/ocn/python/nks.py
)

I don't mind showing some awkwardness (I have these reversals of binary
strings going on because I wanted 110, say, to come out in binary going left
to right, but for this Wolfram stuff, a right to left string would simplify
the code -- I'll likely be revising accordingly).

class Canvas(object):
    """
    Uses PIL -- so far doesn't include option to save picture to disk
    """
    def __init__(self, width, rows, pixelsize):
        self.pixelsize = pixelsize
        self.im = Image.new('RGB',(width*pixelsize, rows*pixelsize),(0,0,0))
        self.draw = ImageDraw.Draw(self.im)

    def drawcell(self, thepoint):
        therow = thepoint[0]*self.pixelsize
        thecol = thepoint[1]*self.pixelsize
        for i in range(self.pixelsize):
            for j in range(self.pixelsize):
                self.draw.point((therow + i, thecol + j), fill=(255,255,0))
                        
    def showimage(self):
        self.im.show()

class Canvas2(object):
    """
    Uses graphics.py by Dr. John Zelle
    See:  http://mcsp.wartburg.edu/zelle/python/
    """

    def __init__(self, width, rows, pixelsize):
        self.pixelsize = pixelsize
        self.c = GraphWin('NKS',width*pixelsize, rows*pixelsize)

    def drawcell(self, thepoint):
        therow = thepoint[0]*self.pixelsize
        thecol = thepoint[1]*self.pixelsize
        for i in range(self.pixelsize):
            for j in range(self.pixelsize):
                thepoint = Point(therow + i, thecol + j)
                thepoint.draw(self.c)


Kirby


PS:  a win for me just now was getting Samba server on the laptop visible on
the Windows network neighborhood via wifi.  So I'm writing email on my XP
desktop, while editing/uploading source code from the Linux laptop via the
XP desktop.  Fun fun.





More information about the Edu-sig mailing list