Draft math/CS stuff (satacad 6938)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Draft math/CS stuff (satacad 6938)

Kirby Urner

I'd be happy if anyone wants to comment on and/or mess with the code below.
It's what I'm working on for my upcoming Saturday Academy class, which uses
a combination of Python and POV-Ray to teach geometry.
http://www.saturdayacademy.org/classes/ClassDetail.aspx?id=6938

Some of my background info is on file here (another list) i.e. I talk about
what's unfinished:
http://mail.geneseo.edu/pipermail/math-thinking-l/2005-September/000840.html

plus I'm still looking at properties, and continuing some recent threads re
pedagogy here on our edu-sig list.

Also, math-think-l unindented/trashed the source, perhaps because I
submitted in rich text through gmail, whereas a plain text submission
might've gotten through unscathed (??).  

Kirby

=====

class Vector(object):

    color = 'Red'
   
    def __init__(self, xyz):
        self.xyz = xyz

    def write(self):
        basic = "cylinder <0,0,0>, <%s,%s,%s>, 0.1" % self.xyz
        return "%s %s" % (basic, "pigment { color %s }" % self.color)
   
    def __repr__(self):
        return "Vector <%s, %s, %s>" % self.xyz
           
    def __mul__(self, scalar):
        xyz = tuple([scalar * i for i in self.xyz])
        return Vector(xyz)

    def __add__(self, other):
        xyz = tuple([ i+j for i,j in zip(self.xyz, other.xyz)])
        return Vector(xyz)

    def _get_xyz(self):
        return '<%s, %s, %s>' % self.xyz

    def _get_length(self):
        return pow(sum([i**2 for i in xyz]), 0.5)
 
    coords = property(_get_xyz)

    length = property(_get_length)
   

class Edge(object):

    color = 'Red'

    def __init__(self, v0, v1):
        self.v0 = v0
        self.v1 = v1

    def __repr__(self):
        return "Edge %s, %s" % (self.v0.coords, self.v1.coords)

    def write(self):
        basic = "cylinder %s, %s, 0.1" % (self.v0.coords, self.v1.coords)
        return "%s %s" % (basic, "pigment { color %s }" % self.color)

   
class Polyhedron(object):

    color = 'Red'

    def __init__(self, vertsdict, faces):
        self.verts = vertsdict
        self.faces = faces
        self.edges = self._get_edges()

    def __repr__(self):
        return "Polyhedron: %s edges, %s faces" \
               % (len(self.edges), len(self.faces))

    def _get_edges(self):
        # stub procedure:  take a list of face-tuples and distill
        # all the unique edges, e.g. ((1,2,3)) => ((1,2),(2,3),(3,1))
        # e.g. icosahedron has 20 faces and 30 unique edges
        # ( = cubocta 24 + tetra's 6 edges to squares per jitterbug)
        pass
       
    def write(self):
        # stub procedure -- needs to be completed
        return 'write out edges'

    def __mul__(self, scalar):
        newvectors = {}
        for v in self.verts:
            newvectors[v] = self.verts[v] * scalar
        return Polyhedron(newvectors, self.faces)

class Header (object):

    # defaults
    bgcolor = "color Gray50"
    lightloc = "<300, 300, -1000>"
    lightcolor = "White"
   
    @staticmethod
    def write():
        return """
#include "colors.inc"

background { %(bgcolor)s }
light_source{ %(lightloc)s %(lightcolor)s }
""" % Header.__dict__


class Camera (object):

    # defaults
    look_at  = 0
    location = '<0, .1, -25>'
       
    @staticmethod
    def write():
        return """
camera {
  location %(location)s
  look_at %(look_at)s
  angle 30
}
""" % Camera.__dict__
   
def buildicosa():
   
    phi = (1 + pow(5,0.5))/2.0
   
    verts = {# 2*phi x 2 rectangle in XZ
              1:Vector((-phi,  0,  -1)),
              2:Vector((-phi,  0,   1)),
              3:Vector(( phi,  0,   1)),
              4:Vector(( phi,  0,  -1)),
             # 2*phi x 2 rectange in XY
              5:Vector(( -1,  phi,  0)),
              6:Vector((  1,  phi,  0)),
              7:Vector((  1, -phi,  0)),
              8:Vector(( -1, -phi,  0)),
             # 2*phi x 2 rectange in YZ
              9:Vector((  0,  1,  phi)),
             10:Vector((  0, -1,  phi)),
             11:Vector((  0, -1, -phi)),
             12:Vector((  0,  1, -phi))}
   
    faces = ((5,6,9),(5,6,2),(5,12,1),(5,6,1),(5,1,2),
             (11,1,12),(11,12,4),(11,1,8),(11,4,7),(11,7,8),
             (4,12,6),(4,6,3),(4,3,7),
             (3,9,6),(3,6,4),
             (10,7,8))  #unfinished
    return verts, faces

def main():
    icosa = Polyhedron(*buildicosa())
    f = file('icosa.pov','w')
    Camera.location = '<0, 2, -20>'
    print >> f, Header.write()
    print >> f, Camera.write()
    print >> f, icosa.write()
    f.close()

if __name__ == '__main__':
    main()
   


_______________________________________________
Edu-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/edu-sig
Reply | Threaded
Open this post in threaded view
|

Re: Draft math/CS stuff (satacad 6938)

Kirby Urner


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Kirby Urner
> Sent: Tuesday, September 13, 2005 4:49 PM
> To: [hidden email]
> Subject: [Edu-sig] Draft math/CS stuff (satacad 6938)
>
>
> I'd be happy if anyone wants to comment on and/or mess with the code
> below.

This curriculum segment has now been turned into a web page linked from my
CP4E page:  http://www.4dsolutions.net/ocn/polys.html

Kirby


_______________________________________________
Edu-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/edu-sig