Coordinate Geometry and Lines
This reference lesson collects the analytic geometry that calculus takes for granted: distance and midpoints, slopes, the equations of a line, parallel and perpendicular lines, circles, and where two lines meet. Tangent lines, linear approximations and Newton's method all reuse these formulas.
Points, distance and midpoint
Slope and the equations of a line
The slope of the line through and , , is : the rise per unit run. A vertical line has no slope.
| Form | Equation | Use it when you know |
|---|---|---|
| Point-slope | a point and the slope (tangent lines!) | |
| Slope-intercept | slope and -intercept | |
| General | anything, including vertical lines () | |
| Vertical | ||
| Horizontal | slope 0 | |
| Two-intercept | the intercepts and (both ) |
| Relation | Condition on slopes | General-form test |
|---|---|---|
| Parallel | ||
| Perpendicular |
The perpendicular rule follows from the dot product: the line has normal vector , and two lines are perpendicular exactly when their normals are. See The Dot Product.
Distance from a point to a line
The numerator is a signed quantity: its sign says which side of the line the point is on. Dividing by the length of the normal turns it into a true distance.
Circles and conics
A circle with centre and radius is . To read the centre from an expanded equation, complete the square:
Parabolas , ellipses and hyperbolas are treated fully in Conic Sections; a gentler survey is Conic Sections in the essentials course.
import sympy as sp
x, y = sp.symbols('x y')
A, B = (1, 5), (7, -3)
m = sp.Rational(B[1] - A[1], B[0] - A[0])
line = sp.Eq(y - A[1], m * (x - A[0]))
print("slope:", m, " line:", sp.expand(sp.solve(line, y)[0]))
print("distance:", sp.sqrt((B[0] - A[0])**2 + (B[1] - A[1])**2))
print("midpoint:", ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2))
perp_slope = -1 / m
print("perpendicular bisector: y =", sp.expand(perp_slope * (x - 4) + 1))slope: -4/3 line: 19/3 - 4*x/3
distance: 10
midpoint: (4.0, 1.0)
perpendicular bisector: y = 3*x/4 - 2
SymPy prints with the constant first; it is the same line.
Where two lines meet
Two lines and form a linear system. By Cramer's rule, with :
In software
Geometry code (games, CAD, GIS, collision detection) uses these formulas constantly, usually in the parametric form instead of , because vertical lines and segments are then no special case. The determinant is a 2D cross product; it tells you if two segments cross, and which way a polygon turns. Because can be tiny but nonzero for almost-parallel lines, robust code compares it with a tolerance, not with 0.
def segment_intersection(p, p2, q, q2, eps=1e-12):
d = (p2[0] - p[0], p2[1] - p[1])
e = (q2[0] - q[0], q2[1] - q[1])
denom = d[0] * e[1] - d[1] * e[0] # 2D cross product
if abs(denom) < eps:
return None # parallel
w = (q[0] - p[0], q[1] - p[1])
t = (w[0] * e[1] - w[1] * e[0]) / denom
u = (w[0] * d[1] - w[1] * d[0]) / denom
if 0 <= t <= 1 and 0 <= u <= 1:
return (p[0] + t * d[0], p[1] + t * d[1])
return None
print(segment_intersection((0, 0), (4, 4), (0, 4), (4, 0)))
print(segment_intersection((0, 0), (1, 1), (3, 0), (4, -2)))
print(segment_intersection((0, 0), (2, 1), (0, 1), (4, 3)))(2.0, 2.0)
None
None
The second pair of segments would meet only if extended; the third pair is parallel.
Exercises
Exercise 1: Tangent line
The line is tangent to a curve at . Write it in point-slope form, and give the line perpendicular to it at the same point.
Answer
; the perpendicular (normal) line is , i.e. .
Solution
At the line passes through , so the point is and the slope 3. A perpendicular slope satisfies , so : .
Exercise 2: Centre and radius
Find the centre and radius of .
Answer
Centre , radius 3.
Solution
Exercise 3: Point to line
Find the distance from to the line .
Answer
.
Solution
Exercise 4: Intersection (code)
Solve and with Cramer's rule in Python, using fractions.Fraction so the answer is exact.
Answer
, .
Solution
, , .
from fractions import Fraction as F
A1, B1, C1 = F(2), F(3), F(12)
A2, B2, C2 = F(1), F(-1), F(1)
D = A1 * B2 - A2 * B1
print(D, (C1 * B2 - C2 * B1) / D, (A1 * C2 - A2 * C1) / D)-5 3 2
Test yourself
A line has slope . What is the slope of a line perpendicular to it?
Which form can describe a vertical line?
- Point-slope with slope
- with
- None of them
In segment-intersection code, the 2D cross product of the two direction vectors is 0. What does it mean?
- The segments cross at their midpoints
- The segments are parallel (or collinear)
- The segments are perpendicular
- One segment has length 0, always