Math for Programming / Part 1: Space and motion

Coordinates: giving a place an address

A coordinate is a small answer to “where?” On a flat screen, two numbers are enough to place a point.

Lesson 1 of 13About 10 minutesBuilds on: ordered pairs and the coordinate plane

Start with a map grid

If you have ever found a seat by row and column, you have used a coordinate system. A screen works the same way. One number tells a program how far across to go; another tells it how far down. We write the pair as (x, y). The starting point, called the origin, is (0, 0).

In common 2D screen coordinates, including the HTML canvas and Godot’s 2D scenes, the origin is at the upper-left corner. Positive x runs right and positive y runs down. That differs from most school graphs, where positive y points up, so it is worth checking the convention before calculating anything.

xy(0, 0) (100, 80) School graph: y goes up xy(0, 0) (100, 80) Screen: y goes down
The same pair, (100, 80), lands in different places depending on which way y points.
Worked example

A dot is at (40, 30), measured in screen units. Move it 15 units right and 10 units down. Add to each coordinate separately: x becomes 40 + 15 = 55; y becomes 30 + 10 = 40. Its new position is (55, 40). On a school graph, “down” would mean subtracting from y instead.

Flipping between the two systems

Suppose a window is 300 pixels tall and you want to plot a math graph inside it. A graph point at height y should appear 300 − y pixels from the top. So the graph point (100, 80) is drawn at screen position (100, 220). The same subtraction converts back the other way. This tiny formula is behind almost every chart a program draws.

GDScript
# Convert a graph point (y up) into a screen point (y down).
func graph_to_screen(p: Vector2, height: float) -> Vector2:
    return Vector2(p.x, height - p.y)

print(graph_to_screen(Vector2(100, 80), 300))  # (100, 220)

Units and grids

Numbers alone do not tell us whether they mean pixels, meters, or tiles. Many games are built on a tile grid. If each tile is 50 pixels wide, a character at x = 137 is in tile column 137 ÷ 50 = 2.74, which we round down to column 2 (counting from 0). Rounding down is called the floor. The same idea tells a program which square of a chessboard you clicked, or which cell of a spreadsheet is under the mouse.

Figure

Click anywhere on the grid.

Screen position: — Graph position (y up): — Tile (50-pixel tiles): —
Common mistake

Mixing the two y-directions. If a character jumps and you add to y on a screen, it moves down instead of up. In screen coordinates, “up” means a smaller y.

Where this comes from in school math

In Algebra I, an ordered pair locates a point on the coordinate plane, and an equation such as y = 2x + 1 gives each x a matching y. A program uses the same ordered-pair idea to place a dot or draw that line. Geometry adds measurement: lengths and angles between points. The screen’s only twist is the direction of the y-axis.

Where you will see it

  • Games: positions of characters, enemies, and pickups.
  • Interfaces: where a button sits and whether a click landed on it.
  • Charts: converting data values into pixel positions.
  • Maps: latitude and longitude are a coordinate system on a sphere.

In Godot, a 2D node’s position is a Vector2. That name leads to the next lesson: a pair of numbers can describe a position, but it can also describe a change in position.

Exercises

A window is 400 pixels tall. Where does the graph point (50, 100) appear on screen?

At (50, 300), because 400 − 100 = 300.

With 32-pixel tiles, which column contains x = 100?

100 ÷ 32 = 3.125, and the floor is 3. So column 3 (the fourth column, counting from 0).

On a screen, a sprite at (200, 150) moves “up” 40 pixels. Where is it now?

(200, 110). Up on a screen means subtracting from y.