Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)

Similar presentations


Presentation on theme: "Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)"— Presentation transcript:

1 Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)
Computer Graphics Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)

2 Bresenham’s Line Algorithm
DDA algorithm has a disadvantage, that is, the round off error. This error happens because the algorithm rounds off the actual floating point line values to integer values (for example, if actual value of a point of line is , the DDA algorithm rounds it to 12 and if the value is 12.62, it will round it to 13). This causes the calculated pixel positions to move away from the actual line path for long lines. For this reason we use another accurate and efficient line drawing algorithm called Bresenham’s algorithm because it was developed by Bresenham. This algorithm draws line by using only integer calculations.

3 Horizontal axis shows the pixel columns
Vertical axis shows the line position. If we take unit x-intervals and we plot a point at (xk,yk), then in order to calculate the y values we need to check which of the two points is closer to the line path at each sample step. That is, we need to decide at the next sample position xk+1 whether to choose the pixel (xk+1,yk) or (xk+1,yk+1), that is, whether to choose point (11,11) or (11,12) for the next step.

4 These issues are solved by Bresenham’s algorithm.
It decides which value to take at next position whether (xk+1, yk) or (xk+1, yk+1). It does this by checking the sign of a decision parameter (pk) which is equal to the difference between the separations of the two pixel positions (yk and yk+1) from the actual line path. If pk is negative it takes the point as (xk+1, yk). If pk is positive it takes point as (xk+1, yk+1)

5

6 Suppose we have determined the first pixel position as (xk,yk).
Now, we have to decide which pixel to plot at xk+1, that is, whether (xk+1,yk) or (xk+1, yk+1). At position (xk+1), we label separation between pixels yk and yk+1 from the line path (y) as d1 and d2. that is, d1 = y – yk; and d2 = yk+1 - y Then we calculate the decision parameter pk as pk = (d1-d2).

7 At pixel xk+ 1, we can calculate y as: y = m.x + b
y = m (xk+ 1) + b Then d1 = y – yk d1 = m (xk+ 1) + b – yk d2 = yk+ 1 – y d2 = yk+1 – (m (xk+ 1) + b) d2 = yk+1 - m (xk+ 1) - b d1 – d2 = (m (xk+1) + b – yk) – (yk m (xk+1) – b) d1 – d2 = m(xk + 1) + b – yk – yk m(xk+1) + b d1 – d2 = 2m(xk + 1) – 2yk + 2b - 1

8 The decision parameter pk for kth step is given as pk = δx(d1 – d2)
pk = 2δy . xk – 2δx. yk + 2δy + δx (2b – 1) pk = 2δy. xk – 2δx. yk c c is a constant as it is not dependent on pixel positions (xk,yk) The decision parameter pk for step k + 1 is given as pk+1 = 2δy. xk+1 – 2δx. yk c => pk+1 – pk = 2δy (xk+1 – xk) – 2δx (yk+1 – yk) xk+1 = xk + 1 pk+1 – pk = 2δy (xk+ 1 – xk) – 2δx (yk+1 – yk) pk+1 – pk = 2δy (1) – 2δx (yk+1 – yk) pk+1 = pk + 2δy – 2δx (yk+1 – yk)

9 If the pixel at yk is closer to line path than yk+1 (that is, d1 < d2) then pk is negative and we take y-value as yk that is we plot the point as (xk+1, yk). Then the term (yk+1 – yk) will become (yk – yk), which is equal to 0. Therefore, pk+1 = pk + 2δy – 2δx (0) => pk+1 = pk + 2δy If yk+1 is closer to line path than yk (that is d1 > d2) then pk is positive and we take y-value as yk+1 that is we plot the point as (xk+1, yk+1). Then the term (yk+1 – yk) will become (yk + 1 – yk), which is equal to 1. Therefore, pk+1 = pk + 2δy – 2δx (1) => pk+1 = pk + 2δy – 2δx

10 Algorithm Input the line endpoints and store left endpoint in (x0, y0)
Load (x0, y0) into frame buffer and plot the first point. Calculate δx, δy, 2δy and 2δy - 2δx calculate p0 = 2δy – δx 4. At each xk, starting at k = 0, do the following if pk < 0, next point to plot is (xk+1, yk) and pk+1 = pk + 2δy if pk > 0, next point to plot is (xk+1, yk+1) and pk+1 = pk + 2δy - 2δx Step 4 is repeated δx times.


Download ppt "Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)"

Similar presentations


Ads by Google