Download presentation

Presentation is loading. Please wait.

Published byShannon Soden Modified about 1 year ago

2
The lines of this object appear continuous However, they are made of pixels 2April 13, 2015

3
Some useful definitions Rasterization: Process of determining which pixels provide the best approximation to a desired line on the screen. Scan Conversion: Combination of rasterization and generating the picture in scan line order. 3April 13, 2015

4
Straight lines must appear as straight lines. They must start and end accurately Lines should have constant brightness along their length Lines should drawn rapidly 4April 13, 2015

5
For horizontal, vertical and 45º lines, the choice of raster elements is obvious. This lines exhibit constant brightness along the length: 5April 13, 2015

6
For any other orientation the choice is more difficult: 6April 13, 2015

7
Rasterization of straight lines. Rasterization yields uneven brightness: Horizontal and vertical lines appear brighter than the 45º lines. 7April 13, 2015

8
The equation of a straight line is given by:y=m.x+b Algorithm 1 : Direct Scan Conversion 1. Start at the pixel for the left-hand endpoint x1 2. Step along the pixels horizontally until you reach the right-hand end of the line, xr 3. For each pixel compute the corresponding y value 4. Round this value to the nearest integer to select the nearest pixel 8April 13, 2015

9
x = x1; while (x <= xr){ y = m*x + b; y = Round (y); PlotPixel (x, y); /* Set the pixel at (x,y) on */ x = x + 1; } The algorithm performs a floating-point multiplication for every step in x. This method therefore requires an enormous number of floating-point multiplications, and is therefore more expensive 9April 13, 2015 Help for programming

10
Algorithm 2 : Digital Differential Analyzer (DDA ) The differential equation of a straight line is given by: OR The solution of the finite difference approximation is: You need only compute m once, as the start of the scan-conversion The DDA algorithm runs rather slowly because it requires real arithmetic (floating-point operations). 10April 13, 2015

11
DDA algorithm for lines with -1 < m < 1 ( No. of Colum more than number of rows) x = x1; y = y1; while (x <= xr){ y = y + m; y = Round (y); PlotPixel (x, y); x = x + 1; } x2 – x1 > y2 – y1 Slope (m) = if Then step on Column and find x 11April 13, 2015

12
Reverse the roles of x and y using a unit step in y, and 1/m for x. Gaps occur when m > 1 Switching the roles of x and y when m>1 Step on the rows and find y 12April 13, 2015

13
Help to write General program dx = x2 – x1 ; dy = y2 – y1; If dx > dy length = dx ; else length = dy ; Xinc = dx / length ; Yinc = dy / length ; x = x + 0.5 ; y = y + 0.5 ; for ( i=1 ; i <= length ; i++) { putpixel (x, y, c) ; x = x + Xinc ; y = y + Yinc ; } 13April 13, 2015

14
stepxy 12010 22110.8 32211.6 42312.4 52413.2 62514 72614.8 82715.6 92816.4 102917.2 113018 Dx = 30 – 20 = 10Dy = 18 – 10 = 8Larger length = 10 = Dx Y inc = 8 / 10 = 0.8X inc = 10 / 10 = 1 14April 13, 2015 Implementation DDA algorithm by hand

15
Algorithm 3 : Bresenham’s algorithm (1965) This algorithm uses only integer arithmetic, and runs significantly faster. Key idea: (error) is the distance between the actual line and the nearest grid locations. Initialize error: e=-1/2 Error is given by: e=e+m Reinitialize error: when e>0 15April 13, 2015

16
16April 13, 2015

17
To draw line with Bresenham’s algorithm follow the following steps 1.In each step change one of the axis in the value 2. The other axis change or not change depended on the value of (error (e)) 3. Determine the largest length ( Dx or Dy ) 4. Loop will be from 1 to largest length 5. Find the initiate value of(e) as following e = - 0. 5 6. Start to draw line with the point ( x1, y1 ) 7. In each step plot-pixel and determine the new values of ( x, y) 17April 13, 2015

18
8. In each step check sign of (e) 9. If the sign of (e) positive then (y) will be incremented by (1), and subtract one from (e). { in case of Dx is the largest length}, otherwise {if Dy is the largest length then x will be incremented} 10. If (e) is negative or zero, then the value of (y) will remained as it without change. 11. In each step the value of (x) will increase by (1). 12. Also in each step (e) will increase with 18April 13, 2015

19
However, this algorithm does not lead to integer arithmetic. Scaling by: 2* dx void Bresenham (int x1, int y1, int xr, int yr) { int x,y; /* coordinates of pixel being drawn */ int dy, dx; int ne; /* integer scaled error term */ x = x1; y = y1; /* start at left endpoint */ ie = 2 * dy - dx; /* initialize the error term */ while (x <= xr){ /* pixel-drawing loop */ PlotPixel (x,y); /* draw the pixel */ if (ie > 0) { y = y + 1; ne = ne - 2 * dx; /* can replaces e = e - 1 */ } x = x + 1; ne = ne + 2 * dy; /* can replaces e = e + m */ } 19April 13, 2015 Help for programming

20
Go through the steps of the Bresenham line drawing algorithm (by hand) for a line going from (21,12) to (29,16) stepxy 20April 13, 2015

21
21April 13, 2015

22
In each step (across the X-axis) the height increased with (M) (slop) If H was positive we change the row value and subtract one from height If the slop was gentle H > 0.5 H = H +M H = H + M - 1 Rewrite H > 0.5 H- 0.5 > 0 Multiply by 2 get 2H – 1 > 0 H = Multiply by Dx get 2. Dx. H – Dx > 0 Suppose G = 2. Dx. H – DxG > 0 22April 13, 2015 H will be OR

23
Now how G change from Colum to another If H new = H old + M If H new = H old + M - 1 Key is to use G > 0 instead of use H > 0.5 23April 13, 2015

24
If the end points was integer numbers the initial value for H equal M Then the initial value for G equal For each Colum check G if it was positive then move to next row and add To G Other wise keep the same row and add To G 24April 13, 2015

25
25April 13, 2015 Home work

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google