Skip to content

Instantly share code, notes, and snippets.

@Trinitek
Last active August 29, 2015 14:12
Show Gist options
  • Save Trinitek/98a215081bae0db725da to your computer and use it in GitHub Desktop.
Save Trinitek/98a215081bae0db725da to your computer and use it in GitHub Desktop.
Line code
void drawLine(short x1, short y1, short x2, short y2, char color) {
short dx = x2 - x1;
short dy = y2 - y1;
short newX = x1;
short newY = y1;
// Iterate along the X axis if slope is not steep (m <= 1)
if (dx >= dy) {
for (; newX <= x2; newX++) {
setPixel(
newX,
((dy * newX) / dx) + y1,
color);
}
}
// Iterate along the Y axis if slope is steep (m > 1)
else {
for (; newY <= y2; newY++) {
setPixel(
((dx * newY) / dy) + x1,
newY,
color);
}
}
}
/*
When the X axis is the major axis...
y = mx+b;
setPixel(x, mx+b, color);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment