Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified.

Similar presentations


Presentation on theme: "Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified."— Presentation transcript:

1 Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified.

2 WHERE TO DRAW A LINE?? Line drawing is accomplished by calculating intermediate positions along the line path between specified end points. Precise definition of line drawing Given two points P and Q in the plane, both with integer coordinates, determine which pixels on a raster screen should be on in order to make a picture of a unit- width line segment starting from P and ending at Q.

3 Line drawing (cont) The thinnest line is of one-pixel wide. We will concentrate on drawing a line of 1 pixel resolution. The Cartesian slope-intercept equation for a straight line is y= m. x + b m is the slope of the line and b is the y intercept. Given the endpoints of a line segment. m = y2-y1 / x2-x1 b= y1-m.x1

4 Bresenham’s Line Algorithm An accurate, efficient raster line drawing algorithm developed by Bresenham, scan converts lines using only incremental integer calculations that can be adapted to display circles and other curves.

5 xk xk+1 Xk+2 Xk+3 yk yk+1 yk+2 yk+3 Section of the screen grid showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope 0<m<1

6 In the figure mentioned left side vertical axis show scan line position and horizontal axes identify pixel columns. The sampling method at unit x interval cannot be adopted to the above lines, because we need to decide which of the two possible pixel position is closer to the line path at each sample step.

7 Starting from the left end point (x 0,y 0 ) of a given line, we step to each successive column (x position) and plot the pixel whose scan-line y value closest to the line path. Assuming we have determined that the pixel at (x k,y k ) is to be displayed, we next need to decide which pixel to plot in column x k+1. (x k+1,yk) or (x k+1, y k+1 )‏ Illustration of Bresenham’s Algorithm Consider the scan conversion process for the lines with positive lines with positive slope less than 1.

8

9

10 The difference between these 2 separations is Choices are (x k +1, y k ) and (x k +1, y K +1)‏ d 1 = y – y k = m(x k + 1) + b – y k d 2 = (y k + 1) – y = y k + 1- m(x k + 1) – b d1-d2 = 2m(xk + 1) – 2 yk + 2b – 1

11 A decision parameter pk for the kth step in the line algorithm can be obtained by rearranging above equation so that it involves only integer calculations. The decision parameter pk is obtained by substituting m = Δy/ Δx Define Pk = Δx ( d1-d2) = 2Δy.xk-2 Δx. yk + c

12  The sign of Pk is the same as the sign of d1- d2, since Δx > 0.  Parameter c is a constant and has the value 2Δy + Δx(2b-1) (independent of pixel position)‏  Check the sign of pk (i.e, if d1 < d2) then pk is negative. Then pixel at yk is closer to line-path than pixel at yk +1 We plot lower pixel in such a case (xk+1, yk). Otherwise, upper pixel will be plotted (xk+1, yk+1).

13  At step k + 1, the decision parameter can be evaluated as, pk+1 = 2Δyxk+1 - 2Δxyk+1 + c  Taking the difference of pk+ 1 and pk we get the following. pk+1 – pk = 2Δy(xk+1- xk)-2Δx(yk+1 – yk)‏ But, xk+1 = xk +1, so that pk+1 = pk + 2Δy - 2 Δx(yk+1 – yk)‏ Where the term yk+1-yk is either 0 or 1, depending on the sign of parameter pk

14 The first parameter p 0 is directly computed from the equation Pk = Δx ( d1-d2) = 2Δy.xk-2 Δx. yk + c ie p 0 = 2 Δy x k - 2 Δx y k + c = 2 Δy x k – 2 Δy + Δx (2b-1)‏ Since (x 0,y 0 ) satisfies the line equation, we also have y 0 = Δy/ Δx * x 0 + b Combining the above 2 equations, we will have p 0 = 2Δy – Δx The constants 2Δy and 2Δy-2Δx are calculated once for each time to be scan converted

15 So, the arithmetic involves only integer addition and subtraction of 2 constants 1.Input the two end points and store the left end Point in (x0,y0)‏ 2. Load (x 0,y 0 ) into the frame buffer (plot the first point)‏ 3. Calculate the constants Δx, Δy, 2Δy and 2Δy-2Δx and obtain the starting value for the decision parameter as p 0 = 2Δy- Δx Steps for Bresenhms Line Drawing Algorithm

16 4. At each x k along the line, starting at k=0, perform the following test: If p k < 0, the next point is (x k +1, y k ) and p k+1 = p k + 2Δy Otherwise Point to plot is (x k +1, y k +1) p k+1 = p k + 2Δy - 2Δx 5. Repeat step 4 (above step) Δx times


Download ppt "Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified."

Similar presentations


Ads by Google