"""
Kirby Urner
4D Solutions
First published: Apr 27 2007
Upgraded: Apr 29, 2007: refactored to use polyhedra module
Upgraded: May 3, 2007: added 'Many Mes' test of scaling, translating

Simple framework for studying VPython

Dependencies (outside of Standard Library):

http://www.4dsolutions.net/ocn/python/stickworks.py
http://www.4dsolutions.net/ocn/python/polyhedra.py

"""

from visual import sphere, color, display, cylinder
from random import randint
from stickworks import Vector, Edge
from polyhedra import Tetrahedron, Icosahedron
from math import sqrt

red = color.red
green = color.green
blue = color.blue
orange = color.orange
cyan = color.cyan
#... (add more?)


somecolors = [red, green, blue, orange, cyan ] # more?


phi = (1 + sqrt(5))/2.0

def makespheres(n=100):
    for i in range(n):  # a loop

        x, y, z = randint(-10, 10), randint(-10,10), randint(-10,10)
        theball = sphere(pos=(x,y,z),  # linebreak OK cuz within ( )

                         radius = 0.1 * randint(1,5),  
                         color = somecolors[randint(0,len(somecolors)-1)])

def makecyls(n=100):
    for i in range(n):  # a loop

        x, y, z = randint(-10, 10), randint(-10,10), randint(-10,10)        
        thecyl = cylinder(pos=(0,0,0), axis=(x,y,z),
                          radius = 0.1 * randint(1,5), # decimalize

                          color = somecolors[randint(0,len(somecolors)-1)])

def maketent():
    sometetra = Tetrahedron()
    sometetra.draw()        

def makeicosa():
    someicosa = Icosahedron()
    someicosa.draw()

def manymes(n = 30):
    for i in range(n):
        color = somecolors[randint(0,len(somecolors)-1)]
        Edge.color = color
        icosa = Icosahedron() * randint(1, 5) # scale it with __mul__

        moveit = Vector( (randint(-25,25), randint(-25,25), randint(-25,25)) )
        icosa = icosa + moveit # translate it with __add__

        icosa.draw()        

def test():

    # create a scene window

    scene2 = display(title='Some VPython Tests',
        width=600, height=600,
        center=(0,0,0),
        background=(0,1,1))

    """list the functions"""
    thetests = [
        makespheres,  # balloon qyoob

        makecyls,     # silly cyls

        maketent,     # tetra tent

        makeicosa,    # i, icosa

        manymes]       # many me

    
    while True:
        print """
        Choose:
        0  Balloon Qyoob
        1  Silly Cyls
        2  Tetra Tent
        3  I, Icosa
        4  Many Mes
        Q  Outta here!
        
        """

        ans = raw_input('Choice? ')
        
        if ans in 'Qq':
            break

        # trap more errors here

        
        thetests[int(ans)]()  # perform user selection (or crash?)


        print "View output, hit Enter to continue..."

        # pause to look in the VPython window

        ok = raw_input()

        for obj in scene2.objects:  # erase all objects

            obj.visible = False
            
    # outta here

    scene2.visible = False  # destroy the scene window

    return # null


if __name__ == '__main__':
    test()

# code highlighted using py2html.py version 0.8