Coordinate Systems
A coordinate system gives every point a numeric address, so geometry turns into algebra: a point becomes a pair of numbers, a line or curve becomes an equation, and "where do they meet?" becomes "solve the system". This lesson reviews the Cartesian plane, extends it to three dimensions, and explains why computer screens put the origin in the top-left corner with pointing down.
The Cartesian plane
Two number lines cross at right angles at the origin . The horizontal one is the -axis, the vertical one the -axis. A point is written as an ordered pair : first how far to walk right (negative: left), then how far up (negative: down). Order matters: and are different points.
The axes split the plane into four quadrants, numbered counter-clockwise starting top-right:
| Quadrant | Sign of | Sign of | Example |
|---|---|---|---|
| I | |||
| II | |||
| III | |||
| IV |
Points with lie on the -axis and points with lie on the -axis; they belong to no quadrant. The coordinate plane lesson in Basic Math has a gentler introduction.
import matplotlib.pyplot as plt
points = {"A": (3, 2), "B": (-4, 1), "C": (-2, -3), "D": (2, -2), "E": (0, 3)}
plt.figure(figsize=(5, 5))
plt.axhline(0, color="black", linewidth=1)
plt.axvline(0, color="black", linewidth=1)
for name, (x, y) in points.items():
plt.plot(x, y, "o", color="tab:blue")
plt.annotate(f"{name}({x}, {y})", (x, y), textcoords="offset points", xytext=(6, 6))
for label, pos in {"I": (4, 4), "II": (-4, 4), "III": (-4, -4), "IV": (4, -4)}.items():
plt.text(*pos, label, fontsize=14, color="gray", ha="center")
plt.xlim(-5, 5); plt.ylim(-5, 5)
plt.grid(True, alpha=0.3)
plt.gca().set_aspect("equal")
plt.title("Points and quadrants")
plt.show()
print("plotted", len(points), "points")plotted 5 points
Distance and midpoint
Two formulas turn geometric questions into arithmetic. Both come from the coordinates alone:
For and : the gaps are 5 and 5, so , and . The distance formula is the Pythagorean theorem applied to the horizontal and vertical gaps.
Three dimensions
Add a third axis, , at right angles to the other two, and every point in space gets an address . The distance formula gains one more term: . In maths and in most 3D tools the axes are right-handed: point the right hand's fingers along , curl them toward , and the thumb points along . Some engines use left-handed axes instead; Handedness in the game-math course compares them.
Other coordinate systems describe the same plane differently. Polar coordinates give a point as a distance from the origin and an angle ; they suit circles and rotations and are covered in Pre-Calculus.
In software: screen coordinates
On a computer screen, a canvas or an image, the origin is the top-left corner, grows to the right and grows downward, measured in pixels. That is because screens are drawn row by row from the top, like reading text.
| Maths coordinates | Screen coordinates | |
|---|---|---|
| origin | centre (wherever you choose) | top-left corner |
| direction | up | down |
| units | any real number | pixels (whole numbers) |
| a "counter-clockwise" turn | positive angle | looks clockwise on screen |
To draw a maths graph on screen you map each point. Suppose the graph shows from −10 to 10 and from −5 to 5 on an 800 × 400 canvas:
The (instead of ) is the flip: a large gives a small , near the top.
def to_screen(x, y, xmin=-10, xmax=10, ymin=-5, ymax=5, width=800, height=400):
px = (x - xmin) / (xmax - xmin) * width
py = (ymax - y) / (ymax - ymin) * height # flip: y up -> py down
return round(px), round(py)
def to_math(px, py, xmin=-10, xmax=10, ymin=-5, ymax=5, width=800, height=400):
x = xmin + px / width * (xmax - xmin)
y = ymax - py / height * (ymax - ymin)
return x, y
for p in [(0, 0), (10, 5), (-10, -5), (3, 2)]:
print(p, "->", to_screen(*p))
print("mouse click at (600, 100) is", to_math(600, 100))(0, 0) -> (400, 200)
(10, 5) -> (800, 0)
(-10, -5) -> (0, 400)
(3, 2) -> (520, 120)
mouse click at (600, 100) is (5.0, 2.5)
to_math is the inverse map, used when the user clicks: it turns a pixel back into graph coordinates. Charting libraries, map widgets and game cameras all contain these two functions. In 3D graphics the same idea appears as the last stage of the pipeline, from normalised device coordinates to pixels (Clip Space, NDC and Screen).
Tip: When a sprite moves "up" but your code adds to , it moves down the screen. Decide once which system a variable lives in, and convert only at the drawing step.
Exercises
Exercise 1: Quadrants
Name the quadrant or axis of , , and .
Answer
II, IV, on the -axis, III.
Solution
Exercise 2: Distance and midpoint
Find the distance and the midpoint of and .
Answer
, .
Solution
import math
print(math.dist((-1, 2), (5, 10)), ((-1 + 5) / 2, (2 + 10) / 2))10.0 (2.0, 6.0)
Exercise 3: A 3D distance
How far apart are and ?
Answer
Solution
Exercise 4: Map to a screen
A chart shows from 0 to 100 and from 0 to 50 on a 500 × 250 pixel canvas ( down). Where is the data point drawn? Which data point is at pixel ?
Answer
is drawn at pixel ; pixel is the data point .
Solution
def to_screen(x, y):
return x / 100 * 500, (50 - y) / 50 * 250
def to_data(px, py):
return px / 500 * 100, 50 - py / 250 * 50
print(to_screen(40, 30), to_data(250, 250))(200.0, 100.0) (50.0, 0.0)
Test yourself
In which quadrant is the point ?
- I
- II
- III
- IV
On a 600 px tall canvas that shows from 0 (bottom) to 10 (top), at which pixel row is drawn?
- 600
- 60
- 0
- 300
What is the distance between and ?
- 14
- 10
- 48