1. Graph.. find the area between curves:
2. Graphical Tools
3. Mastermind: Find Code
4. 2 D Collisions
5. Project
Color Click:
from visual import *
ball=sphere(color=color.red)
bcolors = ["100","010","001","110","101","011"]
cpos=0
while 1:
if scene.mouse.clicked:
m = scene.mouse.getclick()
if (m.pos.x<ball.pos.x+.5) and (m.pos.x>ball.pos.x-.5) and (m.pos.y>ball.pos.y-.5) and (m.pos.y<ball.pos.y+.5):
ball.color = (int(bcolors[cpos][0]),int(bcolors[cpos][1]),int(bcolors[cpos][2]))
cpos=cpos+1
if cpos==6: cpos=0
Color Click Pos
from visual import *
c1 = scene.mouse.getclick()
earth = sphere(pos=(c1.pos.x,c1.pos.y,0),color=color.blue)
c1 = scene.mouse.getclick()
venus = sphere(pos=(c1.pos.x,c1.pos.y,0),color=color.green)
c1 = scene.mouse.getclick()
mars = sphere(pos=(c1.pos.x,c1.pos.y,0),color=color.red)
print "blue to green dist =",mag(earth.pos-venus.pos)
print "green to red",mag(venus.pos-mars.pos)
print "blue to red" ,mag(earth.pos-mars.pos)
Graphing:
from visual.graph import *
from visual.factorial import factorial
xmax = input("enter domain range")
xmin = -xmax
myfunction = raw_input("enter function ")
myderivative = raw_input("enter the derivative")
#define a function to approximate and enter its derivative
def f(x): return eval(myfunction)
def g(x): return eval(myderivative)
dx = (xmax-xmin)/512.0 # chop domain into 512 deltas
graph = gdisplay(ymin=xmin, ymax=xmax,xtitle=myfunction)
curve = gcurve(color=color.yellow)
curve2 = gcurve(color=color.red)
#draw the function itself
for x in arange(xmin,xmax,dx): curve.plot(pos=(x,f(x)))
for x in arange(xmin,xmax,dx): curve2.plot(pos=(x,g(x)))
from visual.graph import *
from visual.factorial import factorial
xmax = input("enter domain range")
xmin = -xmax
myfunction = raw_input("enter function ")
def f(x): return eval(myfunction)
dx = (xmax-xmin)/512.0 # chop domain into 512 deltas
graph = gdisplay(ymin=xmin, ymax=xmax,xtitle=myfunction)
curve = gcurve(color=color.yellow)
area=0.0
#draw the function itself
for x in arange(xmin,xmax,dx):
curve.plot(pos=(x,f(x)))
#draw the area under the curve between 0 and 2: add area of each bar
funct2 = gvbars(color=color.red,delta=dx)
rate(2)
for x in arange(0,2,dx):
funct2.plot(pos=(x,f(x)))
area=area+dx*f(x)
print area
2D Collision
from visual import *
scene.width=900
scene.height=600
scene.autoscale=0
ball = sphere(pos=(-9,0,0), color=color.blue,radius=.75)
ball2 = sphere(pos=(2,1.3,0), color=color.red,radius=.75)
while 1:
mass = input("enter mass of the left ball")
mass2= input("enter the mass of the right ball")
mass=float(mass)
mass2=float(mass2)
mg = input("input the velocity of the ball")
mg=float(mg)
ball.velocity = vector(mg,0,0)
ball2.velocity = vector(-1,0,0)
d=.001
dist = vector(ball.pos-ball2.pos)
while 1:
rate(100)
if mag(dist)<=1.5:
normvec=norm(dist)
normtan=vector(-normvec.y,normvec.x,0)
v1norm= dot(normvec,ball.velocity)
v1tang = dot(normtan,ball.velocity)
v2norm= dot(normvec,ball2.velocity)
v2tang = dot(normtan,ball2.velocity)
v1fnorm = (v1norm * (mass-mass2)+2*mass2*v2norm)/mass+mass2
v2fnorm = (v2norm * (mass2-mass)+2*mass*v1norm)/mass+mass2
v1fnorm = v1fnorm*normvec
v1tang = v1tang*normtan
v2fnorm=v2fnorm*normvec
v2tang=v2tang*normtan
ball.velocity=v1fnorm+v1tang
ball2.velocity=v2fnorm+v2tang
ball.pos=ball.pos+ball.velocity*d
ball2.pos=ball2.pos+ball2.velocity*d
dist = ball.pos-ball2.pos
if ball.pos.x>10 or ball.pos.x<-10:
ball.velocity.x=-ball.velocity.x
if ball.pos.y>6 or ball.pos.y<-6:
ball.velocity.y=-ball.velocity.y
ball.pos = ball.pos + ball.velocity*d
if ball2.pos.x>10 or ball2.pos.x<-10:
ball2.velocity.x=-ball2.velocity.x
if ball2.pos.y>6 or ball2.pos.y<-6:
ball2.velocity.y=-ball2.velocity.y
print mag(ball.velocity)*mass-mag(ball2.velocity)*mass2