Bresenham's Line Algorithm
♣ The Algorithm :: Compute the initial values: dx = x 2 - x 1 Inc 2 = 2(dy - dx) dy = y 2 - y 1 d = Inc 1 - dx Inc 1 = 2dy Set (x,y) equal to the lower left-hand endpoint and x end equal to the largest value of x. If dx < 0, then x = x 2 , y = y 2 , x end = x 1 . If dx > 0, then x = x 1 , y = y 1 , x end = x 2 . Plot a point at the current (x,y) coordinates. Test to see whether the entire line has been drawn. If x = x end , stop. Compute the location of the next pixel. If d < 0, then d = d + Inc 1 . If d >e; 0, then d = d + Inc 2 , and then y = y + 1. Increment x: x = x + 1. Plot a point at the current (x,y) coordinates. Go to step 4. ♣ C / C++ Code :: void BresenhamsLine(int x1, int y1, int x2, int y2) { int dx, dy, Inc1, Inc2, d, x, y, xend; dx = x2-x1; dy = y2 - y1; Inc1 = 2*dy; Inc2 = 2*(dy - dx); d = Inc1 - dx; if(dx < 0) { x=x2, y=y2, xend=x1; } else if(dx &