Math for Programming / Keep going

Practice set

Twenty short problems, one or two from each lesson, plus a challenge that combines several ideas.

These problems mix ideas from every lesson. Work each one on paper first, then open it to check. If one stumps you, the label tells you which lesson to revisit.

1. Coordinates. A window is 600 pixels tall. A chart point has graph coordinates (120, 450). Where is it drawn on screen?

(120, 150), since 600 − 450 = 150.

2. Coordinates. Tiles are 64 pixels wide and tall. Which tile contains the pixel (200, 130)?

Column ⌊200 ÷ 64⌋ = 3, row ⌊130 ÷ 64⌋ = 2.

3. Vectors. Find the vector from A = (−2, 5) to B = (4, −3), and its length.

B − A = (6, −8). Length √(36 + 64) = 10.

4. Vectors. A pickup radius is 30 px. Is a coin at offset (20, 25) collected? Use squared lengths.

20² + 25² = 1,025 and 30² = 900. Since 1,025 > 900, no.

5. Transformations. Rotate (4, 0) by 90° around the origin. Then rotate the result by another 90°.

(0, 4), then (−4, 0). Two quarter turns make a half turn.

6. Transformations. Scale the point (7, 3) by 2 around the pivot (5, 5).

Offset (2, −2), scaled (4, −4), plus pivot: (9, 1).

7. Motion. A ship has velocity (90, −30) px/s. Where is it 0.5 s after leaving (100, 200)?

(100 + 45, 200 − 15) = (145, 185).

8. Motion. A ball is thrown upward at 490 px/s with gravity 980 px/s². When does it stop rising?

Velocity falls by 980 each second, so it reaches 0 after 490 ÷ 980 = 0.5 s.

9. Angles. Convert 45° to radians and give the unit direction vector on a school graph.

π/4 ≈ 0.785. (cos 45°, sin 45°) ≈ (0.707, 0.707).

10. Angles. An arrow is fired at 60° with speed 200. Find its velocity on a school graph.

(200 × 0.5, 200 × 0.866) ≈ (100, 173).

11. Dot product. Compute (3, −2) · (4, 6). What does the answer say about the angle?

12 − 12 = 0. The vectors are perpendicular.

12. Dot product. A guard faces (1, 0). The unit direction to a player is (0.6, 0.8). Is the player inside a 120° vision cone?

Half the cone is 60° and cos 60° = 0.5. The dot product is 0.6 > 0.5, so yes.

13. Interpolation. Remap a temperature of 25 from the range 0–40 to a bar 0–320 px wide.

25 ÷ 40 = 0.625; 0.625 × 320 = 200 px.

14. Binary. What is the largest number 10 bits can store, starting from 0?

2¹⁰ − 1 = 1,023.

15. Binary. Convert the hex color #3C896D into red, green, and blue values.

3C = 60, 89 = 137, 6D = 109.

16. Remainders. An animation has 6 frames, numbered 0–5. Which frame shows on update number 40?

40 % 6 = 4.

17. Logic. Rewrite not (has_key and door_near) without parentheses around “and.”

not has_key or not door_near.

18. Randomness. With two dice, which sum is more likely: 4 or 9?

A sum of 4 has probability 3/36 and a sum of 9 has 4/36, so 9 is more likely.

19. Growth. Doubling a list from 50,000 to 100,000 items adds how many binary-search steps?

Just one. Each extra step doubles the size you can search.

20. Pathfinding. With four-way movement and no walls, how many steps from (1, 1) to (8, 5)? With free diagonals?

Manhattan: 7 + 4 = 11. With diagonals: max(7, 4) = 7.

Challenge: put it together

A turret game

A turret at (400, 300) faces (1, 0) and has a 90° vision cone. An enemy is at (520, 210) and walks at 40 px/s toward the turret.

  1. What is the vector from the turret to the enemy, and how far away is the enemy?
  2. Is the enemy inside the vision cone?
  3. How long until the enemy reaches the turret?
Show the solution

1. (520 − 400, 210 − 300) = (120, −90). Length √(14,400 + 8,100) = √22,500 = 150 px.
2. Unit vector (0.8, −0.6). Dot with (1, 0) is 0.8, and cos 45° ≈ 0.707. Since 0.8 > 0.707, the turret sees it.
3. 150 ÷ 40 = 3.75 seconds.