Sub topic 7
Vertical and Horizontal Lines
This chapter assumes you can compute a slope from two points, write a line in slope-intercept form, and do basic fraction arithmetic. It also assumes you know the earlier results of this tutorial: parallel lines have equal slopes, and perpendicular slopes are negative reciprocals (their product is -1).
Those two rules fail on vertical lines. Here you will see exactly where they fail, what replaces them, and how to write code that handles every case.
Why these lines need separate treatment
The slope formula divides by the change in x:
m = (y2 - y1) / (x2 - x1)
For most lines this works. For a vertical line, x2 - x1 is 0, so the division is undefined. The form y = mx + b cannot describe a vertical line because it has no m to put in.
The slope tests inherit this problem. "Equal slopes" cannot be applied when a slope does not exist, and "product is -1" cannot be applied when one factor is missing. Horizontal lines have a perfectly good slope of 0, but 0 also breaks the perpendicular rule. Both cases need explicit handling.
Horizontal lines
A horizontal line has the same y-value at every point, so every pair of points has y2 - y1 = 0. The slope is 0 / (x2 - x1) = 0 for any nonzero run.
- The equation is y = c, where c is the constant height. This is slope-intercept form with m = 0 and b = c.
- Standard form is 0x + 1y = c.
- Any two horizontal lines have slope 0, so the equal-slope test correctly calls them parallel.
Vertical lines
A vertical line has the same x-value at every point.
- The equation is x = c, where c is the constant x-coordinate.
- The slope is undefined. Do not write "slope = 0" and do not write "no slope". Write "undefined slope".
- Standard form is 1x + 0y = c.
- Two vertical lines x = c1 and x = c2 are parallel when c1 and c2 differ. They are the same line when c1 = c2.
Why "undefined" is the right answer
Take the line through (0, 0) and (0.001, 1). Its slope is 1 / 0.001 = 1000. Move the second point to (0.000001, 1) and the slope becomes 1,000,000. As the line tilts toward vertical, the slope grows without bound, and it never settles on a number. That is what "undefined" means here.
Run the same idea on the perpendicular rule. The negative reciprocal of a huge slope m is -1/m, which is a number very close to 0. So as one line approaches vertical, the line perpendicular to it approaches horizontal. This is the consistent picture behind the special cases below.
Parallel and perpendicular tests with special lines
The table lists every combination for two distinct lines.
| Line 1 | Line 2 | Result | Reason |
|---|---|---|---|
| Vertical | Vertical | Parallel | Both have undefined slope and never meet (unless the x-values match, which makes them the same line) |
| Horizontal | Horizontal | Parallel | Both have slope 0 |
| Vertical | Horizontal | Perpendicular | They meet at a right angle, though the product rule cannot be applied |
| Vertical | Slanted (nonzero slope) | Neither | The slanted line is not at a right angle to the vertical |
| Horizontal | Slanted | Neither | Same reasoning, using slope 0 against a nonzero slope m |
Why the product rule breaks
The rule m1 * m2 = -1 cannot hold when m1 = 0, because 0 times anything is 0. The negative reciprocal of 0 would be -1/0, which does not exist. The rule also cannot be written for a vertical line, since there is no m to multiply.
So the perpendicular rule has two exceptions:
- A line with slope 0 is perpendicular only to a vertical line.
- A vertical line is perpendicular only to a line with slope 0.
Writing lines through a point
When the original line is vertical or horizontal, the answer comes directly from the point and needs no b calculation.
Let the point be (x0, y0).
- Parallel to a vertical line x = c: the answer is x = x0.
- Parallel to a horizontal line y = c: the answer is y = y0.
- Perpendicular to a vertical line: the answer is the horizontal line y = y0.
- Perpendicular to a horizontal line: the answer is the vertical line x = x0.
Example: find the line perpendicular to x = 3 through (5, 2). The given line is vertical, so the perpendicular is horizontal and passes through height 2. The answer is y = 2.
Example: find the line parallel to y = -4 through (1, 7). Horizontal lines stay horizontal, so the answer is y = 7.
Notice that rules 1 and 2 keep the same type as the original line, while rules 3 and 4 switch type. When x0 and y0 are both given, you can check your answer by confirming the line contains the point.
A decision procedure
Check for vertical lines before comparing slopes. The diagram shows the order.
Handling every case in code
The next program stores a line in one of two shapes. A vertical line is ("x", c). Any other line is ("y", m, b), meaning y = mx + b. Fractions keep the arithmetic exact, so a slope such as 1/3 is never rounded.
from fractions import Fraction
def line_from_points(p, q):
(x1, y1), (x2, y2) = p, q
if p == q:
raise ValueError("two distinct points are needed")
if x1 == x2:
return ("x", Fraction(x1))
m = Fraction(y2 - y1, x2 - x1)
return ("y", m, y1 - m * x1)
def describe(line):
if line[0] == "x":
return f"x = {line[1]}"
_, m, b = line
if m == 0:
return f"y = {b}"
sign = "+" if b >= 0 else "-"
return f"y = {m}x {sign} {abs(b)}"
def classify(a, b):
if a == b:
return "the same line"
if a[0] == "x" and b[0] == "x":
return "parallel"
if a[0] == "x":
a, b = b, a # now b is the vertical one, if either is
if b[0] == "x":
return "perpendicular" if a[1] == 0 else "neither"
if a[1] == b[1]:
return "parallel"
if a[1] * b[1] == -1:
return "perpendicular"
return "neither"
def parallel_line(line, point):
x0, y0 = point
if line[0] == "x":
return ("x", Fraction(x0))
m = line[1]
return ("y", m, y0 - m * x0)
def perpendicular_line(line, point):
x0, y0 = point
if line[0] == "x":
return ("y", Fraction(0), Fraction(y0))
m = line[1]
if m == 0:
return ("x", Fraction(x0))
m_perp = -1 / m
return ("y", m_perp, y0 - m_perp * x0)
v = line_from_points((3, 0), (3, 5))
h = line_from_points((0, 2), (7, 2))
print(describe(v), "|", describe(h)) # x = 3 | y = 2
print(classify(v, h)) # perpendicular
print(classify(v, line_from_points((0, 0), (1, 1)))) # neither
print(classify(v, line_from_points((5, 0), (5, 9)))) # parallel
print(classify(h, line_from_points((0, 2), (4, 2)))) # the same line
print(describe(parallel_line(v, (5, 2)))) # x = 5
print(describe(perpendicular_line(v, (5, 2)))) # y = 2
print(describe(perpendicular_line(h, (1, 7)))) # x = 1
slanted = ("y", Fraction(2), Fraction(1)) # y = 2x + 1
print(describe(parallel_line(slanted, (4, 3)))) # y = 2x - 5
print(describe(perpendicular_line(slanted, (4, 3)))) # y = -1/2x + 5The code follows the table. The vertical check comes before any comparison of slopes. Because the slope is never computed for a vertical line, no division by zero occurs. The m == 0 check in perpendicular_line is placed before -1 / m for the same reason.
Edge cases and common errors
- Dividing by zero. Computing a slope from two points with the same x-value crashes in most languages. Test for equal x-values first, as
line_from_pointsdoes. - Saying "same slope" for two vertical lines. Neither slope exists, so the slope test does not apply. Compare the equations: x = c1 and x = c2 are parallel if c1 differs from c2.
- Calling perpendicular lines "reciprocal". The product rule needs the negative sign. The slopes 2 and 1/2 give a product of 1, not -1, so those lines are neither parallel nor perpendicular.
- Forgetting the same-line case. "Parallel" in this tutorial includes coincident lines unless a problem says "distinct". Standard form makes this easy to check: if (A, B, C) is a scalar multiple of (A', B', C'), the lines are identical.
- Swapping x and y. The line through (5, 2) that is perpendicular to x = 3 is y = 2, not x = 2. The perpendicular to a vertical line is horizontal, and a horizontal line has the form y = constant.
- Reading a vertical line off a graph and calling its slope 0. Look at the direction. A line that rises steeply with no horizontal movement has an undefined slope.
Exercises
Exercise
Line 1 passes through (2, -1) and (2, 6). Line 2 passes through (-3, 4) and (8, 4). Are the lines parallel, perpendicular, or neither? Find the point where they meet.
Show answer
Line 1 has equal x-values, so it is the vertical line x = 2. Line 2 has equal y-values, so it is the horizontal line y = 4. A vertical line and a horizontal line are perpendicular. They meet at (2, 4).
Exercise
Write the equation of the line through (-2, 9) that is perpendicular to the line y = 5. Then write the equation of the line through (-2, 9) that is parallel to x = 7.
Show answer
The line y = 5 is horizontal, so the perpendicular is vertical: x = -2. The line x = 7 is vertical, so the parallel is also vertical: x = -2. Both answers are the same line, which makes sense because a line through (-2, 9) that is perpendicular to a horizontal line is also parallel to every vertical line.
Exercise
A student tests the lines 3x + 0y = 12 and x + 0y = -5 by writing both slopes as "0" and concluding they are parallel. The conclusion is correct. What is wrong with the reasoning, and what is the correct reasoning?
Show answer
With B = 0 both lines are vertical (x = 4 and x = -5), so their slopes are undefined, not 0. The lines are parallel because both are vertical with different x-values. A slope of 0 would mean horizontal lines, which is a different case.