回上方

第11堂課

(一)地球的轉動

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf8 -*-
# 匯入視覺化套件
from vpython import *

#  1. 參數設定
#球半徑 0.5 m
size = 0.5
#時間間隔
dt = 0.001

#  2. 畫面設定
scene = canvas(width=800, height=800, background=vector(0.5,0.6,0.5))
# x軸箭頭
x_axis = arrow(axis=vector(1, 0, 0), shaftwidth=0.01)
# y軸箭頭
y_axis = arrow(axis=vector(0, 1, 0), shaftwidth=0.01)
# z軸箭頭
z_axis = arrow(axis=vector(0, 0, 1), shaftwidth=0.01)


#3. 球的設定
earth = sphere(pos=vector(0,0,0), radius=size, texture=textures.earth)

#  4. 運動
while True:
    rate(1000)
    earth.rotate(axis=vector(0,0,1), angle=-2*dt)

(二)五顆球的轉動

使用循序結構

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf8 -*-
# 匯入視覺化套件
from vpython import *

#  1. 參數設定
#球半徑 0.05 m
size = 0.5
#時間間隔
dt = 0.001

#  2. 畫面設定
scene = canvas(width=800, height=800, background=vector(0.5,0.6,0.5))
# x軸箭頭
x_axis = arrow(axis=vector(1, 0, 0), shaftwidth=0.01)
# y軸箭頭
y_axis = arrow(axis=vector(0, 1, 0), shaftwidth=0.01)
# z軸箭頭
z_axis = arrow(axis=vector(0, 0, 1), shaftwidth=0.01)


#3. 球的設定
earth = sphere(pos=vector(0,0,0), radius=size, texture=textures.earth)
stucco = sphere(pos=vector(1,0,0), radius=size, texture=textures.stucco)
metal = sphere(pos=vector(-1,0,0), radius=size, texture=textures.metal)
wood = sphere(pos=vector(-2,0,0), radius=size, texture=textures.wood)
rough = sphere(pos=vector(2,0,0), radius=size, texture=textures.rough)

#  4. 運動
while True:
    rate(1000)
    earth.rotate(axis=vector(0,0,1), angle=-2*dt)
    stucco.rotate(axis=vector(0,0,1), angle=-2*dt)
    metal.rotate(axis=vector(0,0,1), angle=-2*dt)
    wood.rotate(axis=vector(0,0,1), angle=-2*dt)
    rough.rotate(axis=vector(0,0,1), angle=-2*dt)

使用串列(List)

此處balls屬於一種稱為串列(list)的資料型態,可以儲存並處理多項資料。在list中(被中括號[ ]框住) 的資料稱為元素(element)或項目(item),存在 list 中的元素沒有限定種類,可以是任何種類的資 料或物件等,在這裡balls中存了五個ball。創造一個list最簡單的方式就是用中括號[ ], 在其中放入想存的元素,以逗號分隔。你可以想像,list 就是一個清單,裏面有一串序列的物 件。你可以用索引(index)來定位其中的任何元素,每個元素所對應的索引,就是該元素與 list 中第一個元素的索引值的偏移量(offset),如 list 中的第一個元素的索引值為 0,第二個元素的 索引值為 1、第三個元素的索引值為 2…以此類推。例如

A = [1, 2, 3, 4, 5]

代表我們設定一個 list 叫做 A,其中 A[0]的值是 1,A[1]值是 2,以下依此類推。 另外當索引是 -1 時,則指的是最後一個元素,如 A[-1]的值就是 5。

在程式當中,如果要在已存在的 list 增加元素,可利用 append 指令,其用法範例如下: A.append(6)

如此 A 會有六個元素,依序為 1、2、3、4、5、6。而此時,A[-1]的值就是 6。

不含任何元素的 list 也可以使用,如 B=[ ],或主程式中的 arrows 都是空的 list。

使用迴圈

Python的for敘述會將一系列(例如list)裡所有的元素走訪一遍,而執行的順序是依照系列中元素的順序。例如:

1
2
3
A = [1, 2, 3, 4, 5]
for x in A:
    print(x)

以上的式碼會把A中的元素都印出來。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf8 -*-
# 匯入視覺化套件
from vpython import *

#  1. 參數設定
#球半徑 0.05 m
size = 0.5
#時間間隔
dt = 0.001

#  2. 畫面設定
scene = canvas(width=800, height=800, background=vector(0.5,0.6,0.5))
# x軸箭頭
x_axis = arrow(axis=vector(1, 0, 0), shaftwidth=0.01)
# y軸箭頭
y_axis = arrow(axis=vector(0, 1, 0), shaftwidth=0.01)
# z軸箭頭
z_axis = arrow(axis=vector(0, 0, 1), shaftwidth=0.01)


#3. 球的設定
earth = sphere(pos=vector(0,0,0), radius=size, texture=textures.earth)
stucco = sphere(pos=vector(1,0,0), radius=size, texture=textures.stucco)
metal = sphere(pos=vector(-1,0,0), radius=size, texture=textures.metal)
wood = sphere(pos=vector(-2,0,0), radius=size, texture=textures.wood)
rough = sphere(pos=vector(2,0,0), radius=size, texture=textures.rough)

balls = [earth, stucco, metal, wood, rough]

#  4. 運動
while True:
    rate(1000)
    for ball in balls:
        ball.rotate(axis=vector(0,0,1), angle=-2*dt)

(三)滚動的球

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf8 -*-
# 匯入視覺化套件
from vpython import *

#  1. 參數設定
#球半徑 0.05 m
size = 0.05
#地板長
L = 1.0
#時間間隔
dt = 0.001

#  2. 畫面設定
scene = canvas(width=800, height=800, background=vector(0.5,0.6,0.5))
bottom = box(pos=vector(0,-size,0), length=2*L, height=0.001, width=2)
wall = box(pos=vector(L,-size/2,0), length=0.01, height=size, width=2)

#3. 球的設定
earth = sphere(pos=vector(-L,0,0), radius=size, texture=textures.earth)
earth.v = 0.5

#  4. 運動
while True:
    rate(1000)
    earth.pos.x = earth.pos.x + earth.v *dt
    earth.rotate(axis=vector(0,0,1), angle=-earth.v*dt/size)
    if earth.pos.x >= L-size :
        earth.v = 0

作業

請修改上述程式碼,使用串列(list)與迴圈(for loop)畫出三顆滾動的球。


本單元課程自2018.7.5日起已被瀏覽 77