[Tutor] Vpython

Jacob S. keridee at jayco.net
Sun Dec 19 01:21:41 CET 2004


> > I was wondering were can I find some Vpython graphics program codes that
are readily available.

I have 2 that I am proud of.

A simple function grapher
Usage:
>0.125*x**2

Graphs y = 1/8*x**2

>y = 1/x

Graphs y = 1/x

>clear

Clears display window

>r = t**2

Graphs polar function r = t**2

>remove lines

Removes all axes, including polar axes, should a polar function be graphed
after

>return lines

Returns all lines, so that they appear when appropriate.

>x = sin(x**2)+log(x**2)

Graphs the function...


------------------------------------------------------

from __future__ import division
from visual import *
import os
from math import *
ja = 0

def start():
    for objects in scene.objects:
        objects.visible = 0
    scene.title = "Function Grapher by Jacob, Inc."
    tem = raw_input('Are you on a desktop, or a notebook? ')
    if tem == 'desktop':
        scene.x = 365
    if tem == 'notebook':
        scene.x = 574
    scene.visible=1
    scene.exit=0
    scene.userspin = 0
    scene.range=(10,10,1)
    scene.background=(1,1,1)
    global xaxis
    global yaxis
    xaxis = curve(color=color.black)
    xaxis.append(pos=(100,0,0))
    xaxis.append(pos=(-100,0,0))
    yaxis = curve(color=color.black)
    yaxis.append(pos=(0,100,0))
    yaxis.append(pos=(0,-100,0))
    global radiusaxis
    global radiusaxis2
    radiusaxis = curve(pos=[(-100,-100),(100,100)],color=color.black)
    radiusaxis2 = curve(pos=[(-100,100),(100,-100)],color=color.black)
    radiusaxis.visible = 0
    radiusaxis2.visible = 0

start()
y = 3
m = 0
t = 0
d = 1
print """\
List of Commands:
clear
quit
remove lines
return lines

Function Syntax:
var = funct a,b
where var = what
and funct = f(what)
and a,b = range
"""

print 'Please type in functions in below. '
while y != "":
    lists=[]
    y = raw_input('>')
    if y == 'clear':
        scene.visible=0
        start()
        print "-"*36
        continue
    elif y == 'quit':
        scene.visible = 0
        del scene
        break
    elif y == 'remove lines':
        a = [radiusaxis,radiusaxis2,xaxis,yaxis]
        for x in a:
            x.visible = 0
        d = 0
        continue
    elif y == 'return lines':
        a = [radiusaxis,radiusaxis2,xaxis,yaxis]
        for x in a:
            x.visible = 1
        d = 1
        continue
    if y.count('=') == 1:
        y = y.split(' = ')
        type = y[0].lower()
        y = y[1]
        y = y.replace("y","x")
        if type == 'r':
            y = y.replace('x','t')
            if d == 1:
                radiusaxis.visible = 1
                radiusaxis2.visible = 1
    else:
        type = 'y'
    y = y.split(" ")
    if len(y) > 1:
        pass
    else:
        if type == 'r':
            y.append('0,5*pi')
        else:
            y.append('-10,10')
    range = y[1]
    y = y[0]
    range = range.split(",")
    min = float(eval(range[0]))
    max = float(eval(range[1]))
    lists.append(curve(color=(1,0,1)))
    x = min
    if type == 'y' or type == 'x':
        radiusaxis.visible = 0
        radiusaxis2.visible = 0
        while x >= min and x <= max:
            x = x+0.005
            try:
                if eval(y) <= 15 and eval(y) >= -15:
                    if type == 'y':
                        lists[-1].append(pos=(x,eval(y),0))
                    elif type == 'x':
                        lists[-1].append(pos=(eval(y),x,0))
                else:
                    lists.append(curve(color=(1,0,1)))
            except:
                pass
    elif type == 'r':
        m = 'eval(y)*cos(t)'
        n = 'eval(y)*sin(t)'
        t = min
        while t >= min and t <= max:
            try:
                lists[-1].append(pos=(eval(m),eval(n),0))
            except:
                lists.append(curve(color=(1,0,1)))
            t = t+0.005




More information about the Tutor mailing list