In seventh grade algebra, I learned how to derive the equation for a line. In this context, a “line” implicitly means a two-way-infinite line, shooting off to infinity in either direction. What isn’t taught in school is the much more difficult and subtle question of how to derive the equation of a finite line segment, one with a start and an end.

### Fact 1: The Equation of an Infinite Line

If a (two-way-infinite) line passes through the points (x0,y0),(x1,y1), then (one form of) its equation is:

$y-y_0-\frac{y_1-y_0}{x_1-x_0}(x-x_0)=0.$

### Fact 2: The Intersection of Two Graphs

Suppose the equation f(x,y)=0 has graph A, and the equation g(x,y)=0 has graph B, and f(x,y) and g(x,y) are functions which are never negative. Let C be the intersection of the two graphs: in other words, C is the set of points where A and B overlap each other. Then C is the graph of the equation:

$f(x,y)+g(x,y)=0.$

Proof: If (x,y) is a point in C, that means it’s a point in both A and B (by definition). That means f(x,y)=0 and g(x,y)=0. Therefore, f(x,y)+g(x,y)=0+0=0, so (x,y) satisfies the above equation. Conversely, if (x,y) is a point which satisfies the above equation, then both f(x,y) and g(x,y) must be 0. This is because I said f(x,y) and g(x,y) aren’t allowed to take negative values. So if either f(x,y) or g(x,y) were nonzero, then the sum of the two would definitely be positive and nonzero. Since f(x,y) and g(x,y) are 0, that means (x,y) is part of both A and B.

### Fact 3: The Equation of a Strip

How do we get a finite line segment (what we want) from an infinite line (what we know)? The answer is, we intersect the infinite line with a big strip which extends infinitely up and down. Here’s the picture:

But what’s the equation of the strip? If inequalities were allowed, it would be easy: x0<x<x1. In order to get the strip without using inequalities, we need an equation which is true precisely when the inequality is true. And that is where the biggest difficulty lies.

There are various ways to write an inequality as an equation. Different solutions use different machinery of differing obscurity. One way which doesn’t use too much advanced machinery is with absolute values. Consider the graph of

$y=|x-x_0|+|x-x_1|.$

Geometrically, you can read this as: “y equals the distance from x to x0, plus the distance from x to x1“. If x is to the right of both x0 and x1, this sums up to strictly more than the distance from x0 to x1. Similarly if x is to the left of both x0 and x1. But if x is between x0 and x1, then “the distance from x to x0 plus the distance from x to x1” is equal to the distance from x0 to x1… or |x1-x0| to be precise. Moreover, the sum is only equal to |x1-x0| when x is between x0 and x1. Thus, the equation of the strip from x=x0 to x=x1 is:

$|x-x_0|+|x-x_1|-|x_1-x_0|=0.$

What’s more, the left hand side of the above equation is never negative. That’s because of an important principle called the Triangle Inequality. The Triangle Inequality says that for any number a,b,c, |a-b|<|a-c|+|c-b|. Letting a=x, b=x0, and c=x1, the Triangle Inequality says that the LHS of the above equation is never negative. This is important because it’ll allow us to use the above graph-intersection fact.

### Fact 4: The Equation of a Line Segment

Combining Facts 1, 2, and 3, the equation for the finite line segment from (x0,y0) to (x1,y1) is:

$\left(y-y_0-\frac{y_1-y_0}{x_1-x_0}(x-x_0)\right)^2+|x-x_0|+|x-x_1|-|x_1-x_0|=0.$

The reason that I squared the linear function is to make it be non-negative. This doesn’t change the graph, just lets us perform graph intersection– at the price of making the equation slightly more complicated.

(I’m assuming the line isn’t strictly vertical, i.e., x0≠x1. That case is left to the reader as an exercise!)

## The Equation of a Ray

Now that we have the equation of an infinite line (thanks Mr. Ford!) and a finite line-segment, the next question is, what’s the equation for a ray– a line which goes forever in one direction but not the other? That’s actually much easier. A number equals its absolute value if and only if it’s non-negative, and otherwise, the number equals its negative absolute value. Therefore, x+|x| will be zero if x is negative, and positive if x is non-negative. So the inequality x<0 is equivalent to the equation x+|x|=0. And x+|x| can never be negative– go ahead, try to find an x such that x+|x| is negative, you’ll quickly see why it’s impossible

By translating, the inequality x<x1 is equivalent to the equation

$x-x_1+|x-x_1|=0$

Assume x1>x0. The equation for a ray which hits (x0,y0) and (x1,y1) and extends infinitely to the left, but doesn’t go any further than x=x1 to the right, is:

$\left(y-y_0-\frac{y_1-y_0}{x_1-x_0}(x-x_0)\right)^2+x-x_1+|x-x_1|=0.$

As for rays which shoot off infinitely far to the right, I’ll leave that to the reader