Teaching Python with Graphics - PowerPoint PPT Presentation

About This Presentation
Title:

Teaching Python with Graphics

Description:

How might we generate interesting graphics while learning to ... quaternion * quaternion. OSCON 2004. 22. Python is Interactive! v0 = Vector3d(1,1,0) ... – PowerPoint PPT presentation

Number of Views:218
Avg rating:3.0/5.0
Slides: 44
Provided by: 4dsolu
Category:

less

Transcript and Presenter's Notes

Title: Teaching Python with Graphics


1
Teaching Python with Graphics
  • by Kirby Urner

2
The Challenge
  • How might we generate interesting graphics
    while learning to program in core Python?

OSCON 2004
2
3
Pitfalls
  • Getting lost in an elaborate API that takes
    attention away from core Python
  • Too much code for learning purposes (smallish
    projects required)
  • Resulting graphics are trivial and/or
    uninteresting

OSCON 2004
3
4
Some Solutions
  • Wrap the graphics API, or use just a few of an
    applications features
  • Recycle code use classes to isolate API-specific
    methods from generic methods
  • Look for reusable data in text format and/or use
    simple algorithms that generate complex patterns

OSCON 2004
4
5
(No Transcript)
6
Example Using POV-Ray
  • Simplify API edge, point, face
  • Gradually build up a generic Vector class
  • Sources of polyhedra on the web

OSCON 2004
6
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
Standard Core Python
def getedges (faces) """ Return unique
edges from a list of faces -- edges(f) for
rotate/zip) """ alledges sets.Set()
for f in faces for p in edges( f )
e list( p ) e.sort( )
alledges.add( tuple(e) ) return list(alledges)
OSCON 2004
11
12
(No Transcript)
13
A POV-Ray Writer
class Povray (object) """Skeletal outline of a
POV-Ray writer object" " " def __init__
(self, filename) self.output
file(filename, 'w') self.header() def
header (self) pass def edge (self, v0, v1)
pass def face (self, vlist) pass def
point (self, v0) pass def close (self)
self.output.close()
OSCON 2004
13
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
Vector Module
  • How much detail depends on mathematical needs,
    interests, goals of student
  • Prime example of where operator overloading makes
    so much sense
  • Easy to play with interactively (in shell), even
    if the code is pre-written (many examples of
    vectors.py out there)

OSCON 2004
20
21
(No Transcript)
22
Python is Interactive!
gtgtgt v0 Vector3d(1,1,0) gtgtgt v0 Vector (1, 1,
0) gtgtgt v0.rotate(90,axis'x') Vector (1.0,
2.22044604925e-016, 1.0) gtgtgt v0.rotate(90,axis'y
') Vector (2.22044604925e-016, 1.0, -1.0) gtgtgt
v0.spherical() (1.4142135600000001, 90.0,
45.0) gtgtgt v0.rotate(90,axis'y').spherical() (1.4
142135600000001, 135.0, 90.0)
OSCON 2004
22
23
(No Transcript)
24
(No Transcript)
25
OSCON 2004
25
26
Final Puzzle Piece A Shape Class
class Shape def __init__ (self, faces,
points) def translate (self, v) def
rotate (self, degrees, axis) def getcenter
(self) def center (self) def
scale (self, factor) def
__repr__ (self) def display (self, output)
OSCON 2004
26
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
Core Python Topics Explored
  • Edges from Faces data structures, import
  • String substitution POV-Ray methods
  • Classes POV-Ray writer, Shape
  • Operator Overloading Vector class
  • File I/O parsing input, writing output

OSCON 2004
31
32
(No Transcript)
33
(No Transcript)
34
(No Transcript)
35
(No Transcript)
36
Summay of PIL example
  • Focus is on mathematics PIL does not get in the
    way
  • Core algorithm is simple, result is complex
  • Python may clarify mathematical relationships
    (Mandelbrot as subclass of Julia)

OSCON 2004
36
37
(No Transcript)
38
(No Transcript)
39
gtgtgt rule nks.makerule(30) wolfram numbering
scheme a useful Python exercise gtgtgt
nks.sayrule(rule) 111 --gt 0 110 --gt 0 101 --gt
0 100 --gt 1 011 --gt 1 010 --gt 1 001 --gt 1 000 --gt
0
OSCON 2004
39
40
(No Transcript)
41
Motion Three Demonstrations
  • Python POV-Ray with a built-in clock
    generates animations, including mpeg clips
  • VPython provides an API to OpenGL for real time
    experience (similar to VRML) PyGeo an example
    of an app that wraps VPython
  • PyGame provides an API to 2D sprites, with
    collision detection and sound

OSCON 2004
41
42
(No Transcript)
43
Thank You
  • ContactKirby Urner kirby_at_4dsolutions.net
  • These slides and source codehttp//www.4dsolutio
    ns.net/oscon2004/(and via the official OSCON
    site)

OSCON 2004
43
Write a Comment
User Comments (0)
About PowerShow.com