# Incremental Lines

A different approach to drawing a line is to start at one of the points and add a small increment in both directions until the end point is reached. This approach is usually called DDA (digital differential analyzer), the algorithm is easy to express in terms of vector math. Given two points, `p0`

and `p1`

, find the vector between them. Find how many steps the line needs to take and normalize the vector to this value. Set the current point to `p0`

, loop trough all steps in the line and increment the current point by the normalized vector on each iteration.

How do we determine how many times the delta vector needs to be added (I'll call this "number of steps")? The line still has a major and a minor axis, the length of the larger axis is the number of steps the algorithm needs to take. The \(\Delta X\) and \(\Delta Y\) variables are the components of hte vector between the two lines. To normalize to the number of steps, divide both values by the number of steps. Next, set the plot pixel to the start x and y coordinates. For each step in the line, plot the current pixel and increment both the `x`

and `y`

coordinates.

void Line(Image& image, int x0, int y0, int x1, int y1, Color& val) { float xDelta = float(x1 - x0); float yDelta = float(y1 - y0); int steps = max(abs(x1 - x0), abs(y1 - y0)); if (steps == 0) { PutPixel(image, x0, y0, val); return; } float xStep = xDelta / float(steps); float yStep = yDelta / float(steps); float x = float(x0); float y = float(y0); for (int i = 0; i <= steps; ++i) { PutPixel(image, (int)(x), (int)(y), val); y += yStep; x += xStep; } }