OT: Crazy Programming

Anton Vredegoor anton at vredegoor.doge.nl
Mon May 13 05:18:05 EDT 2002


On Mon, 13 May 2002 01:33:54 -0600, "Andrew Dalke"
<dalke at dalkescientific.com> wrote:

>That's not to say that Python isn't colorful or has its own set of
>alternatives.  But the Python way isn't "There's More Than One Way
>To Do It" nor the opposite of "There's one and only one way to do it"
>but instead is "There should be one -- and preferably only one --
>obvious way to do it."  Those "preferably" and "obvious" qualifiers
>are important.

Sometimes when programming numerical python or three D models there
goes a lot of thinking inside very few lines of code. As an example
there's a program for visual python below that draws a colored cube.
It was originally a lot longer before I condensed it into this simpler
form. I suspect if all this simplifying and condensing would go on the
result would  be the ultimate program of length zero.

Anton.

#cube.py for visual python,  by Anton Vredegoor 
# (anton at vredegoor.doge.nl) may 2002
"""
    This program draws a 3d cube with colored sides.To rotate 
    the cube press the right-mouse button and move the mouse.
    To resize the cube press the middle mouse button and move
    the mouse in the  up-down direction.The cube is drawn side
    after side, each side is drawn by drawing two triangles.
    Cornerpoints are identified by numbers and unranked to lists
    of 3 items which represent coordinates in xyz dimensions.
"""

from visual import *

def unrank(id):
    res = [1] * 3
    for i in range(2,-1,-1):
         if id & 1 :
            res[i] *= -1
         id >>= 1
    return res

def drawside(idlist,clr):
    faces(pos=[unrank(id) for id in idlist],  color=clr, frame=f,
	    normal=unrank(0))

def test():
    global f
    f = frame()
    #Colordata in rgb fractions. The six colors below have the
    #follwowing order:  orange,red,yellow,green,white,blue
    colors = [[1.0,0.5,0.0], [0.75,0.0,0.0], [1.0,0.9,0.0],
                  [0.0,0.70,0.0], [0.95,0.95,0.95], [0.0,0.0,0.75]]
    #For each side two triangles are drawn from six corner 
    #point id's. The six sides are drawn in this order: right,
    #left,front,back,top,bottom
    idlists = [[1,2,3,0,2,1],[5,6,4,7,6,5], [0,6,2,4,6,0],
                     [3,5,1,7,5,3], [1,4,0,5,4,1], [3,6,7,2,6,3]]
    for idlist, color in map(None,idlists,colors):
        drawside(idlist,color)

if __name__=='__main__':
    test()




More information about the Python-list mailing list