This post is a bit of background on lines, which we’ll be using in the next post to finish off our bus scenario.
Mathematics typically only deals with lines that are infinite in length. However, in programming we more often deal with lines that are finite, stretching from one endpoint to another. There are several different ways to represent infinite and finite lines, which we’ll look at in turn.
Infinite line: y = mx + c
You may well have come across the equation “” before. You can look at it in two ways. One is that it is a way to calculate y, given a value for x (and thus is a way to draw the line). The other is that it is a relation that holds true for points on the line, and is false otherwise. So let’s say you have . If you plug in then you will find the equation is true: the point (1, 5) is on the line. But if you plug in then you will find the equation is false: the point (1, 6) is not on the line.
This form is usually used for straight lines on graphs because it expresses the relation between x and y. However, it has one major weakness: you can’t express a vertical line in this form, which only exists at one value of x. This makes it unsuitable for programming purposes, because you don’t want to have to program a special case for when a line happens to be vertical.
Infinite line: point and direction
Another way to express a line is as a combination of a point on the line (as a vector), and a direction. For example, a point on the line might be (0, 2), and the direction might be (1, 3). In mathematics it is often useful to combine these two into an expression which represents all points on the line. This is done by introducing a variable, which I’ll call scalar, to represent a form of distance along the line:
The idea here is that all points on the line have a value for the scalar variable. So for example, (1.5, 6.5) is on the line: use and you get:
However, there’s no such value of for the point (2, 9), because it’s not on the line. Two slightly unexpected features of this representation are that it doesn’t matter which point on the line you pick for the expression, and you can multiply the direction vector by any constant without it changing the line. So the above line is equivalent to either of the following:
Finite line: two endpoints
The simplest way to represent a finite line is to simply write down its two endpoints. So you might have a line from (1, 3) to (6, 7). That’s a good, simple, unambiguous definition of the line. However, to do any calculations involving the line, you usually need to convert it to another representation, such as the last on our list:
Finite line: point and direction
You can take the two endpoints of a line and turn it into a form that looks identical to our earlier point and direction form. So using our (1, 3) to (6, 7) example, you get:
The difference now is that the line extends from (the start-point) to (the end-point). Any values for the scalar variable outside the 0–1 range would be on the line if it’s infinite, but are not when it’s finite.
End of the Line
Today’s post was a lot of theory without an actual application, but we’ll be using it in our next post when we’ll look at finding out if two finite lines intersect.