Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created January 2, 2011 21:13
Show Gist options
  • Save Cellane/762820 to your computer and use it in GitHub Desktop.
Save Cellane/762820 to your computer and use it in GitHub Desktop.
private void drawLineBresenham (int startX, int startY, int endX, int endY, G_Color color) {
int dY = endY - startY;
int dX = endX - startX;
int fraction, stepX, stepY;
if (dY < 0) {
dY = -dY;
stepY = -1;
} else {
stepY = 1;
}
if (dX < 0) {
dX = -dX;
stepX = -1;
} else {
stepX = 1;
}
dY = 2 * dY;
dX = 2 * dX;
putPixel (startX, startY, color);
if (dX > dY) {
fraction = (2 * dY) - dX;
while (startX != endX) {
if (fraction >= 0) {
startY += stepY;
fraction -= dX;
}
startX += stepX;
fraction += dY;
putPixel (startX, startY, color);
}
} else {
fraction = (2 * dX) - dY;
while (startY != endY) {
if (fraction >= 0) {
startX += stepX;
fraction -= dY;
}
startY += stepY;
fraction += dX;
putPixel (startX, startY, color);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment