Line Drawing by Algorithm

  • Published on

  • View

  • Download

Embed Size (px)


Line Drawing by Algorithm. Line Drawing Algorithms. Line drawn as pixels Graphics system Projects the endpoints to their pixel locations in the frame buffer (screen coordinates as integers) Finds a path of pixels between the two Loads the color - PowerPoint PPT Presentation


  • Line Drawing by Algorithm

  • Line Drawing AlgorithmsLine drawn as pixelsGraphics system Projects the endpoints to their pixel locations in the frame buffer (screen coordinates as integers)Finds a path of pixels between the twoLoads the colorPlots the line on the monitor from frame buffer (video controller)Rounding causes all lines except horizontal or vertical to be displayed as jigsaw appearance (low resolution)Improvement: high resolution, or adjust pixel intensities on the line path.

  • Line Drawing AlgorithmsLine equationSlope-intercept form y = m . x + b slope m Y-intercept bSlope


  • Line Drawing AlgorithmsDDA (Digital Differential Analyzer)Scan conversion line algorithmLine sampled at regular intervals of x, then corresponding y is calculatedFrom left to right

  • Line Drawing Algorithmsvoid lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs (dx) > fabs (dy)) steps = fabs (dx); else steps = fabs (dy); xIncrement = float (dx) / float (steps); yIncrement = float (dy) / float (steps); setPixel (round (x), round (y)); for (k = 0; k < steps; k++) { x += xIncrement; y += yIncrement; setPixel (round (x), round (y)); } }

  • Line Drawing AlgorithmsAdvantageDoes not calculate coordinates based on the complete equation (uses offset method)DisadvantageRound-off errors are accumulated, thus line diverges more and more from straight lineRound-off operations take timePerform integer arithmetic by storing float as integers in numerator and denominator and performing integer artithmetic.

  • Observation on lines.while( n-- ) {draw(x,y);move right;if( below line )move up;}

  • Line Drawing AlgorithmsBresenhams line drawingEfficient line drawing algorithm using only incremental integer calculationsCan be adapted to draw circles and other curvesPrincipleVertical axes show scan line positions Horizontal axes show pixel columns At each step, determine the best next pixel based on the sign of an integer parameter whose value is proportional to the difference between the vertical separations of the two pixel positions from the actual line.

  • Bresenhams AlgorithmImprove upon DDA algorithm to use integer arithmetic only.Applicable to circle drawing too. We discuss only the line drawing here. An Accurate and efficient raster line generating Algo. And scan converts lines using only incrementa1 integer calculations that can be adapted to display circles and other curves

  • Testing for the side of a line.Need a test to determine which side of a line a pixel lies.Write the line in implicit form:

    Easy to proveY=m*X+Cnts below.

  • Decision VariablesVariables a and b record the distances to the pixel centers of (i+1, j) and (i+1, j+1)If d1 < d2, then y=jIf d1 > d2, then y= j+1

  • The vertical axes show-scan-line positions, and the horizontal axes identify pixel columns.Sampling at unit x intervals in these examples, we need to decide which of two possible pixel positions is closer to the line path at each sample step. Starting from the left endpoint we need to determine at the next sampleposition whether to plot the pixel at position (11, 11) or the one at (11, 12) Line with negative slope-line path starting from the left endpoint at pixel position (50, 50). In this one, do we select the next pixel position as (51,50)or as (51,49)? These questions are answered with Bresenham's line algorithm by testing the sign of an integer parameter, whose value is proportional to the difference between the separations of the two pixel positions from the actual line path.

    we- first consider the scan-conversion process for lines with positive slope less than 1. Pixel positions along a line path are then determined by sampling at unit x intervals. Starting from the left endpoint (x0, yo) of a given line, we step to each successive column ( x position) and plot the pixel whose scan-line y value is closest to the line path.demonstrates the Kh step in this process. Assuming we have determined that the pixel at (xk, yk) is to be displayed, we next need to decide which pixel to plot incolumn x k + 1, . Our choices are the pixels at positions ( Xk+l, yk) and (xk+l, yk+l).At sampling position xk+l, we label vertical pixel separations from themathematical line path as d1 and d2

  • Y co-ordinate at column Xk+1 can be calculated as Y=m(Xk+1)+b thenD1=y-yk=m(xk+1)+b-ykD2=(Yk+1) y=Yk+1 m(Xk+1)-bDifference in these two sepration isD1-D2=2m(Xk+1)-2Yk+2b-1Decision Parameter Pk( Kth iteration)Pk=DX(d1-d2)=2Dy * Xk-2Dx * Yk+C Note Pk has Same Sign as d1-d2Sine DX>1,C is constant & C=2Dy+Dx(2b-1) is Independent of Pixel Position ,Ignored In recursive cal of PkPk+1 can be calculated then Recursive Pk+1 be asPk+1=Pk+2Dy-2Dx(Yk+1- Yk) then Po=2Dy-Dx

  • void lineares (int xa ,Int ya , int x b , int yb)(int dx = a b s ( x a - x b) , dy = abs (ya - yb);int p = 2 * dy - d x ;int twoDy = 2 * dy, twoDyDx = 2 *(dy - dx ) ;int x , y, xEnd:/ * Determine which point to use a s start, which as end * /if ( x a > x b ) {x = x b ;Y = yb;xEnd = x a ;}else {x = xa;Y = ya;xEnd = xb;}setpixel ( x , y);while (x < xEnd) {x++;if (p < 0 )p+= twoDy;else {y + + ;P += twoDyDx;}setpixel ( x , y);}}


View more >