Presentation is loading. Please wait.

Presentation is loading. Please wait.

Line Drawing Algorithms Asst. Prof. Dr. Ahmet Sayar Kocaeli University Computer Engineering Advanced Computer Graphics Spring 2012.

Similar presentations


Presentation on theme: "Line Drawing Algorithms Asst. Prof. Dr. Ahmet Sayar Kocaeli University Computer Engineering Advanced Computer Graphics Spring 2012."— Presentation transcript:

1 Line Drawing Algorithms Asst. Prof. Dr. Ahmet Sayar Kocaeli University Computer Engineering Advanced Computer Graphics Spring 2012

2 The Problem Of Scan Conversion But what happens when we try to draw this on a pixel based display? How do we choose which pixels to turn on?

3 Considerations Considerations to keep in mind: – The line has to look good Avoid jaggies – Lines should pass from the endpoints (if possible) – It has to be lightening fast! How many lines need to be drawn in a typical scene? This is going to come back to bite us again and again

4 Special Lines - Horizontal 4/110

5 Special Lines - Horizontal Increment x by 1, keep y constant 5/110

6 Special Lines - Vertical 6/110

7 Special Lines - Vertical Keep x constant, increment y by 1 7/110

8 Special Lines - Diagonal 8/110

9 Special Lines - Diagonal Increment x by 1, increment y by 1 9/110

10 Line Equations Lets quickly review the equations involved in drawing lines x y y0y0 y end x end x0x0 Slope-intercept line equation: where:

11 Lines & Slopes The slope of a line ( m ) is defined by its start and end coordinates The diagram below shows some examples of lines and their slopes m = 0 m = - 1 / 3 m = - 1 / 2 m = -1 m = -2 m = -4 m = m = 1 / 3 m = 1 / 2 m = 1 m = 2 m = 4 m = 0

12 A Very Simple Solution We could simply work out the corresponding y coordinate for each unit x coordinate Lets consider the following example: x y (2, 2) (7, 5)

13 Simple solution Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) // sample unit intervals in x coordinate DrawPixel ( x i, Round ( y i ) ) //find the nearest pixel for each sampled point x i+1 = x i + 1 y i+1 = m x i+1 + b 13/110

14 A Very Simple Solution (cont…) First work out m and b : x y (2, 2) (7, 5) Now for each x value work out the y value:

15 A Very Simple Solution (cont…) Now just round off the results and turn on these pixels to draw our line

16 A Very Simple Solution (cont…) However, this approach is just way too slow In particular look out for: – The equation y = mx + b requires the multiplication of m by x – Rounding off the resulting y coordinates – a floating point multiplication, addition, rounding We need a faster solution

17 A Quick Note About Slopes In the previous example we chose to solve the parametric line equation to give us the y coordinate for each unit x coordinate What if we had done it the other way around? So this gives us: where: and

18 A Quick Note About Slopes (cont…) Leaving out the details this gives us: We can see easily that this line doesnt look very good! We choose which way to work out the line pixels based on the slope of the line

19 A Quick Note About Slopes (cont…) If the slope of a line is between -1 and 1 then we work out the y coordinates for a line based on its unit x coordinates Otherwise we do the opposite – x coordinates are computed based on unit y coordinates m = 0 m = - 1 / 3 m = - 1 / 2 m = -1 m = -2 m = -4 m = m = 1 / 3 m = 1 / 2 m = 1 m = 2 m = 4 m = 0

20 The DDA Algorithm The digital differential analyzer (DDA) algorithm takes an incremental approach in order to speed up scan conversion Simply calculate y k+1 based on y k

21 DDA - simply

22 The DDA Algorithm (cont…) When the slope of the line is between -1 and 1 begin at the first point in the line and, by incrementing the x coordinate by 1, calculate the corresponding y coordinates as follows: When the slope is outside these limits, increment the y coordinate by 1 and calculate the corresponding x coordinates as follows:

23 The DDA Algorithm (cont…) Again the values calculated by the equations used by the DDA algorithm must be rounded to match pixel values (x k, y k ) (x k +1, y k +m) (x k, round(y k )) (x k +1, round(y k +m)) (x k, y k ) (x k + 1 / m, y k +1) (round(x k ), y k ) (round(x k + 1 / m ), y k +1)

24 Digital Differential Algorithm input line endpoints, (x 0,y 0 ) and (x n, y n ) set pixel at position (x 0,y 0 ) calculate slope m Case |m|1: repeat the following steps until (x n, y n ) is reached: y i+1 = y i + y/ x x i+1 = x i + 1 set pixel at position (x i+1,Round(y i+1 )) Case |m|>1: repeat the following steps until (x n, y n ) is reached: x i+1 = x i + x/ y y i+1 = y i + 1 set pixel at position (Round(x i+1 ), y i+1 )

25 DDA - Problems Floating point operations Rounding Can we do better? ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = y i + m 25/110

26 Does it Work? Graphics26 It seems to work okay for lines with a slope of 1 or less, but doesnt work well for lines with slope greater than 1 – lines become more discontinuous in appearance and we must add more than 1 pixel per column to make it work. Solution? - use symmetry.

27 The DDA Algorithm Summary The DDA algorithm is much faster than our previous attempt – In particular, there are no longer any multiplications involved However, there are still two big issues: – 2 rounds and 2 adds per pixel Is there a simpler way ? Can we use only integer arithmetic ? – Easier to implement in hardware

28 Conclusion In this lecture we took a very brief look at how graphics hardware works Drawing lines to pixel based displays is time consuming so we need good ways to do it The DDA algorithm is pretty good – but we can do better Next time well look at the Bresenham line algorithm

29 Question: why do we add m to y at each step? Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = m x i + m + b y i+1 = m ( x i + 1 ) + b y i = m x i + b y i+1 = y i + m

30 Question: when and why do we add 1/m to x at each step? ???

31 Bresenham Line Drawing Algorithm,

32 The Big Idea Move across the x axis in unit intervals and at each step choose between two different y coordinates For example, from position (2, 3) we have to choose between (3, 3) and (3, 4) We would like the point that is closer to the original line (x k, y k ) (x k +1, y k ) (x k +1, y k +1)

33 The y coordinate on the mathematical line at x k +1 is: Deriving The Bresenham Line Algorithm At sample position x k +1 the vertical separations from the mathematical line are labelled d upper and d lower y ykyk Y k +1 xk+1xk+1 d lower d upper

34 Deriving The Bresenham Line Algorithm (cont…) So, d upper and d lower are given as follows: and: We can use these to make a simple decision about which pixel is closer to the mathematical line

35 Deriving The Bresenham Line Algorithm (cont…) This simple decision is based on the difference between the two pixel positions: Lets substitute m with y / x where x and y are the differences between the end-points:

36 Deriving The Bresenham Line Algorithm (cont…) So, a decision parameter p k for the k th step along a line is given by: The sign of the decision parameter p k is the same as that of d lower – d upper If p k is negative, then we choose the lower pixel, otherwise we choose the upper pixel

37 Deriving The Bresenham Line Algorithm (cont…) Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations At step k +1 the decision parameter is given as: Subtracting p k from this we get:

38 Deriving The Bresenham Line Algorithm (cont…) But, x k+1 is the same as x k +1 so: where y k+1 - y k is either 0 or 1 depending on the sign of p k The first decision parameter p0 is evaluated at (x0, y0) is given as:

39 The Bresenham Line Algorithm BRESENHAMS LINE DRAWING ALGORITHM (for | m | < 1.0) 1.Input the two line end-points, storing the left end-point in ( x 0, y 0 ) 2.Plot the point ( x 0, y 0 ) 3.Calculate the constants Δx, Δy, 2Δy, and ( 2Δy - 2Δx ) and get the first value for the decision parameter as: 4.At each x k along the line, starting at k = 0, perform the following test. If p k < 0, the next point to plot is (x k +1, y k ) and:

40 The Bresenham Line Algorithm (cont…) ! The algorithm and derivation above assumes slopes are less than 1. for other slopes we need to adjust the algorithm slightly Otherwise, the next point to plot is ( x k +1, y k +1 ) and: 5.Repeat step 4 ( Δx – 1) times

41 Bresenham Example Lets have a go at this Lets plot the line from (20, 10) to (30, 18) First off calculate all of the constants: – Δx : 10 – Δy : 8 – 2Δy : 16 – 2Δy - 2Δx : -4 Calculate the initial decision parameter p 0 : – p0 = 2Δy – Δx = 6

42 Bresenham Example (cont…) kpkpk (x k+1,y k+1 )

43 Bresenham Exercise Go through the steps of the Bresenham line drawing algorithm for a line going from (21,12) to (29,16)

44 Bresenham Exercise (cont…) kpkpk (x k+1,y k+1 )

45 Bresenham Line Algorithm Summary The Bresenham line algorithm has the following advantages: – An fast incremental algorithm – Uses only integer calculations Comparing this to the DDA algorithm, DDA has the following problems: – Accumulation of round-off errors can make the pixelated line drift away from what was intended – The rounding operations and floating point arithmetic involved are time consuming

46 Backup

47 How to calculate po in Bresenham Algorithm

48


Download ppt "Line Drawing Algorithms Asst. Prof. Dr. Ahmet Sayar Kocaeli University Computer Engineering Advanced Computer Graphics Spring 2012."

Similar presentations


Ads by Google