Download - The lines of this object appear continuous However, they are made of pixels 2April 13, 2015
The lines of this objectappear continuous
However, they aremade of pixels
2April 18, 2023
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 18, 2023
• 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 18, 2023
For horizontal, vertical and 45º lines, the choice of raster elements is obvious. This lines exhibit constant brightness along the length:
5April 18, 2023
For any other orientation the choice is more difficult:
6April 18, 2023
Rasterization of straight lines.
Rasterization yields uneven brightness: Horizontal and vertical lines appear brighter than the 45º lines.
7April 18, 2023
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 x12. Step along the pixels horizontally until you reach the right-hand endof the line, xr3. For each pixel compute the corresponding y value4. Round this value to the nearest integer to select the nearest pixel
8April 18, 2023
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-pointmultiplications, and is therefore more expensive
9April 18, 2023
Help for programming
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 18, 2023
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;}
yx x2 – x1 > y2 – y1
Slope (m) = xy
ifThen step on Column and find x
11April 18, 2023
Reverse the rolesof x and y usinga unit step in y,and 1/m for x.
Gaps occurwhen m > 1
Switching the roles of x and y when m>1
yx Step on the rows and find y
12April 18, 2023
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 18, 2023
17
16
15
14
13
12
11
10
18
292726252423222120 28 30
step x y
1 20 10
2 21 10.8
3 22 11.6
4 23 12.4
5 24 13.2
6 25 14
7 26 14.8
8 27 15.6
9 28 16.4
10 29 17.2
11 30 18
Dx = 30 – 20 = 10 Dy = 18 – 10 = 8 Larger length = 10 = Dx
Yinc = 8 / 10 = 0.8 Xinc = 10 / 10 = 1
14April 18, 2023
Implementation DDA algorithm by hand
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/2Error is given by:e=e+mReinitialize error:when e>0
15April 18, 2023
16April 18, 2023
To draw line with Bresenham’s algorithm follow the following steps
1. In each step change one of the axis in the value 12. 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. 5x
y
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 18, 2023
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 x
y
18April 18, 2023
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 18, 2023
Help for programming
Go through the steps of the Bresenham line drawing algorithm (by hand) for a line going from (21,12) to (29,16)
17
16
15
14
13
12
11
10
18
292726252423222120 28 30
step x y
20April 18, 2023
21April 18, 2023
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 = x
y
Multiply by Dx get 2 . Dx . H – Dx > 0
Suppose G = 2 . Dx . H – Dx
G > 022April 18, 2023
H will be
OR
Now how G change from Colum to another
If Hnew = Hold + M
If Hnew = Hold + M - 1
Key is to use G > 0 instead of use H > 0.5
23April 18, 2023
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 18, 2023
25April 18, 2023
Home work