[Edu-sig] Introducing Python to Engineering Students

Warren Sande warren.sande at rogers.com
Thu Mar 13 04:19:52 CET 2008


By the way, here is a non-OO version of a the fractals program that uses Pygame to display the output.

Warren Sande

#-------------------------
# Simple fractal program using Pygame to display results
# (Based on Kirby Urner's OO version)
import pygame, sys
palette = [(0,0,0)]

def mkpalette():
    global palette
    for i in range(0,255):
        palette.append((i*5%200 + 20, i*7%200 + 20, i*11%200 + 20))
    return palette
    
def compute_fractal(n, uleft_x, uleft_y, lright_x, lright_y):
    global pixels
    xwidth = lright_x - uleft_x
    ywidth = uleft_y  - lright_y 
    pixels = []
    for x in range (500):
        pixel_row = []
        for y in range (500):
            percentx = x/500.0
            percenty = y/500.0
            xp = uleft_x + percentx * xwidth
            yp = uleft_y - percenty * ywidth
            z = complex(xp,yp)
            o = complex(0,0)
            dotcolor = 0
            for trials in range(n):
                if abs(o) <= 2.0:
                    o = o**2 + z
                else:
                    dotcolor = trials
                    break
            pixel_row.append(palette[dotcolor])
        pixels.append(pixel_row)
     
mkpalette()
pixels = []
print "computing fractal..."
compute_fractal(64, -2, 1.25, 0.5, -1.25)
print "done."
screen = pygame.display.set_mode((500,500))
f_surf = pygame.Surface((500, 500))
for x in range(500):
    for y in range(500):
        f_surf.set_at((x, y), pixels[x][y])
screen.blit(f_surf, [0,0,500,500])
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
#-------------------------

----- Original Message ----
From: Warren Sande <warren.sande at rogers.com>
To: edu-sig at python.org
Sent: Tuesday, March 11, 2008 12:20:02 AM
Subject: Re: [Edu-sig] Introducing Python to Engineering Students


David,

For output graphics, you might want to have a look at Pygame.  It is a wrapper for the SDL library.  It has functionality for creating graphics windows, drawing, sprites, etc.  But what might be of interest for you is the simple set_at(x,y) method, to set the color of individual pixels in a window.

I have found the Pygame documentation to be pretty good.

Here is a simple example of plotting a sinewave using set_at()

#-----------------------------
import pygame, sys, math
screen = pygame.display.set_mode([640,480])
for x in range(0, 640):
    y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240)
    screen.set_at([x, y],[255,0,0])
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
#------------------------------

Warren Sande



----- Original Message ----
From: David MacQuigg <macquigg at ece.arizona.edu>
To: edu-sig at python.org
Sent: Monday, March 10, 2008 10:28:21 PM
Subject: [Edu-sig] Introducing Python to Engineering Students

I've been asked to give an intro to Python for a freshman class with 150 students at University of Arizona.  The class is taught in the Electrical and Computer Engineering Department, and is titled Computer Programming for Engineering Applications. The language is C (Hanly & Koffman, Problem Solving and Program Design in C).

I think a nice way to do this will be an application where we can show the advantages of both languages - the computation of Mandelbrot images http://en.wikipedia.org/wiki/Mandelbrot_set.  Python will provide the high-level "glue" which brings everything together in a nice programming environment, and C will provide the raw power for the loop that actually computes the pixels.  My initial tests show this loop running about 100 times faster in C than in Python.  

The challenge is to do this without overwhelming the students.  The plan is to make everything as simple as possible, just follow the instructions, except the loop itself, which the students will write in C, based on what I have written in Python.  See http://ece.arizona.edu/~edatools/ece175/projects/mandelbrots/mbrotHW.html.

Suggestions are welcome.  Has anyone done something like this before?  Can you improve on my code (I'm not a Python expert), or even suggest something entirely different?

There is one major piece I would like to add to what I have so far - output graphics.  This demo would really be cool if the students could see these glorious images appear on their screen instead of an array of numbers.  I looked at the Python Imaging Library http://www.pythonware.com/products/pil/index.htm, and I don't see any examples that I can work from in converting an array of numbers into an image, just a lot of dense reference material that assumes I already know these image data formats.  Maybe there is a simpler way.  Help from someone with experience in Python graphics would be most appreciated.

-- Dave 



_______________________________________________
Edu-sig mailing list
Edu-sig at python.org
http://mail.python.org/mailman/listinfo/edu-sig





-----Inline Attachment Follows-----

_______________________________________________
Edu-sig mailing list
Edu-sig at python.org
http://mail.python.org/mailman/listinfo/edu-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/edu-sig/attachments/20080312/e9b99197/attachment.htm 


More information about the Edu-sig mailing list