Created
October 16, 2012 18:20
-
-
Save flightcrank/3901049 to your computer and use it in GitHub Desktop.
Bresenham's line algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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