The Coordinate Plane
The coordinate plane puts two number lines at right angles, so that every point on a flat surface gets an address made of two numbers. It is how we draw equations as pictures, and it is also how every screen, map and game world places things. This lesson covers plotting points, quadrants, distance, midpoints and a first look at straight lines.
Axes and ordered pairs
The horizontal number line is the x-axis and the vertical one is the y-axis. They cross at the origin . A point is written as an ordered pair : first go steps right (or left if negative), then steps up (or down if negative).
Order matters: and are different points.
The axes cut the plane into four quadrants, numbered counter-clockwise from the top right:
| Quadrant | Sign of | Sign of | Example |
|---|---|---|---|
| I | |||
| II | |||
| III | |||
| IV |
A point with a zero coordinate, such as or , lies on an axis and belongs to no quadrant.
Plotting points and a line
Matplotlib draws the plane for us. Here are a few points and the line , built from a table of values (the same idea as evaluating an expression in Variables and Expressions):
import matplotlib.pyplot as plt
xs = [-2, -1, 0, 1, 2, 3]
ys = [2 * x - 1 for x in xs]
print(list(zip(xs, ys)))
plt.figure(figsize=(6, 4))
plt.axhline(0, color="gray", lw=1)
plt.axvline(0, color="gray", lw=1)
plt.plot(xs, ys, "o-", label="y = 2x − 1")
for (px, py), name in [((3, 2), "A"), ((-4, 1), "B"), ((-2, -3), "C"), ((5, -1), "D")]:
plt.plot(px, py, "s", color="tab:red")
plt.annotate(f"{name}({px}, {py})", (px, py), textcoords="offset points", xytext=(5, 5))
plt.grid(True, alpha=0.3)
plt.legend()
plt.title("Points and a line on the coordinate plane")[(-2, -5), (-1, -3), (0, -1), (1, 1), (2, 3), (3, 5)]
Slope and intercept
Each step of 1 to the right on the line above raises by 2. That rate is the slope . Between any two points:
A line can be written as , where is the y-intercept, the value where the line crosses the y-axis (). For , and . A positive slope rises to the right, a negative slope falls, and slope 0 is a flat line. A vertical line has no slope, because its run is 0.
Distance and midpoint
The horizontal and vertical gaps between two points form the legs of a right triangle, so the Pythagorean theorem gives the straight-line distance:
From to : the gaps are 6 and 8, so , and the midpoint is .
import math
def distance(p, q):
return math.hypot(q[0] - p[0], q[1] - p[1])
def midpoint(p, q):
return ((p[0] + q[0]) / 2, (p[1] + q[1]) / 2)
def slope(p, q):
return (q[1] - p[1]) / (q[0] - p[0])
A, B = (1, 2), (7, 10)
print("distance:", distance(A, B))
print("midpoint:", midpoint(A, B))
print("slope:", round(slope(A, B), 4))distance: 10.0
midpoint: (4.0, 6.0)
slope: 1.3333
In software
- Screens are coordinate planes with y flipped. In HTML canvas, CSS, SVG and most 2D game frameworks, is the top-left corner and grows downward. Converting a math point to a screen pixel means (or a full mapping, see Screen Space).
- Game worlds and maps store positions as pairs or vectors (see Vectors). Distance checks such as "is the enemy within 5 units?" use the formula above.
- Charts in dashboards, Matplotlib and spreadsheets map data values to plane coordinates.
- In machine learning, each data row is a point. With two features it is a point in the plane, and a classifier draws a line between groups.
W, H = 800, 600
def to_screen(x, y, scale=50):
# math origin in the centre of the canvas, y up -> pixels with y down
return (W / 2 + x * scale, H / 2 - y * scale)
for p in [(0, 0), (2, 3), (-4, -1)]:
print(p, "->", to_screen(*p))(0, 0) -> (400.0, 300.0)
(2, 3) -> (500.0, 150.0)
(-4, -1) -> (200.0, 350.0)
Exercises
Exercise 1: Which quadrant?
Name the quadrant (or axis) of each point: , , , .
Answer
II, IV, on the y-axis, III.
Solution
Look only at the signs:
(−7, 3) x −, y + → II
(4, −4) x +, y − → IV
(0, −2) x = 0 → on the y-axis (no quadrant)
(−1, −9) x −, y − → IIIExercise 2: Distance and midpoint
Find the distance and the midpoint between and .
Answer
Distance 13, midpoint .
Solution
import math
print(math.hypot(10 - (-2), 6 - 1), ((-2 + 10) / 2, (1 + 6) / 2))13.0 (4.0, 3.5)
Exercise 3: Line through two points
Find the equation of the line through and . Where does it cross the x-axis?
Answer
; it crosses the x-axis at .
Solution
Exercise 4: Flip to the screen
A canvas puts the math origin at its bottom-left corner, with . Write to_screen(x, y) returning pixel coordinates ( down from the top) and convert , and .
Answer
, , .
Solution
is unchanged. The math counts up from the bottom while the pixel counts down from the top, so .
def to_screen(x, y, h=300):
return (x, h - y)
print(to_screen(0, 0), to_screen(400, 300), to_screen(100, 50))(0, 300) (400, 0) (100, 250)
Test yourself
In which quadrant is the point ?
- I
- II
- III
- IV
What is the slope of the line through and ?
- 2
- −2
On an HTML canvas, what happens to a point's pixel when the object moves up the screen?
- It increases
- It decreases
- It stays the same
- It becomes negative