Sub topic 8
Horizontal and Vertical Distances
This chapter shows how to find the distance between two points that lie on the same horizontal or the same vertical line. It assumes you can already plot and read points in all four quadrants, and that you can subtract signed numbers and take an absolute value. You don't need square roots or the Pythagorean theorem here. Those come in a later chapter, when the two points are not aligned.
Distance along a line you can count
Take two points on the plane. If you can draw a horizontal or vertical segment between them, you can measure the segment by counting grid steps. Each step is one unit on the axis scale.
Counting works, but it is slow and error-prone when points are far apart or sit on opposite sides of an axis. The goal of this chapter is to turn the counting into a short subtraction. That subtraction is the same operation you used on the number line.
Distance on a single number line between positions a and b is |b - a|. The coordinate plane is two number lines glued together. On a horizontal segment, only the x-coordinate changes. On a vertical segment, only the y-coordinate changes. So each case reduces to the single number-line formula.
Horizontal distance: the y-coordinates match
Two points (x1, y) and (x2, y) have the same y-coordinate, so they lie on one horizontal line. The distance between them is:
horizontal distance = |x2 - x1|Example: find the distance from (-3, 2) to (4, 2).
- Both points have y = 2, so the segment is horizontal.
- Subtract the x-coordinates:
4 - (-3) = 7. - The absolute value of 7 is 7.
The distance is 7 units. You can check by counting: 3 units from -3 to the y-axis, then 4 more units to x = 4. The total is 3 + 4 = 7.
Vertical distance: the x-coordinates match
Two points (x, y1) and (x, y2) have the same x-coordinate, so they lie on one vertical line. The distance is:
vertical distance = |y2 - y1|Example: find the distance from (5, -6) to (5, -1).
- Both points have x = 5, so the segment is vertical.
- Subtract the y-coordinates:
-1 - (-6) = 5. - The distance is 5 units.
Here both points are below the x-axis. The segment never crosses an axis, and the subtraction handles that without any special case.
Why the absolute value matters
Subtraction gives a signed result. Subtracting in the opposite order flips the sign:
4 - (-3) = 7-3 - 4 = -7
Distance cannot be negative, and it should not depend on which point you call first. The absolute value removes the sign, so |4 - (-3)| = |-3 - 4| = 7.
An alternative is to subtract the smaller coordinate from the larger one. That gives the same answer, but you must compare the numbers first. The absolute value does this for you.
Crossing an axis
When a segment crosses an axis, the two coordinates have opposite signs. For example, (-8, 1) and (3, 1) lie on opposite sides of the y-axis. The subtraction 3 - (-8) = 11 already handles this. It is equivalent to adding the two distances to the axis: |-8| + |3| = 8 + 3 = 11.
The table summarizes the cases for a horizontal segment between x-values a and b.
| Case | Example x-values | Calculation | Distance | Column | Column |
|---|---|---|---|---|---|
| Both positive | 2 and 9 | \ | 9 - 2\ | 7 | |
| Both negative | -9 and -2 | \ | -2 - (-9)\ | 7 | |
| Opposite signs | -4 and 6 | \ | 6 - (-4)\ | 10 | |
| One value is 0 | 0 and -5 | \ | -5 - 0\ | 5 | |
| Same value | 3 and 3 | \ | 3 - 3\ | 0 |
The same table holds for vertical segments if you replace x with y.
Notice that the same two numbers in the first and second rows give distance 7. Mirroring a pair of points across the y-axis changes their signs but not the distance between them. You will use this fact when you study reflections.
Distance from a point to an axis
A special case of these formulas is the distance from a point to an axis. The axes are themselves lines with a fixed coordinate:
- The x-axis is the line
y = 0. - The y-axis is the line
x = 0.
The shortest path from (x, y) to the x-axis goes straight down or up to (x, 0). That is a vertical segment of length |y - 0| = |y|. Likewise, the shortest path to the y-axis ends at (0, y) and has length |x|.
So the point (-4, 5) is 5 units from the x-axis and 4 units from the y-axis. Many learners reverse these. A quick check is that the distance to the x-axis depends on how high or low the point is, which is the y-coordinate.
Computing it in code
The function below checks which case applies and applies the matching formula. It raises an error for slanted segments instead of returning a wrong number.
def axis_distance(p, q):
"""Distance between two points on a shared horizontal or vertical line."""
x1, y1 = p
x2, y2 = q
if y1 == y2:
return abs(x2 - x1)
if x1 == x2:
return abs(y2 - y1)
raise ValueError("points are not on a horizontal or vertical line")
def distance_to_axes(p):
"""Return (distance to x-axis, distance to y-axis)."""
x, y = p
return abs(y), abs(x)
print(axis_distance((-3, 2), (4, 2)))
print(axis_distance((5, -6), (5, -1)))
print(axis_distance((-2, -5), (-2, 3)))
print(axis_distance((4, 2), (-3, 2)))
print(distance_to_axes((-4, 5)))
try:
axis_distance((1, 1), (2, 3))
except ValueError as error:
print("error:", error)The output is:
7
5
8
7
(5, 4)
error: points are not on a horizontal or vertical lineThe fourth line shows the same segment as the first, with the points in the opposite order. The distance is unchanged because of the absolute value.
Edge cases to expect
- Identical points. If both coordinates match, the first branch returns 0. That is the correct distance, and it is not an error.
- Decimals and fractions. The formulas work for any real coordinates. The distance from
(0.5, 2)to(-1.5, 2)is|-1.5 - 0.5| = 2.0. With floating-point numbers, comparisons such asy1 == y2can fail when values come from earlier calculations that were slightly rounded. For measured data, compare with a tolerance, for example withmath.isclose. - Slanted segments. If neither coordinate matches, subtracting only x or only y gives the length of one leg, not the length of the segment. That leg is useful later, but it is not the distance.
Using the scale
All distances so far are counted in units of the axis scale. If the grid lines are labelled every 5 units, a segment that spans 3 grid squares has length 15, not 3. When reading a graph, convert grid squares to units using the axis labels. When the x-axis and y-axis use different scales, counting squares on one axis does not compare directly with counting squares on the other.
The formulas avoid this issue because they work on coordinate values, not on squares. Use the labelled coordinates and the formulas, and treat square-counting only as a quick check on a graph with a scale of 1.
Common errors
- Adding instead of subtracting. Writing
4 + 3 = 7happens to work for(-3, 2)and(4, 2)but fails for(2, 2)and(9, 2). The reliable rule is to subtract. The sum of absolute values is valid only when the coordinates have opposite signs. - Forgetting to subtract a negative.
4 - (-3)is4 + 3. Dropping the parentheses and computing4 - 3 = 1is the most common sign mistake. - Subtracting the wrong coordinate. For a horizontal segment, the y-values are equal, so subtracting them gives 0. Subtract the coordinates that differ.
- Dropping the absolute value. A negative distance is a sign that the points were taken in the opposite order.
- Treating a slanted segment as axis-aligned. Always confirm that one coordinate matches before using these formulas.
Where this leads
Horizontal and vertical distances are the building blocks for the rest of the plane. The distance formula for slanted segments uses the horizontal difference and the vertical difference as the two legs of a right triangle. The perimeter and area of rectangles with sides parallel to the axes come from these same two subtractions. Counting these distances correctly now makes those chapters straightforward.
Exercise
Find the distance between (-7, 4) and (2, 4), and the distance between (3, -9) and (3, -2). State which formula you used for each.
Show answer
The first pair shares y = 4, so the segment is horizontal. The distance is |2 - (-7)| = |9| = 9. The second pair shares x = 3, so the segment is vertical. The distance is |-2 - (-9)| = |7| = 7.
Exercise
A point is at (-6, -2.5). How far is it from the x-axis and from the y-axis? Which axis is it closer to?
Show answer
The distance to the x-axis is |y| = |-2.5| = 2.5. The distance to the y-axis is |x| = |-6| = 6. The point is closer to the x-axis.
Exercise
The points (a, 3) and (-5, 3) are 12 units apart, and a is positive. Find a.
Show answer
The segment is horizontal, so |a - (-5)| = 12, which means |a + 5| = 12. Then a + 5 = 12 or a + 5 = -12, giving a = 7 or a = -17. Since a is positive, a = 7.