VPYTHON程式的基本學習-1

在這個講次當中我們學習如何使用VPYTHON,包括對於物件的定義,物件如何進行運動的動畫控制。
vpython物件與動畫的基本解說影片




1. 基本物件與運動




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
    
    VPY04.py

  • 運動軌跡
  • 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
    
    VPY05.py (運動軌跡)

  • 隱藏或可見一個物件(visible)
  • Web VPython 3.2
    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(2)
        balls[t].visible=False  #隱藏物件
        t+=1
    sleep(1)
    
    for i in range(10):
        balls[i].visible=True
    sleep(0.5)
    for i in range(10):
        balls[i].visible=False
    sleep(0.5)
    
    t=0
    while t < 10:
        rate(2)
        balls[9-t].visible=True  #物件可見
        t+=1
    
    VPY06.py (隱藏/可見一個物件)

    小練習2: 10個球+10個箭頭的運動(youtube)





    code for you to try:

    Web VPython 3.2
    balls=[sphere(pos=vector(i*2,0,0), radius=0.5, color=color.cyan) for i in range(10)]
    arrs=[arrow(pos=vector(i*2,-2,0), axis=vec(0,4,0), shaftwidth=0.05, headwidth=0.2, color=color.yellow) for i in range(10)]
    Y=arrow(pos=vector(-2,-2,0), axis=vec(0,18,0), shaftwidth=0.05, headwidth=0.2, color=color.white)
    scene.center=vec(10,8,0)   #視窗的中央點座標
    scene.pause("click to start")
    t=0; dt=0.1; vy=1
    while t < 20:
        print('t1=',t)
        rate(30)
        for i in range(10):
            arrs[i].pos.y += vy*dt
            if(arrs[i].pos.y > 10): arrs[i].visible=False
        t+=dt
        
    t=0; dt=0.1; vy=1
    while t < 20:
        print('t1=',t)
        rate(30)
        for i in range(10):
            balls[i].color=color.yellow
            balls[i].radius = 0.5+0.3*cos(t)
        t+=dt
    


    3. 運動控制 pause


  • 使用scene.pause('step 1\nclick:move in +x')
  • opacity(透明度)=0~1。1=完全不透明。
    所有運動結束之後,請按著滑鼠的右鍵,帶著轉動整個畫面可以看到三維的軌跡。
    Web VPython 3.2
    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,-20), 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+=2
    
    VPY07.py




    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)
    
    run this code in glowscript





    Christmas

    想製作一個動畫送給你的朋友嗎?你可以參考下面這個程式,如果很想弄懂的話可以跟老師聯絡。



    Christmas-Tree-YCC