Transcript line

The lines of this object
appear continuous
April 7, 2015
However, they are
made of pixels
2
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.
April 7, 2015
3
• 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
April 7, 2015
4
For horizontal, vertical and 45º lines, the choice of raster
elements is obvious. This lines exhibit constant brightness
along the length:
April 7, 2015
5
For any other orientation the choice is more difficult:
April 7, 2015
6
Rasterization of straight lines.
Rasterization yields uneven brightness: Horizontal and vertical lines appear
brighter than the 45º lines.
April 7, 2015
7
The equation of a straight line is given by:
Algorithm 1:
y=m.x+b
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
April 7, 2015
8
Help for programming
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
April 7, 2015
9
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).
April 7, 2015
10
DDA algorithm for lines with -1 < m < 1 ( No. of Colum more than number of rows)
Slope (m) =
if
y
x
x  y
x2 – x1 > y2 – y1
Then step on Column and find x
x = x1;
y = y1;
while (x <= xr){
y = y + m;
y = Round (y);
PlotPixel (x, y);
x = x + 1;
}
April 7, 2015
11
Switching the roles of x and y when m>1
x  y
Gaps occur
when m > 1
April 7, 2015
Step on the rows and find y
Reverse the roles
of x and y using
a unit step in y,
and 1/m for x.
12
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 ;
}
April 7, 2015
13
Implementation DDA algorithm by hand
Dx = 30 – 20 = 10
Yinc = 8 / 10 = 0.8
Dy = 18 – 10 = 8
Larger length = 10 = Dx
Xinc = 10 / 10 = 1
step
x
y
1
20
10
17
2
21
10.8
16
3
22
11.6
15
4
23
12.4
14
5
24
13.2
6
25
14
7
26
14.8
8
27
15.6
11
9
28
16.4
10
10
29
17.2
11
30
18
18
13
12
20 21 22 23 24 25 26 27 28 29 30
April 7, 2015
14
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
April 7, 2015
15
April 7, 2015
16
To draw line with Bresenham’s algorithm follow the following steps
1. In each step change one of the axis in the value
1
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=
y
x
- 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)
April 7, 2015
17
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
April 7, 2015
y
x
18
However, this algorithm does not lead to integer arithmetic. Scaling by: 2* dx
Help for programming
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 */
}
}
April 7, 2015
19
Go through the steps of the Bresenham line drawing algorithm (by
hand) for a line going from (21,12) to (29,16)
step
18
x
y
17
16
15
14
13
12
11
10
20 21 22
April 7, 2015
23 24
25 26 27 28 29 30
20
April 7, 2015
21
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 will be
H=H+M-1
H > 0.5
Multiply by 2 get
Multiply by Dx get
Suppose
April 7, 2015
x
H = H +M
OR
Rewrite
y
H- 0.5 > 0
2H – 1 > 0
2 . Dx . H – Dx > 0
G = 2 . Dx . H – Dx

G>0
22
Now how G change from Colum to another
If
If
Hnew = Hold + M
Hnew = Hold + M - 1
Key is to use G > 0 instead of use H > 0.5
April 7, 2015
23
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
April 7, 2015
G
24
Home work
April 7, 2015
25