The Artists | Think Like A Coder, Ep 5

438,594 views ・ 2020-01-13

TED-Ed


請雙擊下方英文字幕播放視頻。

譯者: Lilian Chiu 審譯者: Harper Chang
[用程式設計的方式思考]
[地點(上):八達圍] [地點(下):198 森林]
[第五集:藝術家]
00:22
Dawn and the train are both breaking when Ethic and Hedge arrive in the woods.
0
22840
5488
當艾希克和海吉搭乘火車 到達樹林時,天剛破曉。
00:28
The adventurers have recovered the first artifact—
1
28328
2880
這兩位冒險家已經取得了 第一件工藝品——
00:31
the Node of Power—
2
31208
1497
力量球,
00:32
and have come to the 198forest in search of the second.
3
32705
4030
現在來到 198 森林尋找第二個。
00:36
Here they’re welcomed by the director of the colony, Octavia.
4
36735
5111
在這裡迎接他們的是 殖民地領導人奧特薇雅。
00:41
She established this treehouse sanctuary
5
41846
2600
在機器人出現讓大家 可以免於工作之後,
00:44
after the robots freed everyone from having to work.
6
44446
2969
她建立了這個樹屋聖所。
00:47
It was meant to be a haven where people could follow their passions,
7
47415
3290
本意是要成為一個避風港, 讓大家能追求自己的熱情,
00:50
take up crafts, and find fulfillment.
8
50705
2370
學習工藝,找到滿足感。
00:53
Which they did… at first.
9
53075
2263
他們也做到了……一開始時。
00:55
Some years ago everyone forgot the point.
10
55338
2510
幾年前,大家忘了重點。
00:57
They abandoned arts and crafts
11
57848
1630
他們拋棄了藝術和工藝,變成只是
00:59
and instead just painted and exhibited pictures of themselves
12
59478
3200
繪製和展示他們自己的圖像,
01:02
over, and over, and over.
13
62678
2600
一而再,再而三。
01:05
The location of the second artifact is no secret;
14
65278
2610
第二件工藝品的所在並不是秘密;
01:07
it’s in a tower, guarded by a garrison of bots,
15
67888
3460
它在一個由機器人 警衛隊看守的塔中,
01:11
a bottomless ravine, and who knows what other traps.
16
71348
4150
那是個不見底的深谷, 天知道還有什麼其他陷阱。
01:15
As soon as the tower went up with the node inside,
17
75498
3158
隨著塔帶著裡面的 力量球向上升起,
01:18
human communication across the land went dark.
18
78656
3922
整片土地上的人類 交通網都陷入黑暗。
01:22
Octavia’s been after it for years,
19
82578
2050
數年來,奧特薇雅都想取得它,
01:24
but try as she might, the defenses thwart her.
20
84628
4245
但她怎麼努力也無法 通過塔的防禦。
01:28
In order to even get to the tower, the team will need a distraction.
21
88873
4258
要到達塔的所在,這個團隊 需要分散對方的注意力。
01:33
Octavia has an idea:
22
93131
1849
奧特薇雅有個點子:
01:34
stir up the people through some well-intentioned vandalism.
23
94980
4131
用善意的破壞行為 來引起大家的騷動。
01:39
The residents’ paintings are all squares that come in different sizes,
24
99111
3832
居民的畫作都是方形的,尺寸不一。
01:42
all an odd number of pixels across.
25
102943
3608
邊長的像素數目都是奇數。
01:46
Helper-bots pick up the finished portraits
26
106551
2412
助手機器人會去取得完成的畫像,
01:48
and hang them in public places for everyone to admire.
27
108963
3848
將它們公開掛出來給大家欣賞。
01:52
There’s a slim margin of time when Hedge can access the paintings.
28
112811
4190
有一個小小的空檔, 海吉可以接觸到那些畫作。
01:57
If he were to deface each one with an X,
29
117001
3361
如果他能把每張畫 都畫上一個大叉叉,
02:00
the people would blame the helper-bots,
30
120362
2439
大家就會怪罪助手機器人,
02:02
creating just the distraction the team needs.
31
122801
3410
就能幫團隊達到 分散注意力的效果。
02:06
If only it were so easy.
32
126211
2300
事實上沒那麼簡單。
02:08
Hedge can’t just paint an X—
33
128511
2230
不能只叫海吉去畫個大叉叉,
02:10
his painting processor requires very specific instructions.
34
130741
4121
他的繪圖處理器需要 非常明確的指令。
02:14
Treating the paintings as square grids,
35
134862
2630
把畫作視為是方形的格子,
02:17
he can fill in one pixel, or little square, at a time.
36
137492
4000
他一次能填滿一個像素 或一個小方格。
02:21
He can move forwards and make 90 degree turns over the canvas,
37
141492
3720
在畫布上,他能向前移動, 也可以做九十度的轉彎,
02:25
but can’t move diagonally.
38
145212
2728
但不能走斜對角。
02:27
How does Ethic program Hedge to paint an X over each portrait?
39
147940
4802
艾希克要如何設定海吉的程式,
才能在每張畫像上 都畫一個大叉叉?
02:32
Pause now to figure it out for yourself.
40
152742
8528
[若你想要嘗試解題,請在此暫停]
[題目規則一、二、三]
02:41
Here’s a hint.
41
161270
1530
提示如下。
02:42
Try drawing a square grid like this,
42
162800
2702
試試看畫一個像這樣的方格,
02:45
and simulating Hedge’s path over it.
43
165502
2950
並模擬海吉在上面的路徑。
02:48
What patterns can you find to guide him?
44
168452
2556
你能找出什麼模式來引導他?
02:51
Pause now to figure it out for yourself.
45
171008
4790
[若你想要嘗試解題,請在此暫停]
02:55
The challenge here is to craft a set of instructions
46
175798
2599
這裡的挑戰,是要 設計出一組指令,
02:58
that will work for any square grid.
47
178397
2791
能夠用在任何方格上。
03:01
Fortunately, one of the strengths of programming
48
181188
2570
幸運的是,寫程式的優點之一
03:03
is the flexibility to solve not just one problem,
49
183758
2980
就是有彈性,可以 不只解一個問題,
03:06
but a whole class of them all at once.
50
186738
3110
同類的問題都能解決。
03:09
It often helps to start with one case, and work towards the general.
51
189848
4801
有個不錯的方式是 先處理一個,再普及到整體。
03:14
Let’s say we had this square.
52
194649
1970
比如,我們有這個方形。
03:16
Hedge can measure the length of its sides and store that number as a variable.
53
196619
4479
海吉可以測量它的邊長, 把這個數字當作變數儲存下來。
03:21
Now, what we need is a plan for how Hedge will paint an X,
54
201098
4585
我們現在需要一個計畫, 規劃海吉要如何畫出大叉叉。
03:25
pixel by pixel.
55
205683
1540
一次畫一個像素。
03:27
There’s more than one right answer for how to do this;
56
207223
2952
要做到這件事, 正確答案不只一個;
03:30
let’s look at two.
57
210175
1483
咱們來看兩種做法。
03:31
First, what if Hedge went row by row, like a typewriter?
58
211658
3638
首先,如果海吉像打字機 一樣逐行進行呢?
03:35
If it’s a 9 pixel by 9 pixel painting,
59
215296
3023
如果一幅畫的長寬都是九像素,
03:38
in the first row he’d paint, skip 7, and then paint again.
60
218319
4309
在第一行,他會先上色, 跳過七格,接著再上色。
03:42
In the second row he’d skip the first, paint, skip 5, and paint.
61
222628
4818
在第二行,他會跳過第一格, 上色,跳過五格,再上色。
03:47
And so on.
62
227446
1330
以此類推。
03:48
The pattern here is that for each row the pixels skipped at the beginning
63
228776
4242
這裡的模式是,每跳到下一行
一開始要跳過的像素 數目就要多一個,
03:53
go up by one,
64
233018
1410
03:54
and the pixels skipped in the middle go down by 2.
65
234428
3650
每行中間要跳過的像素 數目則會少兩個。
03:58
Things get more complicated when Hedge reaches the center.
66
238078
3080
海吉越接近中心時, 情況就會越複雜。
04:01
Here there’s a row with just one pixel painted.
67
241158
3041
在這裡,這一行只要 將一個像素上色。
04:04
Then the whole thing reverses—
68
244199
1969
接著整個模式就會反轉——
04:06
the number of pixels skipped goes down by one each time on the left,
69
246168
4123
每換一行,左側要跳過的 像素數目會少一個,
04:10
and up by two each time in the middle.
70
250291
3366
中間要跳過的像素 數目則會多兩個。
04:13
Instructing Hedge to do this with a series of loops will work
71
253657
3454
用一系列的迴圈可以 指示海吉做到這件事,
04:17
and is a perfectly fine solution.
72
257111
2439
且這個解決方案完全沒問題。
04:19
The main drawback is that this requires quite a bit of logic—
73
259550
3601
主要的缺點是,這個做法 需要相當的邏輯——
04:23
knowing what to do in the middle, when to reverse the process,
74
263151
3020
要知道在中間時該怎麼做、 何時要反轉過程,
04:26
and exactly how to reverse it.
75
266171
2690
以及要如何反轉過程。
04:28
So how might we approach this
76
268861
1784
我們要怎麼在邏輯 不變的情況下做到這件事?
04:30
so that the logic remains consistent from start to finish?
77
270645
3916
04:34
The key insight is to look at a grid as a series of concentric squares.
78
274561
5488
關鍵點在於要把一個方格 視為是一系列的同心方形。
04:40
Each square follows the same pattern—
79
280049
2640
每個方形都遵循同樣的模式——
04:42
painted pixels in the corners, and unaltered pixels in between.
80
282689
4222
將角落的像素上色, 中間的像素則不做改變。
04:46
So if we can figure out a way to paint one nested square,
81
286911
3418
如果我們能想辦法 將角落的方形上色,
04:50
transition to the next, and repeat, we can paint them all.
82
290329
4435
轉換到下一個,再重覆, 就可以全部上色完成。
04:54
Painting the outermost one is easy.
83
294764
2361
將最外面的方形上色很容易。
04:57
Start in a corner and paint that pixel.
84
297125
2489
從一個角落開始, 將那個像素上色。
04:59
If we call the length of the painting n,
85
299614
2440
如果我們把畫作的長度叫做 n,
05:02
fly forward n minus 1 spaces.
86
302054
2750
就是向前飛躍 n 減一格。
05:04
Paint another pixel, and turn right.
87
304804
2500
將那個像素上色,接著向右轉。
05:07
Now do the whole thing again… and again.
88
307304
3530
再把整個過程做一次, 然後再做一次。
05:10
Now move forward one less space, turn right, fly forward once,
89
310834
5195
現在向前進的距離減少一格, 向右轉,向前飛躍一次,
05:16
and Hedge will be in the next concentric square
90
316029
3170
接著海吉就會進入 下一個同心方形,
05:19
and ready to repeat the whole process.
91
319199
2785
準備繼續重覆同樣的流程。
05:21
Each square is n minus 2 pixels smaller than the last in length and width,
92
321984
5742
每一個方形的長寬都會比 上一個方形小 n 減二個像素,
05:27
and we can follow this spiral pattern all the way to the center
93
327726
3920
我們可以循著這個 螺旋模式一路到中心點,
05:31
with a loop and a variable that tracks how far Hedge should fly.
94
331646
5147
只要用一個迴圈和一個變數 來追蹤海吉應該飛躍的距離即可。
05:36
Is one of these methods better than the other?
95
336793
2240
這些方法有高下嗎? 這就要看你重視什麼了。
05:39
It really depends on what you value.
96
339033
2064
05:41
The strength of the spiral is the simplicity of finding a pattern
97
341097
3430
螺旋法的優點是很容易找到模式,
05:44
and reusing the same logic from start to finish.
98
344527
3040
同樣的邏輯可以 從頭到尾一直重覆使用。
05:47
The advantage of the typewriter approach
99
347567
2100
打字機法的優點在於 它是較一般化的解決方案,
05:49
is that it’s a more generalized solution,
100
349667
2290
05:51
meaning it can be adapted much more simply to fill in any pattern.
101
351957
4070
意思就是,很容易就可以 把它改寫套用在任何模式上。
05:56
For Ethic’s sake, either will do just fine.
102
356027
3271
就艾希克的情況來說, 兩種方法皆可行。
05:59
So here’s what happens.
103
359298
1830
所以,發生的狀況是這樣:
06:01
Hedge rapidly defaces all of the portraits.
104
361128
3109
海吉很快地破壞了所有的畫像。
06:04
And within moments
105
364237
1110
沒多久,整個森林各處 都可以聽見憤怒的吼叫。
06:05
cries of anguish break out all over the forest.
106
365347
3671
06:09
The garrison guarding the tower abandon their posts
107
369018
2980
看守著塔的警衛隊 離開了它們的崗位,
06:11
to calm the agitated people,
108
371998
1970
去安撫激動的民眾,
06:13
and Ethic, Hedge, and Octavia slip through—
109
373968
2990
而艾西克、海吉, 及奧特薇雅便溜過去——
06:16
and nearly slip into the depths of the gorge
110
376958
3040
差一點點滑落 他們和塔之間的深谷。
06:19
standing between them and the tower.
111
379998
2889
關於本網站

本網站將向您介紹對學習英語有用的 YouTube 視頻。 您將看到來自世界各地的一流教師教授的英語課程。 雙擊每個視頻頁面上顯示的英文字幕,從那裡播放視頻。 字幕與視頻播放同步滾動。 如果您有任何意見或要求,請使用此聯繫表與我們聯繫。

https://forms.gle/WvT1wiN1qDtmnspy7