Actually it is the same for dy>dx
as dx>dy
. But you should probably check the order in which the points are given. How I would do it:
Consider start
and end
to be input points, mem
is the index of pixel in a framebuffer (or image or whatever) with given width
and height
.
k = d_y / d_x;
if (-1 <= k && k <= 1) {
c0 = 2 * abs(d_y);
c1 = c0 - 2 * abs(d_x);
p = c0 - abs(d_x);
if (start->x <= end->x) {
for (int i = start->x + 1; i < end->x; ++i) {
if (p < 0) {
p += c0;
++mem;
} else {
p += c1;
if (k >= 0) {
mem += width + 1;
} else {
mem += (1 - width);
}
}
colorBuffer[mem] = currentColor;
}
} else {
for (int i = start->x - 1; i > end->x; --i) {
if (p < 0) {
p += c0;
--mem;
} else {
p += c1;
if (k >= 0) {
mem -= width + 1;
} else {
mem += width - 1;
}
}
colorBuffer[mem] = currentColor;
}
}
}
else {
c0 = 2 * abs(d_x);
c1 = c0 - 2 * abs(d_y);
p = c0 - abs(d_y);
//this is the same as dx >= dy, just switch x and y
...