forked from BruceSherwood/vpython-wx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
85 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,105 @@ | ||
from visual import * | ||
# demonstration of vector cross product | ||
#!/usr/bin/env python | ||
#coding:utf-8 | ||
""" | ||
Demonstration of vector cross product | ||
print(""" | ||
Vector cross product: Red cross Green = Yellow | ||
Drag to change green vector | ||
Click to toggle fixed angle or fixed length | ||
""") | ||
""" | ||
from __future__ import print_function, division | ||
|
||
import visual as vp | ||
|
||
print(__doc__) | ||
|
||
# Ruth Chabay | ||
|
||
scene.title="Vector Cross Product" | ||
scene.width=600 | ||
scene.height=600 | ||
vp.scene.title = "Vector Cross Product" | ||
vp.scene.width = 600 | ||
vp.scene.height = 600 | ||
R = 0.15*4 | ||
plane = curve(pos=[(0, -10, -10), (0, -10, 10), (0, 10,10), (0, 10, -10), | ||
(0, -10, -10), (0,-6,-10), (0,-6,10), (0, -2, 10), (0, -2, -10), | ||
(0,2,-10), (0,2,10), (0,6,10), (0,6,-10),(0,10, -10), | ||
(0,10,-6), (0,-10,-6), (0,-10,-2), (0,10,-2), (0,10,2), | ||
(0,-10,2), (0,-10,6), (0,10,6)]) | ||
PLANE = vp.curve( | ||
pos=[(0, -10, -10), (0, -10, 10), (0, 10, 10), (0, 10, -10), | ||
(0, -10, -10), (0, -6, -10), (0, -6, 10), (0, -2, 10), (0, -2, -10), | ||
(0, 2, -10), (0, 2, 10), (0, 6, 10), (0, 6, -10), (0, 10, -10), | ||
(0, 10, -6), (0, -10, -6), (0, -10, -2), (0, 10, -2), (0, 10, 2), | ||
(0, -10, 2), (0, -10, 6), (0, 10, 6)]) | ||
|
||
S_THETA = vp.sphere(pos=(0, -12, -10), radius=0.6, color=(0.6, 1.0, 0.6)) | ||
S_THETA_LABEL = vp.label(pos=S_THETA.pos, text="Fix Angle", yoffset=-5, | ||
opacity=0, box=0, line=0) | ||
S_LENGTH = vp.sphere(pos=(0, -12, 10), radius=0.6, color=(0.6, 0.6, 1.0)) | ||
S_LENGTH_LABEL = vp.label(pos=S_LENGTH.pos, text="Fix Length", yoffset=-5, | ||
opacity=0, box=0, line=0) | ||
|
||
s_theta=sphere(pos=(0,-12,-10), radius=0.6, color=(0.6, 1.0, 0.6)) | ||
s_theta_label=label(pos=s_theta.pos, text="Fix Angle", yoffset=-5, | ||
opacity=0, box=0, line=0) | ||
s_length=sphere(pos=(0,-12,10), radius=0.6, color=(0.6, 0.6, 1.0)) | ||
s_length_label=label(pos=s_length.pos, text="Fix Length", yoffset=-5, | ||
opacity=0, box=0, line=0) | ||
S_TEXT = vp.label(pos=(0, 12, 0), text="Yellow = Red x Green", | ||
opacity=0, box=0, line=0) | ||
|
||
s_text=label(pos=(0,12,0), text="Yellow = Red x Green", | ||
opacity=0, box=0, line=0) | ||
|
||
fixlength = 0 | ||
fixtheta = 0 | ||
FIXLENGTH = 0 | ||
FIXTHETA = 0 | ||
|
||
avector = array ([0,0,-3.5]) | ||
bvector = vector (0,3,2) | ||
AVECTOR = vp.array([0, 0, -3.5]) | ||
BVECTOR = vp.vector(0, 3, 2) | ||
|
||
a = arrow(pos=(0,0,0), shaftwidth=R, color=color.red) | ||
b = arrow(pos=(0,0,0), axis=bvector, shaftwidth=R, color=color.green) | ||
a.axis =avector | ||
cvector = cross(avector,bvector) | ||
c = arrow(pos=(0,0,0), axis=cvector, shaftwidth=R, color=color.yellow) | ||
A = vp.arrow(pos=(0, 0, 0), shaftwidth=R, color=vp.color.red) | ||
B = vp.arrow(pos=(0, 0, 0), axis=BVECTOR, shaftwidth=R, color=vp.color.green) | ||
A.axis = AVECTOR | ||
CVECTOR = vp.cross(AVECTOR, BVECTOR) | ||
C = vp.arrow(pos=(0, 0, 0), axis=CVECTOR, shaftwidth=R, color=vp.color.yellow) | ||
|
||
scene.autoscale = 0 | ||
scene.forward = (-1,-.5,-1) | ||
vp.scene.autoscale = 0 | ||
vp.scene.forward = (-1, -.5, -1) | ||
|
||
drag = 0 | ||
DRAG = 0 | ||
|
||
while True: | ||
rate(100) | ||
if scene.mouse.events: | ||
m = scene.mouse.getevent() | ||
if m.drag: | ||
drag = True | ||
obs = None | ||
elif m.drop: | ||
drag = False | ||
elif m.click: | ||
if m.pick is s_length: | ||
if fixtheta: | ||
fixtheta = not(fixtheta) | ||
s_theta.color = (0.6, 1.0, 0.6) | ||
fixlength=not(fixlength) | ||
if fixlength: | ||
s_length.color=(0.0, 0.0, 1.0) | ||
vp.rate(100) | ||
if vp.scene.mouse.events: | ||
M = vp.scene.mouse.getevent() | ||
if M.drag: | ||
DRAG = True | ||
OBS = None | ||
elif M.drop: | ||
DRAG = False | ||
elif M.click: | ||
if M.pick is S_LENGTH: | ||
if FIXTHETA: | ||
FIXTHETA = not FIXTHETA | ||
S_THETA.color = (0.6, 1.0, 0.6) | ||
FIXLENGTH = not FIXLENGTH | ||
if FIXLENGTH: | ||
S_LENGTH.color = (0.0, 0.0, 1.0) | ||
else: | ||
s_length.color=(0.6, 0.6, 1.0) | ||
elif m.pick is s_theta: | ||
if fixlength: | ||
fixlength = not(fixlength) | ||
s_length.color=(0.6, 0.6, 1.0) | ||
fixtheta = not(fixtheta) | ||
if fixtheta: | ||
s_theta.color=(0.0, 1.0, 0.0) | ||
S_LENGTH.color = (0.6, 0.6, 1.0) | ||
elif M.pick is S_THETA: | ||
if FIXLENGTH: | ||
FIXLENGTH = not FIXLENGTH | ||
S_LENGTH.color = (0.6, 0.6, 1.0) | ||
FIXTHETA = not FIXTHETA | ||
if FIXTHETA: | ||
S_THETA.color = (0.0, 1.0, 0.0) | ||
else: | ||
s_theta.color = (0.6, 1.0, 0.6) | ||
if drag: | ||
rate(100) | ||
newobs=scene.mouse.project(normal=vector(1,0,0), d=0) | ||
if newobs and (newobs != obs): | ||
obs = newobs | ||
if not fixlength and not fixtheta: | ||
bvector = obs | ||
if bvector.mag > 20: bvector=bvector*(20/bvector.mag) | ||
b.axis=bvector | ||
elif fixlength: | ||
length=3.9 | ||
bvector = length*norm(obs) | ||
b.axis=bvector | ||
elif fixtheta: | ||
length=mag(obs) | ||
bvector=length*norm(vector(0, .3, 1)) | ||
b.axis=bvector | ||
S_THETA.color = (0.6, 1.0, 0.6) | ||
if DRAG: | ||
vp.rate(100) | ||
NEWOBS = vp.scene.mouse.project(normal=vp.vector(1, 0, 0), d=0) | ||
if NEWOBS and (NEWOBS != OBS): | ||
OBS = NEWOBS | ||
if not FIXLENGTH and not FIXTHETA: | ||
BVECTOR = OBS | ||
if BVECTOR.mag > 20: | ||
BVECTOR = BVECTOR*(20/BVECTOR.mag) | ||
B.axis = BVECTOR | ||
elif FIXLENGTH: | ||
LENGTH = 3.9 | ||
BVECTOR = LENGTH*vp.norm(OBS) | ||
B.axis = BVECTOR | ||
elif FIXTHETA: | ||
LENGTH = vp.mag(OBS) | ||
BVECTOR = LENGTH*vp.norm(vp.vector(0, 0.3, 1)) | ||
B.axis = BVECTOR | ||
|
||
cvector=cross(avector,b.axis) | ||
c.axis=cvector | ||
CVECTOR = vp.cross(AVECTOR, B.axis) | ||
C.axis = CVECTOR | ||
|