There was a very efficient solution to this problem in common use decades ago when computers were slow and primitive enough that you couldn't waste any resources. (I independently invented this in 1971, but I probably wasn't the first and later inventors of it apparently didn't copy me). Lots of methods are possible now that computers are more powerful, but the old way still works well:

(All integers):

You use an accumulator A and constants SR and FR that are the abs of the X and Y rates of change, swapped if necessary so that SR is the slower rate and FR is the faster rate. You also need three binary flags for whether X, Y were increasing/decreasing and which was faster.

Initialize A to FR/2.

At each step, you will unconditionally bump the position of the faster changing of X or Y and you will subtract SR from A.

If the result of that subtraction is negative, you will also bump the position of the slower changing of X or Y and you will add FR to A.

Note, you don't need to special case equal rates. Whichever you accidentally choose as "faster" when the rates are actually equal, the method will still work.

For elegant C code, avoiding many if statements, I suggest having an array of two counters instead of having scalar x and y counters. Also use an array of two directions 1 or -1 instead of bools for direction. So to bump the faster, instead of something like

Code:

if (y_faster)
if (y_forward)
y++;
else
y--;
else
if (x_forward)
x++;
else
x--;

you only need

Code:

counter[y_faster] += direction[y_faster];

To bump the slower counter you only need

Code:

counter[1-y_faster] += direction[1-y_faster];

I assume you can figure out the setup code that computes:

FR the larger of the two abs rates

SR the smaller of the two abs rates

counter[0] the initial x position

counter[1] the initial y position

direction[0] 1 if x is increasing, otherwise -1

direction[1] 1 if y is increasing, otherwise -1

y_faster 1 if y is changing faster than x, otherwise 0.

For line segment drawing, the integer "rate" is the total delta for that coordinate: segment end minus segment beginning.