VPYTHON簡介

在這個講次中我們學習如何在Glowscript IDE網頁平台,編輯與執行VPYTHON程式。大家要注意到,在這個平台上使用PYTHON語言編輯程式的時候,numpy跟matplotlib這些模組,無法呼叫進來使用。我們只能使用python內建的list這些函數,並結合vpython本身的模組,包括向量還有vpython專屬的物件。

more videos in youtube(Let's code physics)


  1. Glowscript: 編輯執行Vpython程式的網頁平台

  2. ▶Glowscript影片


    GlowScript 2.9 VPython
    v0=10; theta=radians(60.); g=9.8
    t=[0.1*i for i in range(11)]
    x = [v0*cos(theta)*i  for i in t]
    y = [v0*sin(theta)*i-0.5*g*i**2  for i in t]
    for i in t: print("{:7.3f}".format(i), end=" "); print()
    for i in x: print("{:7.3f}".format(i), end=" "); print()
    for i in y: print("{:7.3f}".format(i), end=" "); print('\n')
    for i in range(len(t)):
        print("{0:7.3f}{1:7.3f}{2:7.3f}".format(t[i],x[i],y[i]))
    
    ============輸出:=============
    
      0.000   0.100   0.200   0.300   0.400   0.500   0.600   0.700   0.800   0.900   1.000 
      0.000   0.500   1.000   1.500   2.000   2.500   3.000   3.500   4.000   4.500   5.000 
      0.000   0.817   1.536   2.157   2.680   3.105   3.432   3.661   3.792   3.825   3.760 
    
      0.000  0.000  0.000
      0.100  0.500  0.817
      0.200  1.000  1.536
      0.300  1.500  2.157
      0.400  2.000  2.680
      0.500  2.500  3.105
      0.600  3.000  3.432
      0.700  3.500  3.661
      0.800  4.000  3.792
      0.900  4.500  3.825
      1.000  5.000  3.760
    



  3. Glowscript: Vpython程式中加入物件

  4. GlowScript 2.9 VPython
    v0=10; theta=radians(60.); g=9.8; N=21
    t=[0.1*i for i in range(N)]
    x=[v0*cos(theta)*i  for i in t]
    y=[v0*sin(theta)*i-0.5*g*i**2  for i in t]
    z=[0 for i in range(N)]
    
    scene = display(width=800, height=800, center=vec(3,0,0),background=vec(0.2,0.5,0.2)) 
    ball=sphere(pos=vec(4,0,0),radius=0.3, color=color.yellow)
    balls=[sphere(pos=vec(x[i],y[i],z[i]),radius=0.1,color=color.blue) for i in range(N)]
    Xaxis=arrow(pos=vec(0,0,0), axis=vec(10,0,0), shaftwidth=0.05, color=vec(0,1,0))
    
    



  5. Vpython的物件

  6. 常用的物件共有7個display, arrow, box, sphere, ring, cylinder, helix 共7個。每一個物件都有自己的參數,同學可以從觀察程式與視窗中的物件樣式得知。
    1. display:開啟一個動畫播放的視窗, 視窗的寬(width)和高(height),中心點的座標(center),背景顏色(background),視窗的觀看方向(forward)
    2. arrow:箭頭,箭頭的位置(pos),軸向(axis),箭頭的大小(shaftwidth)和顏色(color)
    3. box:盒子,盒子的中心點位置(pos),盒子的長寬高(size),盒子的顏色(color),盒子的透明度(opacity)
    4. sphere:球,球的球心位置(pos),半徑(radius),顏色(color)
    5. ring:圓環,圓環的環心位置(pos),圓環的半徑(radius),圓環的寬度(width)
    6. cylinder:圓柱體,圓柱體的底面圓心座標(pos),圓柱體的半徑(radius),圓柱體的軸心向量(axis)
    7. helix:螺旋線,螺旋線的底端座標(pos),螺旋軸向量(axis),螺旋的半徑(radius),螺旋線的寬度(width)


    ▶VPYTHON 7物件影片(Vpython 6版本)

    '''
    1. display:開啟一個動畫播放的視窗, 視窗的寬(width)和高(height),
      中心點的座標(center),背景顏色(background),視窗的觀看方向(forward)
    2. arrow:箭頭,箭頭的位置(pos),軸向(axis),箭頭的大小(shaftwidth)和顏色 
      (color)
    3. box:盒子,盒子的中心點位置(pos),盒子的長寬高(size),盒子的顏色(color),盒 
       子的透明度(opacity)
    4. sphere:球,球的球心位置(pos),半徑(radius),顏色(color)
    5. ring:圓環,圓環的環心位置(pos),圓環的半徑(radius),圓環的寬度(width)
    6. cylinder:圓柱體,圓柱體的底面圓心座標(pos),圓柱體的半徑(radius),圓柱體 
       的軸心向量(axis)
    7. helix:螺旋線,螺旋線的底端座標(pos),螺旋軸向量(axis),螺旋的半徑 
       (radius),螺旋線的寬度(width)
    '''
    GlowScript 2.9 VPython
    scene = display(width=800, height=500,center=vec(1, 1, 0),
                    background=vec(0.2,0.5,0.2), forward=vec(-0.2,-0.2,-1))
    Xaxis=arrow(pos=vec(0,0,0), axis=vec(6,0,0), shaftwidth=0.05, color=vec(1,0,0))
    Yaxis=arrow(pos=vec(0,0,0), axis=vec(0,6,0), shaftwidth=0.03, color=vec(0,1,0))
    Zaxis=arrow(pos=vec(0,0,0), axis=vec(0,0,6), shaftwidth=0.02, color=vec(0,0,1))
    box1=box(pos=vec(0,0,0),size=vec(3,3,3), color=color.cyan, opacity=0.2)
    ball1 = sphere(pos=vec(3,2,1), radius = 0.5, color=color.yellow)
    ball2 = sphere(pos=vec(0,1,0), radius = 0.2, color=color.red)
    ring1=ring(pos=vec(4,0,0), axis=vec(1,0,0),radius=1, thickness=0.1)
    gun=cylinder(radius=0.5,pos=vec(0,0,2),axis=vec(0,0,2), color=color.blue)
    spring1 = helix(pos=vec(0,0,0), axis=vec(0,3,0), color=color.black,
                    radius=0.5, thickness=0.05)
    



  7. 立方體

  8. 在下面的程式中我們將學習如何確定的立方體的頂角座標,並且在那個座標放上一個紅色的球。

    ▶在立方體的頂角上放置一個球影片(Vpython 6版本)

    GlowScript 2.9 VPython
    scene = display(width=800, height=800,center=vec(1, 1, 0),
                    background=vec(0.2,0.5,0.2), forward=vec(-0.2,-0.2,-1))
    L=1;Lx=1.5; Nb=8
    Xaxis=arrow(pos=vec(0,0,0), axis=vec(Lx,0,0), shaftwidth=0.05, color=vec(1,0,0))
    Yaxis=arrow(pos=vec(0,0,0), axis=vec(0,Lx,0), shaftwidth=0.03, color=vec(0,1,0))
    Zaxis=arrow(pos=vec(0,0,0), axis=vec(0,0,Lx), shaftwidth=0.02, color=vec(0,0,1))
    box1=box(pos=vec(L/2.,L/2.,L/2.),size=vec(L,L,L), color=color.cyan, opacity=0.2)
    balls=[sphere(radius=0.1,color=color.red) for i in range(Nb)]
    for i in range(2):
        for j in range(2):
            for k in range(2):
                n=i*4+j*2+k
                balls[n].pos=vec(k,j,i)  
                print (n,balls[n].pos)  
    
    ============輸出:=============
    
    0 < 0, 0, 0 >
    1 < 1, 0, 0 >
    2 < 0, 1, 0 >
    3 < 1, 1, 0 >
    4 < 0, 0, 1 >
    5 < 1, 0, 1 >
    6 < 0, 1, 1 >
    7 < 1, 1, 1 >
    



    3x3x3立方體

    GlowScript 2.9 VPython
    scene = display(width=800, height=800,center=vec(1, 1, 0),
                    background=vec(0.2,0.5,0.2), forward=vec(-0.2,-0.2,-1))
    L=4; Lx=6; m=3; Nb=m**3
    Xaxis=arrow(pos=vec(0,0,0), axis=vec(Lx,0,0), shaftwidth=0.05, color=vec(1,0,0))
    Yaxis=arrow(pos=vec(0,0,0), axis=vec(0,Lx,0), shaftwidth=0.03, color=vec(0,1,0))
    Zaxis=arrow(pos=vec(0,0,0), axis=vec(0,0,Lx), shaftwidth=0.02, color=vec(0,0,1))
    box1=box(pos=vec(L/2.,L/2.,L/2.),size=vec(L,L,L), color=color.cyan, opacity=0.2)
    balls=[sphere(radius=0.2,color=color.red) for i in range(Nb)]
    r=[[k%m,(int(k/m))%m,int(k/m**2)] for k in range(m**3)] 
    for i in range(m**3):
        balls[i].pos=vec(r[i][0]*2,r[i][1]*2,r[i][2]*2)  
        print (i,r[i],balls[i].pos)   
    
    ============輸出:=============
    
    0 [0, 0, 0] < 0, 0, 0 >
    1 [1, 0, 0] < 2, 0, 0 >
    2 [2, 0, 0] < 4, 0, 0 >
    3 [0, 1, 0] < 0, 2, 0 >
    4 [1, 1, 0] < 2, 2, 0 >
    5 [2, 1, 0] < 4, 2, 0 >
    6 [0, 2, 0] < 0, 4, 0 >
    7 [1, 2, 0] < 2, 4, 0 >
    8 [2, 2, 0] < 4, 4, 0 >
    9 [0, 0, 1] < 0, 0, 2 >
    10 [1, 0, 1] < 2, 0, 2 >
    11 [2, 0, 1] < 4, 0, 2 >
    12 [0, 1, 1] < 0, 2, 2 >
    13 [1, 1, 1] < 2, 2, 2 >
    14 [2, 1, 1] < 4, 2, 2 >
    15 [0, 2, 1] < 0, 4, 2 >
    16 [1, 2, 1] < 2, 4, 2 >
    17 [2, 2, 1] < 4, 4, 2 >
    18 [0, 0, 2] < 0, 0, 4 >
    19 [1, 0, 2] < 2, 0, 4 >
    20 [2, 0, 2] < 4, 0, 4 >
    21 [0, 1, 2] < 0, 2, 4 >
    22 [1, 1, 2] < 2, 2, 4 >
    23 [2, 1, 2] < 4, 2, 4 >
    24 [0, 2, 2] < 0, 4, 4 >
    25 [1, 2, 2] < 2, 4, 4 >
    26 [2, 2, 2] < 4, 4, 4 >