The Naïve Way: Drawing a line with brute force
So, you have an equation to draw a line and want to plot it on the screen pixel by pixel. The naïve approach would be to loop through a series of x values, calculate what the y value should be at that x location given the equation, and then plot x, y as a colored pixel. When you do, a very unfortunate issue will arise.
An issue occurs because pixel locations are represented as whole numbers; however, points on a straight line can in fact be floating points. Consider the line in Figure 3.1, showing a grid of pixel locations and a line drawn through them:
Figure 3.1: A grid of pixels and a line
If you were to draw every pixel the line touched (shown by the shaded squares), it wouldn’t give a very accurate presentation of the line. In addition, the real location of any point on the line could calculate to a floating-point value. When this occurs, the value is rounded to an integer to find the closest...