Line Drawing by Algorithm

Post on 01-Jan-2016

37 views 6 download

description

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

Transcript of Line Drawing by Algorithm

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– Plots 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 Algorithms

• Line equation– Slope-intercept form

y = m . x + bslope mY-intercept b

– Slope

– Y-intercept

==x

y

0end

0end

δ

δ=

x-x

y-y=m

mx-y=b 00

Line Drawing Algorithms

• DDA (Digital Differential Analyzer)– Scan conversion line algorithm– Line sampled at regular intervals of x, then

corresponding y is calculated– From left to right

=

)1=δ( m

1+x=x1.0,>mif

)1=δ( m+y=y1,≤mif

yk1+k

xk1+k

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 Algorithms

• Advantage– Does not calculate coordinates based on the

complete equation (uses offset method)

• Disadvantage– Round-off errors are accumulated, thus line

diverges more and more from straight line– Round-off operations take time

• Perform 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 Algorithms

• Bresenham’s line drawing– Efficient line drawing algorithm using only incremental

integer calculations– Can be adapted to draw circles and other curves

• Principle– Vertical 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.

Bresenham’s Algorithm

• Improve 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:

0),( cbyaxyxF

• Easy to proveY=m*X+Cnts below.

Decision Variables• Variables a and b record the distances to the

pixel centers of (i+1, j) and (i+1, j+1)• If d1 < d2, then y=j• If 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 sample

• position 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 end

• point (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 in

• column 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 the

• mathematical line path as d1 and d2

• Y co-ordinate at column Xk+1 can be calculated as Y=m(Xk+1)+b then

• D1=y-yk=m(xk+1)+b-yk• D2=(Yk+1) – y=Yk+1 –m(Xk+1)-b• Difference in these two sepration is• D1-D2=2m(Xk+1)-2Yk+2b-1• Decision 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 Pk

Pk+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);}}