Skip to content

Commit

Permalink
Adding implementation of angled lines (qmk#3)
Browse files Browse the repository at this point in the history
* Adding implementation of angled lines
  • Loading branch information
Gr1mR3aver authored and tzarc committed May 10, 2021
1 parent b61e16b commit e2b3d52
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion drivers/painter/fallback/qp_fallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,35 @@ bool qp_fallback_line(painter_device_t device, uint16_t x0, uint16_t y0, uint16_
}
}
} else {
// TODO: Angled lines
// draw angled line using Bresenham's algo
uint16_t x = x0;
uint16_t y = y0;
uint16_t slopex = x0 < x1 ? 1 : -1;
uint16_t slopey = y0 < y1 ? 1 : -1;
uint16_t dx = abs(x1 - x0);
uint16_t dy = -abs(y1 - y0);

uint16_t e = dx + dy;
uint16_t e2 = 2 * e;

while (x != x1 && y != y1) {
if (!qp_setpixel(device, x, y, hue, sat, val)) {
return false;
}
e2 = 2 * e;
if (e2 >= dy) {
e += dy;
x += slopex;
}
if (e2 <= dx) {
e += dx;
y += slopey;
}
}
// draw the last pixel
if (!qp_setpixel(device, x, y, hue, sat, val)) {
return false;
}
}

return true;
Expand Down

0 comments on commit e2b3d52

Please sign in to comment.