Solving Systems by Graphing
Every equation in two unknowns has a graph, and a solution of a system is a point that lies on all of the graphs at once. So you can solve a system by drawing the graphs and finding where they meet. Graphing is less precise than algebra, but it shows how many solutions to expect, and its geometric view is exactly how games test whether a bullet hits a wall.
Where the lines meet
Solve and by graphing.
- Draw both lines (from their intercepts or slopes; see Graphing Linear Equations).
- Read the coordinates of the crossing point: .
- Check algebraically in both equations: ✓ and ✓.
The check matters because a graph only gives an estimate. For and , a careful sketch suggests "about "; the exact answer from substitution is . Use the graph to see the answer, then algebra to nail it.
The three cases in pictures
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 5, 50)
cases = [
("one solution", 2 * x - 1, -x + 5, "y = −x + 5"),
("no solution", 2 * x - 1, 2 * x + 2, "y = 2x + 2"),
("infinitely many", 2 * x - 1, (4 * x - 2) / 2, "4x − 2y = 2"),
]
fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
for ax, (title, y1, y2, label2) in zip(axes, cases):
ax.plot(x, y1, label="y = 2x − 1", linewidth=3, alpha=0.6)
ax.plot(x, y2, "--", label=label2)
ax.set_title(title); ax.grid(True, alpha=0.3); ax.legend(fontsize=7)
axes[0].plot(2, 3, "ko")
plt.tight_layout()
plt.show()
print("crossing, parallel, and the same line")crossing, parallel, and the same line
| Picture | Slopes and intercepts | Solutions |
|---|---|---|
| lines cross | different slopes | exactly one |
| parallel lines | same slope, different intercepts | none |
| one line drawn twice | same slope, same intercept | infinitely many |
So you can classify a linear system without drawing anything: put both equations in the form and compare and . In the third panel, becomes , the same line.
Curves can meet more than once
Graphing works for any pair of equations, not only lines. A line and a parabola can meet twice, once (the line is tangent) or not at all; two parabolas or a line and a circle likewise. For and the graphs cross at and , the solutions found in The Substitution Method. Graphing is often the fastest way to see how many solutions a non-linear system has before you solve it.
In software: segment intersection in 2D games
A game rarely asks where two infinite lines meet. It asks whether this bullet path, from where the bullet was last frame to where it is now, crosses this wall segment. That is a system of equations plus a range check.
Write each segment with a parameter: the bullet is for (), the wall is for . They meet where
That is two equations ( and components) in two unknowns and . Solving it with Cramer's rule gives formulas in terms of the 2D cross product :
If the segments are parallel. Otherwise the lines meet, and the segments meet only if both and lie between 0 and 1.
def cross(a, b):
return a[0] * b[1] - a[1] * b[0]
def segment_hit(p, p_end, q, q_end):
r = (p_end[0] - p[0], p_end[1] - p[1])
s = (q_end[0] - q[0], q_end[1] - q[1])
denom = cross(r, s)
if denom == 0:
return "parallel"
qp = (q[0] - p[0], q[1] - p[1])
t = cross(qp, s) / denom
u = cross(qp, r) / denom
if 0 <= t <= 1 and 0 <= u <= 1:
return f"hit at ({p[0] + t * r[0]}, {p[1] + t * r[1]}), t = {t}"
return f"lines meet but segments miss (t = {t}, u = {u})"
wall = ((0, 4), (4, 0))
print(segment_hit((0, 0), (4, 4), *wall)) # long shot through the wall
print(segment_hit((0, 0), (1, 1), *wall)) # short shot, stops before the wall
print(segment_hit((0, 5), (4, 5), (0, 6), (4, 6)))hit at (2.0, 2.0), t = 0.5
lines meet but segments miss (t = 2.0, u = 0.5)
parallel
In the second call the lines still cross at , but that point is at , twice as far as the bullet travelled this frame, so there is no hit yet. The value of is useful in its own right: it is the fraction of the frame at which the hit happens, which engines use to place the impact effect and for continuous collision detection. The same test powers line-of-sight checks, mouse picking of edges and polygon clipping. Real code also compares the denominator with a small tolerance instead of exactly 0, because coordinates are floats.
Exercises
Exercise 1: Graph and check
Solve and by graphing, then check algebraically.
Answer
.
Solution
Both lines pass through on a sketch. Algebra:
Exercise 2: Classify without drawing
How many solutions does the system , have?
Answer
None: the lines are parallel.
Solution
Exercise 3: Exact from estimate
A graph of and suggests an intersection near . Find the exact point.
Answer
.
Solution
from sympy import symbols, Eq, Rational, solve
x, y = symbols("x y")
print(solve([Eq(y, x * Rational(1, 3) + 1), Eq(y, -x + 4)], [x, y])){x: 9/4, y: 7/4}
Exercise 4: Do the segments cross?
Does the segment from to cross the segment from to ? If so, where?
Answer
Yes, at , halfway along both segments ().
Solution
def cross(a, b):
return a[0] * b[1] - a[1] * b[0]
r, s, qp = (4, 2), (2, -4), (1, 3)
t, u = cross(qp, s) / cross(r, s), cross(qp, r) / cross(r, s)
print(t, u, (1 + t * r[0], 1 + t * r[1]))0.5 0.5 (3.0, 2.0)
Test yourself
Two lines in a system have the same slope and different y-intercepts. What does the graph show?
- One crossing point
- Parallel lines: no solution
- The same line: infinitely many solutions
- Perpendicular lines
Why should you check a solution read from a graph?
- Graphs are always wrong
- A graph only gives an estimate; fractional coordinates are easy to misread
- Algebra and graphs disagree
- It is not necessary
In the segment test, the lines meet at , . Does the moving segment () hit the wall this frame?
- Yes, because is between 0 and 1
- No, because : the crossing lies beyond the end of the segment
- Yes, at 140 % of the wall
- Only if