L4-LineDrawingAlgorithm
L4-LineDrawingAlgorithm
LINE DRAWING
ALGORITHMS
Lecture 4 ITSW4111 Computer Graphics
Chapter Objectives
2
Identify:
Set of pixels (x, y) to display for segment
(x2, y2)
(x1, y1)
Line Rasterization
6
Requirements
Transform continuous primitive into
discrete samples
Uniform thickness and brightness
Continuous appearance
No gaps (x2, y2)
Accuracy
Speed
(x1, y1)
Line Drawing: The
7
Horizontal Line
The horizontal line is obtained by keeping
the value of y constant and repeatedly
incrementing the x value by one unit.
The following code draws a horizontal line
from
(xstart,y) to (xend,y), xstart <= xend.
For (x=xstar; x<= xend ; x++)do
Putpixel(x,y,8);
If xstart>xend, in the for loop you
must start from reserved order
(high to low)
Line Drawing: The
8
Vertical Line
The vertical line is obtained by keeping the
value of x constant and repeatedly
incrementing the y value by one unit.
The following code draws a vertical line
y
y mx x
m
Scan Converting A Line
11
y mx y
x
m
|m|<1 |m|>1
Line Drawing (cont)
12
(-x,y) (x,y)
450
(-x,-y) (x,-y)
(-y,-x) (y,-x)
Lines and Slopes
17
m = -1 m=1
m = -1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0
A Very Simple Solution
18
(7, 5)
5
2
(2, 2)
x
2 7
A Very Simple Solution
19
(cont…)
5
0 1 2 3 4 5 6 7
A Very Simple Solution (cont…)
20
y
(7, 5) First work
5 out m and b: 5 2 3
m
7 2 5
2 3 4
(2, 2)
b 2 2
5 5
x
2 3 4 5 6 7
Now for each x value work out the y value:
3 4 3 3 4 1
y (3) 3 2 y (4) 4 3
5 5 5 5 5 5
3 4 4 3 4 2
y (5) 5 3 y (6) 6 4
5 5 5 5 5 5
A Very Simple Solution (cont…)
21
multiplication of m by x
We need to round off the resulting y
coordinates
Thus, we need a faster solution
A Quick Note About Slopes
23
m = -1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0
26
Digital Differential
Analyzer
(DDA Algorithm)
DDA Algorithm
27
y K 1
y m
k
DDA Algorithm
28
1
if y 1, y m, xK 1 xk
m
DDA Pseudo-code
29
}
DDA Example
30
numsteps = 12 – 2 = 10
Suppose we want to xinc = 10/10 = 1.0
yinc = 5/10 = 0.5
draw a line starting
at pixel (2,3) and t x y R(x) R(y)
0 2 3 2 3
ending at pixel
1 3 3.5 3 4
(12,8).
2 4 4 4 4
What are the values 3 5 4.5 5 5
of the variables x 4 6 5 6 5
and y at each 5 7 5.5 7 6
timestep? 6 8 6 8 6
What are the pixels 7 9 6.5 9 7
colored, according 8 10 7 10 7
algorithm? 10 12 8 12 8
DDA Algorithm
31