Skip to content

Instantly share code, notes, and snippets.

@flightcrank
Created October 16, 2012 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flightcrank/3901049 to your computer and use it in GitHub Desktop.
Save flightcrank/3901049 to your computer and use it in GitHub Desktop.
Bresenham's line algorithm
void draw_line(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
float slope = (float)dy / (float)dx;
float error = 0;
int y = y1;
int i;
for (i = 0; i < x2; i++) {
if (i == 0) {
draw_pixel(x1 + i, y, 255);
continue;
}
error += slope;
if (error >= 0.5) {
y++;
error--;
}
draw_pixel(x1 + i, y, 255);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment