TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Coordinate Geometry

Coordinate Geometry

Coordinate geometry puts shapes on the x-y grid, so that questions about lengths, angles and positions become arithmetic on numbers. A handful of formulas (distance, midpoint, slope, the distance from a point to a line) answer most of the geometry questions that come up in games, maps and drawing tools.

The core formulas

For points A=(x1,y1)A = (x_1, y_1) and B=(x2,y2)B = (x_2, y_2):

Quantity Formula Where it comes from
distance d=(x2−x1)2+(y2−y1)2d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} Pythagorean theorem on the horizontal and vertical gaps
midpoint M=(x1+x22,y1+y22)M = \left(\frac{x_1 + x_2}{2}, \frac{y_1 + y_2}{2}\right) average of the coordinates
point a fraction tt of the way from AA to BB A+t(B−A)A + t(B - A) vectors (Vectors in Geometry)
slope m=y2−y1x2−x1m = \frac{y_2 - y_1}{x_2 - x_1} rise over run
parallel lines m1=m2m_1 = m_2 same steepness
perpendicular lines m1⋅m2=−1m_1 \cdot m_2 = -1 one slope is the negative reciprocal of the other

Line equations (slope-intercept y=mx+by = mx + b, point-slope y−y1=m(x−x1)y - y_1 = m(x - x_1), and the general form Ax+By+C=0Ax + By + C = 0) are covered in Slope and Equations of Lines. A vertical line has no slope (the run is zero), which is one reason code often prefers direction vectors or the general form.

python · try it yourself
from fractions import Fraction
import math

A, B = (-2, 1), (4, 9)
M = (Fraction(A[0] + B[0], 2), Fraction(A[1] + B[1], 2))
m = Fraction(B[1] - A[1], B[0] - A[0])
perp = -1 / m
b = M[1] - perp * M[0]          # perpendicular bisector y = perp·x + b passes through M

print("distance:", math.dist(A, B))
print(f"midpoint: ({M[0]}, {M[1]})")
print("slope:", m, "  perpendicular slope:", perp)
print(f"perpendicular bisector: y = {perp}·x + {b}")
P = (5, 2)                                   # a point on the bisector: −3/4·5 + 23/4 = 2
print("P on bisector?", P[1] == perp * P[0] + b, "  dist to A, B:", math.dist(P, A), math.dist(P, B))
distance: 10.0
midpoint: (1, 5)
slope: 4/3   perpendicular slope: -3/4
perpendicular bisector: y = -3/4·x + 23/4
P on bisector? True   dist to A, B: 7.0710678118654755 7.0710678118654755

The perpendicular bisector of AB‾\overline{AB} is the set of all points equally far from AA and BB, as the check on PP shows. Fraction keeps slopes exact instead of 1.3333…. Perpendicular bisectors of a triangle's sides meet at the circumcentre, the centre of the circle through all three corners (Circles).

Distance from a point to a line

The shortest distance from P=(x0,y0)P = (x_0, y_0) to the line Ax+By+C=0Ax + By + C = 0 is measured along the perpendicular:

distance=∣Ax0+By0+C∣A2+B2\text{distance} = \frac{\lvert A x_0 + B y_0 + C \rvert}{\sqrt{A^2 + B^2}}

Without the absolute value it is a signed distance whose sign says which side of the line PP is on. Three points are collinear (on one line) exactly when the triangle they form has zero area, which the 2D cross product tests with integers only:

python · try it yourself
import math

def point_line_distance(A, B, C, p):
    return abs(A * p[0] + B * p[1] + C) / math.hypot(A, B)

def foot_of_perpendicular(A, B, C, p):
    k = (A * p[0] + B * p[1] + C) / (A * A + B * B)
    return (round(p[0] - A * k, 6), round(p[1] - B * k, 6))

print("distance from (5, 6) to 3x + 4y − 12 = 0:", point_line_distance(3, 4, -12, (5, 6)))
F = foot_of_perpendicular(3, 4, -12, (5, 6))
print("closest point on the line:", F, "  check 3x + 4y =", round(3 * F[0] + 4 * F[1], 6))

def collinear(p, q, r):
    return (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]) == 0

print("(1, 1), (3, 5), (6, 11) collinear?", collinear((1, 1), (3, 5), (6, 11)))
print("(1, 1), (3, 5), (6, 12) collinear?", collinear((1, 1), (3, 5), (6, 12)))
distance from (5, 6) to 3x + 4y − 12 = 0: 5.4
closest point on the line: (1.76, 1.68)   check 3x + 4y = 12.0
(1, 1), (3, 5), (6, 11) collinear? True
(1, 1), (3, 5), (6, 12) collinear? False

The distance 27/5=5.427/5 = 5.4 agrees with dist⁡((5,6),(1.76,1.68))=3.242+4.322=5.4\operatorname{dist}((5, 6), (1.76, 1.68)) = \sqrt{3.24^2 + 4.32^2} = 5.4. Snapping a dragged point onto a guide line in a drawing tool is exactly this foot-of-perpendicular calculation. See also Closest Point.

In software: distance and midpoint in games

Two of the most-run lines of game code are "who is closest?" and "where should the camera look?". Comparing squared distances skips the square root, since for non-negative numbers d1<d2d_1 < d_2 exactly when d12<d22d_1^2 < d_2^2:

python · try it yourself
player = (0, 0)
enemies = {"slime": (3, 4), "bat": (-2, 6), "orc": (5, -1), "imp": (1, -5)}

def dist2(p, q):
    return (p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2

for name, pos in enemies.items():
    print(f"{name:5} at {pos}: distance² = {dist2(player, pos)}")
nearest = min(enemies, key=lambda n: dist2(player, enemies[n]))
print("nearest:", nearest, "  in attack range 5?", dist2(player, enemies[nearest]) <= 5 ** 2)

# a two-player camera: look at the midpoint, zoom out as the players separate
p1, p2 = (2, 3), (10, 9)
centre = ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)
separation = dist2(p1, p2) ** 0.5
print("camera centre:", centre, "  view width:", max(8, separation + 4))
slime at (3, 4): distance² = 25
bat   at (-2, 6): distance² = 40
orc   at (5, -1): distance² = 26
imp   at (1, -5): distance² = 26
nearest: slime   in attack range 5? True
camera centre: (6.0, 6.0)   view width: 14.0

The midpoint is the t=12t = \frac{1}{2} case of linear interpolation; moving the camera a fraction of the way toward the target each frame gives a smooth follow (Lerp). More collision uses of these formulas are in Collision Geometry, and a review aimed at calculus is in Coordinate Geometry Review.

Exercises

Exercise 1: Distance and midpoint

Find the distance between (3,−4)(3, -4) and (−5,2)(-5, 2), and their midpoint.

Answer

Distance 10, midpoint (−1,−1)(-1, -1).

Solution
d=(−5−3)2+(2−(−4))2=64+36=100=10M=((3−5)/2,(−4+2)/2)=(−1,−1)\begin{aligned} d &= \sqrt{(-5 - 3)^2 + (2 - (-4))^2} = \sqrt{64 + 36} = \sqrt{100} = 10 \\ M &= ((3 - 5)/2, (-4 + 2)/2) = (-1, -1) \end{aligned}

Exercise 2: A perpendicular line

Find the equation of the line through (1,2)(1, 2) perpendicular to y=2x+5y = 2x + 5.

Answer

y=−12x+52y = -\frac{1}{2}x + \frac{5}{2}

Solution
perpendicular slope=−1/2y−2=−12(x−1)⇒y=−12x+12+2=−12x+52\begin{aligned} &\text{perpendicular slope} = -1/2 \\ &y - 2 = -\tfrac{1}{2}(x - 1) \Rightarrow y = -\tfrac{1}{2}x + \tfrac{1}{2} + 2 = -\tfrac{1}{2}x + \tfrac{5}{2} \end{aligned}

Exercise 3: On one line?

Are (1,1)(1, 1), (4,7)(4, 7) and (6,11)(6, 11) collinear?

Answer

Yes.

Solution
slope (1,1)→(4,7)=6/3=2slope (4,7)→(6,11)=4/2=2equal slopes through a shared point→one line (y=2x−1)\begin{aligned} &\text{slope } (1,1) \to (4,7) = 6/3 = 2 \\ &\text{slope } (4,7) \to (6,11) = 4/2 = 2 \\ &\text{equal slopes through a shared point} \to \text{one line } (y = 2x - 1) \end{aligned}

Exercise 4: Point to line

How far is (2,−1)(2, -1) from the line 5x−12y+4=05x - 12y + 4 = 0?

Answer

2

Solution
∣5⋅2−12⋅(−1)+4∣25+144=∣10+12+4∣13=2613=2\frac{\lvert 5 \cdot 2 - 12 \cdot (-1) + 4 \rvert}{\sqrt{25 + 144}} = \frac{\lvert 10 + 12 + 4 \rvert}{13} = \frac{26}{13} = 2

Exercise 5: A quarter of the way

A projectile flies in a straight line from (2,1)(2, 1) to (10,5)(10, 5). Where is it after a quarter of the trip? Write a small function point_at(A, B, t) and check.

Answer

(4,2)(4, 2)

Solution
A+14(B−A)=(2,1)+14(8,4)=(2+2,1+1)=(4,2)A + \tfrac{1}{4}(B - A) = (2, 1) + \tfrac{1}{4}(8, 4) = (2 + 2, 1 + 1) = (4, 2)
python · try it yourself
def point_at(A, B, t):
    return (A[0] + t * (B[0] - A[0]), A[1] + t * (B[1] - A[1]))

print(point_at((2, 1), (10, 5), 0.25))
(4.0, 2.0)

Test yourself

What is the midpoint of (−4,7)(-4, 7) and (6,−1)(6, -1)?

  1. (1,3)(1, 3)
  2. (5,−4)(5, -4)
  3. (2,6)(2, 6)
  4. (1,4)(1, 4)

A line has slope 2/5. What is the slope of a line perpendicular to it?

  1. 2/5
  2. 5/2
  3. −5/2
  4. −2/5

Why do games compare squared distances to find the nearest enemy?

  1. Squared distances are more accurate
  2. The ordering is the same and it avoids computing square roots
  3. Square roots are not allowed in games
  4. Squared distances work only in 3D