Example 13 — Rotate a Text Picture with a Rotation Matrix ตัวอย่างที่ 13 — หมุนภาพตัวอักษรด้วย rotation matrix

Last updated แก้ไขล่าสุด

Turn a picture by an angle you enter, with a rotation matrix made of cos and sin. หมุนภาพด้วยมุมที่ป้อนเอง โดยใช้ rotation matrix ที่มี cos และ sin

This page solves the problem of Example 12 again, and this time the picture can turn by other angles. Read card 2 for the basics of turning with a matrix and card 3 for a worked example, then follow the flowchart and press Run.

หน้านี้แก้โจทย์เดียวกับตัวอย่างที่ 12 แต่หมุนได้หลายมุม อ่านการ์ดที่ 2 เพื่อปูพื้นฐานการหมุนด้วย matrix และการ์ดที่ 3 เพื่อดูตัวอย่างการคำนวณ แล้วค่อยไล่ตาม flowchart และกด Run

1 The problem โจทย์

Use a picture like the one in Example 12. Read an angle in degrees with input(), then show the picture turned around its center by that angle. A positive angle turns clockwise. The result has the same size as the picture, and any cell with no data shows -.

ใช้ picture แบบตัวอย่างที่ 12 รับมุมเป็นองศาจาก input() แล้วแสดงภาพที่หมุนรอบจุดกึ่งกลางด้วยมุมนั้น มุมบวกหมุนตามเข็มนาฬิกา ภาพผลมีขนาดเท่าเดิม ช่องที่ไม่มีข้อมูลให้แสดง -

  • Example: the angle 90 gives the same result as Example 12
  • ตัวอย่าง: มุม 90 ได้ผลเหมือนตัวอย่างที่ 12
Commands used in this program:
  • math.radians(angle) changes degrees to radians, because math.cos and math.sin take radians.
  • round(v) rounds v to the nearest whole number. A value exactly halfway goes to the even neighbour, so round(2.5) is 2 and round(3.5) is 4.
  • and is True when every condition is true. It checks that a position is inside the picture.
คำสั่งที่ใช้ในโปรแกรมนี้:
  • math.radians(angle) แปลงองศาเป็นเรเดียน เพราะ math.cos และ math.sin รับค่าเป็นเรเดียน
  • round(v) ปัด v เป็นจำนวนเต็มที่ใกล้ที่สุด ค่าที่อยู่กึ่งกลางพอดีจะปัดไปหาเลขคู่ round(2.5) จึงได้ 2 และ round(3.5) ได้ 4
  • and เป็น True เมื่อทุกเงื่อนไขเป็นจริง ใช้ตรวจว่าตำแหน่งอยู่ในภาพ
2 The basics of turning with a matrix พื้นฐานการหมุนด้วย matrix

Step 1: positions become coordinates. The character at row r and column c is one point. The picture turns around its center, so the coordinates are measured from the center.

ขั้นที่ 1 เปลี่ยนตำแหน่งเป็นพิกัด ตัวอักษรแถว r คอลัมน์ c เป็นจุด 1 จุด เนื่องจากต้องการหมุนรอบกึ่งกลางภาพ จึงวัดพิกัดจากจุดกึ่งกลาง

\[ center = \frac{N - 1}{2}, \qquad x = c - center, \qquad y = r - center \]

A 9×9 picture has center = 4, so the @ at r = 2, c = 3 has the coordinates \(x = -1,\ y = -2\).

ภาพ 9×9 มี center = 4 ตัว @ ที่ r = 2, c = 3 จึงมีพิกัด \(x = -1,\ y = -2\)

Step 2: turn the point with a rotation matrix.

ขั้นที่ 2 หมุนจุดด้วย rotation matrix

\[ \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} \]

The y axis of a text picture points down, because the row number grows from top to bottom. So this formula turns the point clockwise when \(\theta\) is positive. For \(\theta = 90^\circ\), \(\cos\theta = 0\) and \(\sin\theta = 1\):

แกน y ของภาพตัวอักษรชี้ลง เพราะเลขแถวเพิ่มจากบนลงล่าง สูตรนี้จึงหมุนตามเข็มนาฬิกาเมื่อ \(\theta\) เป็นบวก ตัวอย่างที่ \(\theta = 90^\circ\) ซึ่ง \(\cos\theta = 0\) และ \(\sin\theta = 1\)

\[ \begin{aligned} x' &= (0)(-1) - (1)(-2) = 2 \\ y' &= (1)(-1) + (0)(-2) = -1 \end{aligned} \]

Adding center back gives c = 6, r = 3, which is where @ is in the result of Example 12. The code below does the same calculation. In a computer math.cos(theta) is not exactly 0 but about \(6 \times 10^{-17}\), so the code rounds the result to 6 decimal places.

บวก center กลับ ได้ c = 6, r = 3 ตรงกับตำแหน่งของ @ ในผลของตัวอย่างที่ 12 โค้ดด้านล่างคำนวณแบบเดียวกัน ในคอมพิวเตอร์ math.cos(theta) ไม่ได้เป็น 0 พอดี แต่ประมาณ \(6 \times 10^{-17}\) โค้ดจึงปัดผลเหลือทศนิยม 6 ตำแหน่ง

[point]
[output]
2.0 -1.0

Step 3: the trouble with other angles. Other angles give coordinates with decimals. If each character turns from the picture to the result and is then rounded, several characters land on the same cell and some cells get nothing. In this 9×9 picture turned by 45°, a . marks a cell where no character landed. These cells are at the corners and also inside the picture, such as r = 2, c = 4.

ขั้นที่ 3 ปัญหาของมุมที่ไม่ใช่มุมฉาก มุมอื่นให้พิกัดเป็นทศนิยม ถ้าหมุนจากภาพเดิมไปภาพผลแล้วปัดเศษ หลายตัวจะตกช่องเดียวกัน และบางช่องไม่มีตัวอักษรเลย ในภาพ 9×9 ที่หมุน 45° นี้ ช่อง . คือช่องที่ไม่มีตัวอักษรตกลงมา มีทั้งมุมภาพและรูโหว่กลางภาพ เช่น แถว r = 2 คอลัมน์ c = 4

..*-*-*..
.*.---.*.
*.--.@-.*
------@--
*-.+--.-*
*---++--*
*.--.--.*
.*.---.*.
..*****..

Step 4: reverse mapping. Turn the thinking around. Go through the cells of the result one at a time, and turn each cell back by \(-\theta\) to find where it came from in the picture. Putting \(-\theta\) into the matrix gives:

ขั้นที่ 4 แก้ด้วย reverse mapping กลับทิศการคิด ไล่ทีละช่องของภาพผล แล้วหมุนกลับด้วยมุม \(-\theta\) เพื่อหาว่าช่องนี้มาจากตำแหน่งใดของภาพเดิม แทน \(-\theta\) ลงใน matrix ได้

\[ \begin{bmatrix} x_{src} \\ y_{src} \end{bmatrix} = \begin{bmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} \]

Rounding the result picks the nearest cell. A character can be off by up to 1 cell, but every cell always gets a value. If the position falls outside the picture, the cell gets -.

ปัดผลเป็นจำนวนเต็มเพื่อเลือกช่องที่ใกล้ที่สุด ตัวอักษรอาจคลาดไม่เกิน 1 ช่อง แต่ทุกช่องจะมีค่าเสมอ ถ้าตำแหน่งที่ได้อยู่นอกภาพ ให้เติม -

3 A worked example ตัวอย่างการคำนวณ

Take the 3×3 picture of numbers from Example 12 and the angle 90, and work out every cell of the result by hand. The picture and the result are:

ลองใช้ภาพตัวเลขขนาด 3×3 จากตัวอย่างที่ 12 กับมุม 90 แล้วคำนวณทุกช่องของภาพผลด้วยมือ ภาพเดิมและภาพผลเป็นดังนี้

The picture — picture ภาพเดิม — picture
c = 0 c = 1 c = 2
r = 0 1 2 3
r = 1 4 5 6
r = 2 7 8 9
The result — angle = 90 ภาพผล — angle = 90
c′ = 0 c′ = 1 c′ = 2
r′ = 0 7 4 1
r′ = 1 8 5 2
r′ = 2 9 6 3

r′ and c′ are the row and the column of the result, and r and c are the row and the column of the picture the character comes from. The picture has size = 3, so center = (3 - 1) / 2 = 1, and the angle 90° gives \(\cos\theta = 0\) and \(\sin\theta = 1\). Each cell of the result is worked out in three steps:

ให้ r′ และ c′ เป็นแถวและคอลัมน์ของภาพผล ส่วน r และ c เป็นแถวและคอลัมน์ของภาพเดิมที่ตัวอักษรนั้นมาจาก ภาพนี้มี size = 3 จึงได้ center = (3 - 1) / 2 = 1 และมุม 90° ให้ \(\cos\theta = 0\) กับ \(\sin\theta = 1\) การคิดแต่ละช่องของภาพผลมี 3 ขั้น

\[ x = c' - center, \qquad y = r' - center \]
\[ x_{src} = \cos\theta \cdot x + \sin\theta \cdot y = y, \qquad y_{src} = -\sin\theta \cdot x + \cos\theta \cdot y = -x \]
\[ c = x_{src} + center, \qquad r = y_{src} + center \]
Every cell of the result (size = 3, center = 1, angle = 90) ทุกช่องของภาพผล (size = 3, center = 1, angle = 90)
New data — the resultข้อมูลใหม่ — ภาพผล Coordinatesพิกัดจากกึ่งกลาง Turned backหมุนกลับ Old dataข้อมูลเดิม
Valueค่า r′ c′ x y x_src y_src r c
7 00 −1−1 −11 20
4 01 0−1 −10 10
1 02 1−1 −1−1 00
8 10 −10 01 21
5 11 00 00 11
2 12 10 0−1 01
9 20 −11 11 22
6 21 01 10 12
3 22 11 1−1 02

Every r and c in the table is a whole number between 0 and 2, so no cell falls outside the picture and no cell gets -. Reading the values down the table gives 741, 852, 963, the same result as Example 12. In the code the loop counters r and c run over the result, so they are r′ and c′ of this table, and the two columns on the right are src_r and src_c.

ค่า r และ c ในตารางเป็นจำนวนเต็มตั้งแต่ 0 ถึง 2 ทุกช่อง จึงไม่มีช่องใดตกนอกภาพและไม่มีช่องใดได้ - อ่านค่าไล่ลงมาตามตารางได้ 741, 852, 963 ซึ่งเป็นผลเดียวกับตัวอย่างที่ 12 ส่วนในโค้ด ตัวนับของ loop คือ r และ c ซึ่งไล่ไปตามภาพผล จึงเป็น r′ และ c′ ของตารางนี้ และสองคอลัมน์ขวาสุดคือ src_r กับ src_c

4 Main program — flowchart and full code โปรแกรมหลัก — flowchart และโค้ดทั้งหมด

The program starts by setting the picture in picture, keeps its size in size, and reads the angle with input into angle. Next it changes the angle to radians in theta and works out center. Then it sets r = 0 and c = 0 as the row and column counters of the result, and changes the position to the coordinates x and y. It turns them back with the reverse mapping formula, adds center and rounds with round to get src_c and src_r. It checks whether this position is inside the picture: if so, it adds the character from picture to the end of text; if not, it adds -. It repeats this until every column is done, shows the row with print and moves on to the next row. When every row is done, the program ends.

เริ่มต้นด้วยกำหนดภาพไว้ใน picture หาขนาดภาพเก็บใน size แล้วรับมุมจาก input เก็บใน angle ถัดไปแปลงมุมเป็นเรเดียนเก็บใน theta และคำนวณ center จากนั้นกำหนด r = 0 และ c = 0 เป็นตัวนับแถวและคอลัมน์ของภาพผล แล้วเปลี่ยนตำแหน่งเป็นพิกัด x และ y หมุนกลับด้วยสูตร reverse mapping บวก center และปัดเศษด้วย round ได้ src_c และ src_r ตรวจสอบเงื่อนไขว่าตำแหน่งนี้อยู่ในภาพหรือไม่ ถ้าใช่ให้หยิบตัวอักษรจาก picture ต่อท้าย text ถ้าไม่ใช่ให้ต่อด้วย - ทำแบบนี้จนครบทุกคอลัมน์จึงแสดงแถวด้วย print แล้วไปแถวถัดไป เมื่อครบทุกแถวแล้วจบการทำงานของโปรแกรม

one round = one row Start Set the picture picture = [...] Find the size size = len(picture) Ask for the angle angle = int(input(...)) Change degrees to radians theta = math.radians(angle) Find the center center = (size - 1) / 2 Start at the first row r = 0 Row left? r < size? Yes Start an empty row text = "" Start at the first column c = 0 Column left? c < size? Yes Coordinates from the center x = c - center y = r - center Turn back by the angle and round src_c = round(cos * x + sin * y + center) src_r = round(-sin * x + cos * y + center) Inside the picture? 0 ≤ src_c, src_r < size? Yes Add the character to the row text += picture[src_r][src_c] No Add a dash text += "-" Go to the next column c += 1 No Show this row print(text) Go to the next row r += 1 No End
In the box of src_c and src_r, cos stands for math.cos(theta) and sin stands for math.sin(theta). ในกล่อง src_c และ src_r คำว่า cos แทน math.cos(theta) และ sin แทน math.sin(theta)
[main]
[input]
[output]
Angle (degrees): 45
--*****--
-**---**-
**---@-**
*-----@-*
*-++----*
*--++---*
**--+--**
-**---**-
--*****--

Try it yourself — enter an angle in degrees and press Submit. Each value starts a new run.

ลองป้อนค่าเอง — ป้อนมุมเป็นองศาแล้วกดส่ง ทุกค่าที่ส่งจะเริ่มรันใหม่

5 Results of the program ผลของโปรแกรม

Enter each angle in the form of card 4 to check this table. Characters on a screen are taller than they are wide, so a picture at a slanted angle looks a little squeezed.

ป้อนแต่ละมุมในฟอร์มของการ์ดที่ 4 เพื่อตรวจตารางนี้ ตัวอักษรบนจอสูงกว่ากว้าง ภาพที่มุมเอียงจึงดูบีบเล็กน้อย

Angleมุม Resultผล
0, 360 The same as the pictureเหมือนภาพเดิม
90 The same as Example 12เหมือนตัวอย่างที่ 12
180 The picture upside downภาพกลับหัว
270, -90 The same result: a 90° counterclockwise turnผลเท่ากัน หมุนทวนเข็มนาฬิกา 90°
45 A slanted picture, shown in card 4ภาพเอียง ดูผลได้ในการ์ดที่ 4
30 A slanted picture, and some cells of the outline are lost to roundingภาพเอียง ขอบบางช่องหายเพราะการปัดเศษ
6 Compared with Example 12 เทียบกับตัวอย่างที่ 12
  Example 12ตัวอย่างที่ 12 Example 13ตัวอย่างที่ 13
Finding the positionการหาตำแหน่ง Whole-number indexindex จำนวนเต็ม cos and sin, then roundingคำนวณด้วย cos และ sin แล้วปัดเศษ
Anglesมุมที่หมุนได้ 90° only90° เท่านั้น Any angleมุมใดก็ได้
Result at 90°ผลที่ 90° Exactตรงทุกตัว Exactตรงทุกตัว
Result at other anglesผลที่มุมอื่น Not possibleทำไม่ได้ A character can be off by up to 1 cell, and some outline cells can be lostตัวอักษรคลาดได้ไม่เกิน 1 ช่อง ขอบบางช่องอาจหาย
Lines that work out one cellจำนวนบรรทัดที่คำนวณ 1 ช่อง 1 8

If you need only 90°, Example 12 is simpler and exact. If you need many angles, use the matrix.

ถ้าต้องการแค่ 90° ตัวอย่างที่ 12 ง่ายและแม่นกว่า ถ้าต้องการหลายมุมให้ใช้ matrix

Think further: the smiley face is round, so all its characters stay in the picture at 45°. But a picture with characters in its corners, such as a * frame around it, loses its corners in the result. What would you do to see the whole picture?

ชวนคิดต่อ: หน้ายิ้มเป็นรูปกลม หมุน 45° แล้วตัวอักษรจึงอยู่ในภาพครบ แต่ถ้าภาพมีตัวอักษรที่มุม เช่น กรอบ * รอบภาพ มุมจะหลุดออกนอกภาพผล จะทำอย่างไรให้เห็นครบ

Show the answer ดูคำตอบ

Make the result bigger. Its side must be at least \(\sqrt{2}\) times the side of the picture, so a 9×9 picture needs 13×13, because \(9\sqrt{2} \approx 12.7\).

ขยายภาพผลให้ด้านยาวอย่างน้อย \(\sqrt{2}\) เท่าของเดิม ภาพ 9×9 จึงต้องใช้ 13×13 เพราะ \(9\sqrt{2} \approx 12.7\)

The Run buttons on this page simulate Python with JavaScript, the same as the other lessons. To run the real Python, paste the code into Google Colab.

ปุ่ม Run ในหน้านี้จำลองโค้ด Python ด้วย JavaScript เหมือนบทเรียนอื่นๆ หากต้องการรัน Python จริง ให้นำโค้ดไปวางใน Google Colab

References References / แหล่งอ้างอิง

  1. Python docs — math: Trigonometric functions (math.cos, math.sin) (math.cos, math.sin)
  2. Python docs — math: Angular conversion (math.radians) (math.radians)
  3. Python docs — round (rounding to a whole number) (การปัดเป็นจำนวนเต็ม)
  4. The 90° version of this problem: Example 12 — Rotate a Text Picture 90° โจทย์นี้แบบ 90°: ตัวอย่างที่ 12 — หมุนภาพตัวอักษร 90°

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

A 9×9 picture has size = 9. From this line, what is center?
center = (size - 1) / 2
ภาพ 9×9 มี size = 9 จากบรรทัดนี้ center เท่ากับเท่าไร?
center = (size - 1) / 2

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 44

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 4.54.5
    Wrong: 4.5 is 9 divided by 2, but the last index is 8.ผิด: 4.5 เป็นการหาร 9 ด้วย 2 แต่ index สุดท้ายคือ 8
  • (B) 99
    Wrong: 9 is the size of the picture.ผิด: 9 คือขนาดภาพ
  • (C) 33
    Wrong: (9 - 1) / 2 is not 3.ผิด: (9 - 1) / 2 ไม่ได้เท่ากับ 3
  • (D) 44
    Correct! (9 - 1) / 2 = 4.ถูกต้อง! (9 - 1) / 2 = 4

ข้อ 2

2 / 10

Why is math.radians(angle) needed?
angle = int(input("Angle (degrees): "))
theta = math.radians(angle)
src_c = round(math.cos(theta) * x + math.sin(theta) * y + center)
ทำไมต้องใช้ math.radians(angle)?
angle = int(input("Angle (degrees): "))
theta = math.radians(angle)
src_c = round(math.cos(theta) * x + math.sin(theta) * y + center)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Because math.cos and math.sin take radiansเพราะ math.cos และ math.sin รับค่าเป็นเรเดียน

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) To round the numberเพื่อปัดเศษ
    Wrong: rounding is done with round.ผิด: การปัดเศษใช้ round
  • (B) Because math.cos and math.sin take radiansเพราะ math.cos และ math.sin รับค่าเป็นเรเดียน
    Correct! The angle is entered in degrees, so it must be changed first.ถูกต้อง! มุมที่ป้อนเป็นองศา จึงต้องแปลงก่อน
  • (C) To change it to a whole numberเพื่อแปลงเป็นจำนวนเต็ม
    Wrong: the result usually has decimals.ผิด: ผลของการแปลงมักเป็นทศนิยม
  • (D) To check that the angle is validเพื่อตรวจว่ามุมถูกต้อง
    Wrong: this function does not check the value.ผิด: ฟังก์ชันนี้ไม่ได้ตรวจค่า

ข้อ 3

3 / 10

Why does a positive angle turn clockwise?
angle = int(input("Angle (degrees): "))
theta = math.radians(angle)
center = (size - 1) / 2

for r in range(size):
    for c in range(size):
        x = c - center
        y = r - center
ทำไมมุมบวกจึงหมุนตามเข็มนาฬิกา?
angle = int(input("Angle (degrees): "))
theta = math.radians(angle)
center = (size - 1) / 2

for r in range(size):
    for c in range(size):
        x = c - center
        y = r - center

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Because the y axis of the picture points downเพราะแกน y ของภาพชี้ลง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Because the y axis of the picture points downเพราะแกน y ของภาพชี้ลง
    Correct! The row number grows from top to bottom, so the turn on screen is clockwise.ถูกต้อง! เลขแถวเพิ่มจากบนลงล่าง ทิศการหมุนบนจอจึงเป็นตามเข็มนาฬิกา
  • (B) Because cos is positiveเพราะ cos เป็นบวก
    Wrong: cos can be negative, and the direction stays the same.ผิด: cos เป็นลบได้ และทิศการหมุนยังเหมือนเดิม
  • (C) Because center is positiveเพราะ center เป็นบวก
    Wrong: center is only the reference point.ผิด: center เป็นแค่จุดอ้างอิง
  • (D) Because input() takes positive valuesเพราะ input() รับค่าบวก
    Wrong: input() can take a negative angle too.ผิด: input() รับมุมติดลบได้

ข้อ 4

4 / 10

Using \(x' = x\cos\theta - y\sin\theta\) and \(y' = x\sin\theta + y\cos\theta\), what is \((x', y')\) when the point \(x = -1,\ y = -2\) is turned by 90°?ใช้สมการ \(x' = x\cos\theta - y\sin\theta\) และ \(y' = x\sin\theta + y\cos\theta\) จุด \(x = -1,\ y = -2\) หมุน 90° ได้ \((x', y')\) เท่าไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) \((2, -1)\)\((2, -1)\)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) \((-1, 2)\)\((-1, 2)\)
    Wrong: with \(\cos\theta = 0\) and \(\sin\theta = 1\), \(x' = 2\).ผิด: แทน \(\cos\theta = 0\), \(\sin\theta = 1\) แล้ว \(x' = 2\)
  • (B) \((1, 2)\)\((1, 2)\)
    Wrong: this is the point turned by 180°.ผิด: นี่คือจุดที่หมุน 180° จากจุดเดิม
  • (C) \((2, -1)\)\((2, -1)\)
    Correct! \(x' = -(1)(-2) = 2\) and \(y' = (1)(-1) = -1\).ถูกต้อง! \(x' = -(1)(-2) = 2\) และ \(y' = (1)(-1) = -1\)
  • (D) \((-2, 1)\)\((-2, 1)\)
    Wrong: this is the point turned by −90°.ผิด: นี่คือจุดที่หมุน −90°

ข้อ 5

5 / 10

What goes wrong if each character turns from the picture to the result and is then rounded?ถ้าหมุนจากภาพเดิมไปภาพผลแล้วปัดเศษ เกิดปัญหาอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Some cells get nothing and some characters land on the same cellบางช่องไม่มีตัวอักษรและบางตัวตกช่องเดียวกัน

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) The picture ends up upside downภาพกลับหัว
    Wrong: the direction of the turn is still right.ผิด: ทิศการหมุนยังถูกต้อง
  • (B) The program stopsโปรแกรมหยุดทำงาน
    Wrong: the program finishes, but the picture has holes.ผิด: โปรแกรมทำงานจบ แต่ภาพมีรู
  • (C) New characters appearได้ตัวอักษรใหม่
    Wrong: every character comes from the picture.ผิด: ตัวอักษรมาจากภาพเดิมทั้งหมด
  • (D) Some cells get nothing and some characters land on the same cellบางช่องไม่มีตัวอักษรและบางตัวตกช่องเดียวกัน
    Correct! Rounding means the cells no longer match one to one.ถูกต้อง! การปัดเศษทำให้การจับคู่ช่องไม่ครบ

ข้อ 6

6 / 10

How does reverse mapping work?reverse mapping ทำอย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) It goes through the cells of the result and turns each one back to find its position in the pictureไล่ทีละช่องของภาพผล แล้วหมุนกลับหาตำแหน่งในภาพเดิม

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It turns the picture twiceหมุนภาพสองครั้ง
    Wrong: the position of each cell is turned back once.ผิด: หมุนเพียงตำแหน่งของแต่ละช่องกลับ 1 ครั้ง
  • (B) It goes through the cells of the result and turns each one back to find its position in the pictureไล่ทีละช่องของภาพผล แล้วหมุนกลับหาตำแหน่งในภาพเดิม
    Correct! So every cell of the result always gets a value.ถูกต้อง! ทุกช่องของภาพผลจึงได้ค่าเสมอ
  • (C) It reverses the order of the rowsกลับลำดับแถว
    Wrong: that only flips the picture upside down.ผิด: แบบนั้นคือการกลับภาพบนลงล่าง
  • (D) It removes the empty cellsลบช่องว่างออก
    Wrong: the result keeps the same size.ผิด: ภาพผลยังมีขนาดเท่าเดิม

ข้อ 7

7 / 10

Which values are right?
print(round(2.6), round(2.5), round(3.5))
ค่าใดถูกต้อง?
print(round(2.6), round(2.5), round(3.5))

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 3 2 43 2 4

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 3 2 43 2 4
    Correct! 2.6 is nearest to 3, while 2.5 and 3.5 are exactly halfway, so Python rounds to the even neighbour: 2 and 4.ถูกต้อง! 2.6 ใกล้ 3 ส่วน 2.5 และ 3.5 อยู่กึ่งกลางพอดี Python จึงปัดไปหาเลขคู่ ได้ 2 และ 4
  • (B) 3 3 43 3 4
    Wrong: round(2.5) does not go up to 3, because 2 is the even neighbour.ผิด: round(2.5) ไม่ได้ปัดขึ้นเป็น 3 เพราะ 2 เป็นเลขคู่
  • (C) 2 2 32 2 3
    Wrong: that is cutting the decimals off, not rounding.ผิด: แบบนี้คือการตัดทศนิยมทิ้ง ไม่ใช่การปัด
  • (D) 2.6 2.5 3.52.6 2.5 3.5
    Wrong: round with one argument gives a whole number.ผิด: round ที่ใส่ค่าเดียวให้จำนวนเต็ม

ข้อ 8

8 / 10

What does the program do if the position it turns back to is outside the picture?
if src_c >= 0 and src_c < size and src_r >= 0 and src_r < size:
    text += picture[src_r][src_c]
else:
    text += "-"
ถ้าตำแหน่งที่หมุนกลับแล้วอยู่นอกภาพ โปรแกรมทำอย่างไร?
if src_c >= 0 and src_c < size and src_r >= 0 and src_r < size:
    text += picture[src_r][src_c]
else:
    text += "-"

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) It adds - to textต่อ text ด้วย -

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It adds - to textต่อ text ด้วย -
    Correct! So every row keeps its length.ถูกต้อง! ทุกแถวจึงยาวเท่าเดิม
  • (B) It stopsหยุดทำงาน
    Wrong: the if check keeps it from causing an error.ผิด: การตรวจด้วย if ทำให้ไม่เกิด error
  • (C) It uses a character from the first rowใช้ตัวอักษรแถวแรก
    Wrong: the program takes no character in this case.ผิด: โปรแกรมไม่ได้หยิบตัวอักษรในกรณีนี้
  • (D) It skips the cell, so the row gets shorterข้ามช่องนั้น ทำให้แถวสั้นลง
    Wrong: the result must keep the same size.ผิด: ภาพผลต้องมีขนาดเท่าเดิม

ข้อ 9

9 / 10

Which angle gives the same result as -90?มุม -90 ให้ผลเท่ากับมุมใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 270270

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 9090
    Wrong: 90 turns the other way.ผิด: 90 หมุนคนละทิศ
  • (B) 00
    Wrong: 0 does not turn the picture.ผิด: 0 ไม่ได้หมุนภาพ
  • (C) 180180
    Wrong: 180 turns the picture upside down.ผิด: 180 ทำให้ภาพกลับหัว
  • (D) 270270
    Correct! The two angles differ by 360°.ถูกต้อง! ทั้งสองมุมต่างกัน 360°

ข้อ 10

10 / 10

If you need only a 90° turn, which method suits better?
Example 12:
text += picture[size - c - 1][r]
Example 13:
src_c = round(math.cos(theta) * x + math.sin(theta) * y + center)
src_r = round(-math.sin(theta) * x + math.cos(theta) * y + center)
text += picture[src_r][src_c]
ถ้าต้องการแค่หมุน 90° วิธีใดเหมาะกว่า?
ตัวอย่างที่ 12:
text += picture[size - c - 1][r]
ตัวอย่างที่ 13:
src_c = round(math.cos(theta) * x + math.sin(theta) * y + center)
src_r = round(-math.sin(theta) * x + math.cos(theta) * y + center)
text += picture[src_r][src_c]

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Example 12, because it is short and needs no roundingตัวอย่างที่ 12 เพราะสั้นและไม่ต้องปัดเศษ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Example 13, because it has cosตัวอย่างที่ 13 เพราะมี cos
    Wrong: using cos does not make the result at 90° any better.ผิด: การใช้ cos ไม่ได้ทำให้ผลที่ 90° ดีขึ้น
  • (B) Example 12, because it is short and needs no roundingตัวอย่างที่ 12 เพราะสั้นและไม่ต้องปัดเศษ
    Correct! A whole-number index gives an exact result.ถูกต้อง! index จำนวนเต็มให้ผลตรงทุกตัว
  • (C) Neither can do itทั้งสองทำไม่ได้
    Wrong: both methods give an exact 90° turn.ผิด: ทั้งสองวิธีหมุน 90° ได้ตรงทุกตัว
  • (D) Both are needed togetherต้องใช้ทั้งสองวิธีพร้อมกัน
    Wrong: one method is enough.ผิด: วิธีเดียวก็ได้ผลครบ