VPYTHON程式的基本學習-1
在這個講次當中我們學習如何使用VPYTHON,包括對於物件的定義,物件如何進行運動的動畫控制。
1. 基本物件與運動
- sphere and box
- sphere: pos; radius; color
- box: pos; size; axis; color
GlowScript 3.0 VPython
ball=sphere(pos=vector(-5,0,0), radius=0.5, color=color.cyan)
wallR=box(pos=vector(6,0,0), size=vector(0.2,12,12), color=color.green)
how to make the objects move
GlowScript 3.0 VPython
ball = sphere(pos=vector(-5,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=vector(6,0,0), size=vector(0.2,12,12), color=color.green)
v=10; t=0; dt=0.1;
while t < 2:
rate(10)
ball.pos.x+=v*dt
t+=dt
讓球反彈
GlowScript 3.0 VPython
ball = sphere(pos=vector(-5,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=vector(6,0,0), size=vector(0.2,12,12), color=color.green)
v=10; t=0; dt=0.1;
while t < 2:
rate(10)
ball.pos.x+=v*dt
if ball.pos.x > wallR.pos.x:
v=-v
t+=dt
2. 更多物件與運動控制
使用箭頭物件來標示速度向量
GlowScript 3.0 VPython
ball = sphere(pos=vector(-6,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=vector(6,0,0), size=vector(0.2,12,12), color=color.green)
v=10; t=0; dt=0.1; ball.v=vec(v*0.2,0,0)
varr = arrow(pos=ball.pos, axis=ball.v, color=color.yellow)
scene.waitfor('click')
while t < 1.8:
rate(10)
ball.pos.x+=v*dt
varr.pos=ball.pos
t+=dt
運動軌跡
GlowScript 3.0 VPython
ball_1=sphere(pos=vector(-20,0,0), radius=0.5, color=color.cyan, make_trail=True)
ball_2=sphere(pos=vector(0,-20,0), radius=0.5, color=color.green, make_trail=True)
scene.autoscale = False
v=10; t=0; dt=0.1;
while t < 2:
rate(10)
ball_1.pos.x+=v*dt
ball_2.pos.x+=v*dt
ball_2.pos.y+=v*dt
t+=dt
隱藏或可見一個物件(visible)
GlowScript 3.0 VPython
balls=[sphere(pos=vector(i,0,0), radius=0.5, color=color.cyan) for i in range(10)]
scene.center=vec(5,0,0) #視窗的中央點座標
t=0
while t < 10:
rate(1)
balls[t].visible=False #隱藏物件
t+=1
3. 運動控制 pause
使用scene.pause('step 1\nclick:move in +x')
opacity(透明度)=0~1。1=完全不透明。
所有運動結束之後,請按著滑鼠的右鍵,帶著轉動整個畫面可以看到三維的軌跡。
GlowScript 3.0 VPython
scene.center=vec(3,3,0)
box1=box(pos=vec(0,0,0),size=vec(12,12,12),color=color.green,opacity=0.3)
ball = sphere(pos=vector(0,0,0), radius=0.5, color=color.cyan, make_trail=True)
scene.pause('step 1\nclick:move in +x')
for i in range(20):
rate(10)
ball.pos.x+=0.2
scene.pause('step 2\nclick:move in +y')
for i in range(20):
rate(10)
ball.pos.y+=0.2
scene.pause('step 3\nclick:move in +z')
for i in range(20):
rate(10)
ball.pos.z+=0.2
4. 數學函數
abs(x)
sqrt(x)
sin(x)
cos(x)
tan(x)
asin(x) # arc sine
acos(x) # arc cosine
atan(x) # arc tangent; -pi/2 to pi/2
atan2(y,x) # angle whose tangent is y/x; -pi to pi
sqrt(x) # square root
exp(x) # e to the x
log(x) # natural log, base e
# log(x)/log(10) gives log base 10
pow(x,y) # x to the power y
pi # 3.14159....
ceil(x) # round up to nearest integer
floor(x) # round down to nearest integer
sign(x) # +1 if x > 0, -1 if x < 0, 0 if x == 0
round(x) # round to nearest integer
max(x,y,z) # the largest of x,y,z
min(x,y,z) # the smallest of x,y,z
random() # pseudorandom number 0 to 1
factorial(x) # x! = x*(x-1)*(x-2)....(1)
combin(x,y) # x!/(y!*(x-y)!)
max(a,b,c,..) # maximum of these
min(a,b,c,..) # minimum of these
5. 向量函數
Vector functions
mag(A) = A.mag = |A|, the magnitude of a vector
mag2(A) = A.mag2 = |A|*|A|, the vector's magnitude squared
norm(A) = A.norm() = A/|A|, a unit vector in the direction of the vector
hat(A) = A.hat = A/|A|,
dot(A,B) = A.dot(B) = A dot B, the scalar dot product between two vectors
cross(A,B) = A.cross(B), the vector cross product between two vectors
diff_angle(A,B) = A.diff_angle(B), the angle between two vectors, in radians
proj(A,B) = A.proj(B) = dot(A,norm(B))*norm(B), the vector projection of A along B
comp(A,B) = A.comp(B) = dot(A,norm(B)), the scalar projection of A along B
A.equals(B) is True if A and B have the same components (which means
that they have the same magnitude and the same direction).
vector.random() produces a vector each of whose
components is a random number in the range -1 to +1
cross(A,B) or A.cross(B) gives the cross product of two vectors
dot(A,B) or A.dot(B) gives the dot product of two vectors
Rotating a vector
There is a function for rotating a vector:
v2 = rotate(v1, angle=a, axis=vector(x,y,z))
v2 = v1.rotate(angle=a, axis=vector(x,y,z))
There are functions for converting between degrees and radians
radians(360)
degrees(pi)
GlowScript 3.0 VPython
A=vector.random()
B=vector.random()
print('A=',A,' B=',B)
print('|A|,A^2=',mag(A),mag2(A))
print('norm(A)=',norm(A))
print('hat(A)=',hat(A))
print('A.B=',dot(A,B))
print('AxB=',cross(A,B))
dth=diff_angle(A,B)
print('dth=',dth,degrees(dth))
C=proj(A,B)
D=comp(A,B)
A.equals(B)
cross(A,B)
v1=vec(1,1,1)
v2 = rotate(v1, angle=radians(90), axis=vector(0,0,-1))
print(v1,v2)
O=vec(0,0,0)
X=arrow(pos=O, axis=vec(2,0,0),color=vec(1,0,1), shaftwidth=0.02)
Y=arrow(pos=O, axis=vec(0,2,0),color=vec(1,1,1), shaftwidth=0.02)
Z=arrow(pos=O, axis=vec(0,0,2),color=vec(1,0,1), shaftwidth=0.02)
arrow(pos=O, axis=v1,color=vec(1,1,0), shaftwidth=0.04)
N=20
for i in range(N):
dth=2*pi/N*i
v2 = rotate(v1, angle=dth, axis=vector(0,1,0))
arrow(pos=O, axis=v2, color=vec(0,0.5,1), shaftwidth=0.02)
arrow(pos=O, axis=proj(v1,Y.axis), color=vec(1,1,0), shaftwidth=0.04)