How to Implement Bresenham

Embed Size (px)

Citation preview

  • 7/31/2019 How to Implement Bresenham

    1/4

    How To Implement Bresenhams Line Drawing Algorithm In C?

    2 Comments

    by

    raksdesouza

    , 04-17-2011 at 10:13 AM (8167 Views)The Bresenhams algorithm offers the following four advantages over the other line drawing algorithms:

    1) It involves simple addition and does not perform any multiplication or division. As a result, it consumesless time and memory during execution.

    2) It involves simple integer arithmetic and not real or floating-point arithmetic.

    3) Its calculations use simple constants instead of complex first- or second-order equations.

    To draw a line between two endpoints, the Bresenhams algorithm evaluates all the intermediate pointsnearest the line, instead of simply rounding the attained value. Following Figure shows a line drawn up tothe point (xn, yn), with the next point (xn+1, yn+1) yet to be evaluated:

    This figure shows the line drawn up to the point (xn, yn). The next point is decided between (xn+1, yn)and (xn+1, yn+1).

    After plotting the current point, the Bresenhams algorithm calculates the value of the successive point.In Figure after you plot (xn, yn), the next point chosen is (xn+1, yn) and (xn+1, yn+1), whichever is nearestto the line. The next point is determined by checking if the midpoint D, whose value is (xn+1, yn+1/2),lies above or below the line. If D lies above the line, as it does in above Figure, the point (xn+1, yn) isclosest to the line; if D lies below the line, (xn+1, yn+1) is closest to the line. For example, the equation ofthe line for the function g(x, y) is:

    g(x, y) = Lx + My + N ....(1)

    A point (xk, yk) lies on the line if g(xk, yk) = 0, below the line if g(xk, yk) > 0, and above the line if g(xk,yk) < 0. These relationships enable you to decide the position of point D and to select the next point tobe plotted.

    The equation of the line in the slope-intercept form is:

    y = m x + c= (y/x) x + c.

    => x . y = y . x + x . c

    http://techforum4u.com/entry.php/796-How-To-Implement-Bresenham%E2%80%99s-Line-Drawing-Algorithm-In-C?s=2f6ee3d1cfee8017e4cd8945fa47bf02#commentshttp://techforum4u.com/entry.php/796-How-To-Implement-Bresenham%E2%80%99s-Line-Drawing-Algorithm-In-C?s=2f6ee3d1cfee8017e4cd8945fa47bf02#commentshttp://techforum4u.com/member.php/2171-raksdesouza?s=2f6ee3d1cfee8017e4cd8945fa47bf02http://techforum4u.com/member.php/2171-raksdesouza?s=2f6ee3d1cfee8017e4cd8945fa47bf02http://techforum4u.com/attachment.php?s=2f6ee3d1cfee8017e4cd8945fa47bf02&attachmentid=206&d=1303035488http://techforum4u.com/attachment.php?s=2f6ee3d1cfee8017e4cd8945fa47bf02&attachmentid=206&d=1303035488http://techforum4u.com/member.php/2171-raksdesouza?s=2f6ee3d1cfee8017e4cd8945fa47bf02http://techforum4u.com/entry.php/796-How-To-Implement-Bresenham%E2%80%99s-Line-Drawing-Algorithm-In-C?s=2f6ee3d1cfee8017e4cd8945fa47bf02#comments
  • 7/31/2019 How to Implement Bresenham

    2/4

    As a result, g(x, y) = y . x-x . y + x . c = 0. ....(2)

    To evaluate the values of L, M, and N in equation (1), compare the equation with equation (2). As aresult,

    L = y, M =-x and N = x.c

    To draw a line using Bresenhams algorithm:

    1) Enter the values of the two endpoints of the line, (x1, y1) and (x2, y2).2) Calculate x and y from these endpoints.3) To determine the next pixel, check whether the sign of g(D) is plus or minus. You can determine thisusing the value of g(xn+1, yn+1/2) denoted by pn:

    pn = g(xn+1, yn+1/2)= L (xn+1) + M (yn+ 1/2) + N.

    4) Choose the next pixel depending on the value of pn. If pn < 0, choose pixel (xn+1, yn); if > 0, choosepixel (xn+1, yn+1); and if pn = 0, choose either pixel.

    5) If you choose pixel (xn+1, yn), determine the next pixel from the sign of (xn+2, yn+ 1/2) ded by pn+1:

    pn+1= g(xn+2, yn+1/2)= L (xn+2) + M (yn+1/2) + N

    The increment in p is p1 = pn+1- pn= L = y.

    If you choose pixel (xn+1, yn+1), determine the next pixel from the sign of (xn+2, yn+ 3/2) denoted bypn+1:

    pn+1= g(xn+2, yn+3/2)= L (xn+2) + M (yn+3/2) + N

    The increment in p is, p2 = pn+1- pn= L + M = y-x.

    6) To draw the line, first calculate p0. The first pixel is (x1, y1) and the first midpoint D is at (x1+1, y1+1/2):

    => p0= g(x1+1, y1+ 1/2)

    = L (x1+ 1) + M (y1+ ) + N

    = L x1+ M y1+ N + L + M / 2

    = g(x1, y1) + L + M / 2

    Because (x1, y1) is a point on the line, as a result, g(x1, y1) = 0:

    => p0 = L + M / 2 = y-x/ 2.

    7) Multiply p0, p1 and p2 by 2 to remove the fraction. In all subsequent calculations for the Bresenhamsalgorithm, you need only their sign. To remove the fraction:

  • 7/31/2019 How to Implement Bresenham

    3/4

    p0 = 2 . y-x,

    p1 = 2 . y,

    and p2 = 2 ( y-x)

    8) To draw a line, start with (x1, y1) and initialize p to 0. After plotting the pixel (x1, y1), increment thevalue of p by p0. For subsequent pixels, check the value of p. If p < 0, choose (xn+1, yn) as the next pixel

    and increment p by p1. If p > 0, choose pixel (xn+1, yn+1) and increment p by p2. If p = 0, chooseeither. Plot the subsequent pixels until you reach (x2, y2).

    Following Example shows how to draw a line with 0

  • 7/31/2019 How to Implement Bresenham

    4/4

    putpixel(x1,y1,EGA_WHITE);p = p0;

    for(x=x1+1,y=y1;x