L6
L6
Graphics output
primitive
Primitive graphic objects
x = Xstart
y=Y
Next: Set pixel (x, y) with desired color
x=x+1
If x Xend then go to Next
End
Vertical Line Drawing
Algorithm
The screen coordinates of the points on a
Vertical Line are obtained by keeping the
value of x constant and repeatedly
incrementing the y value by one unit.
x=X
y = Ystart
Next: Set pixel (x, y) with desired color
y = y+ 1
If y Yend then go to Next
End
Diagonal Line Drawing Algorithm
(m=1)
To draw a diagonal line with a slope equals
+1 (m=1), we need only repeatedly
increment by one unit both the x and y
values from the starting to the ending pixels.
a diagonal line
(m=1) with starting
point (2,2) and
ending point (7,7)
on a pixel based
display
14
Diagonal Line Drawing Algorithm
(m=1)The following code can be used to draw a diagonal
line from (Xstart, Ystart) to (Xend, Yend), where
Xstart Xend and Ystart Yend
x = Xstart
y = Ystart
Next: Set pixel (x, y) with desired color
y = y+ 1
x=x+1
If x Xend then go to Next
End 15
Diagonal Line Drawing Algorithm
(m=-1)
To draw a diagonal line with a slope equals –1
(m=-1), we need only repeatedly increment
by one unit the x and decrementing by one
unit the y values from the starting to the
ending pixels.
a diagonal line (m=-
1) with starting
point (2,7) and
ending point (7,2)
on a pixel based
display.
16
Diagonal Line Drawing Algorithm
(m=-1)
The following code can be used to draw a
diagonal line from (Xstart, Ystart) to (Xend,
Yend), where Xstart Xend and Ystart
Yend
x = Xstart
y = Ystart
Next: Set pixel (x, y) with desired color
y=y– 1
x=x+1
If x Xend then go to Next
17
End
Arbitrary Lines Drawing
Algorithm
Drawing lines with arbitrary slope creates
several problems.
Direct Line Drawing Algorithm
Simple Digital Differential Analyzer
(simple
DDA) Line Drawing Algorithm
Bresenham ’s Line Drawing Algorithm
18
Direct Line Drawing
Algorithm
Perhaps the most natural method of generating a
straight line is to use its equation. First we calculate
the slope (m) and the y-intercept (b) using these
equations:
Yend –
m Ystart
= Xend –
b = Ystart – m Xstart or b = Yend – m Xend
Xstart
We then draw the line by incrementing the x value
one unit from (Xstart, Ystart) to (Xend, Yend) and at
each step solve for the corresponding y value. For
non-integer y value, we must first determine the
nearest integer coordinate. 19
Direct Line Drawing
Algorithm
The following code can be used to draw a line
from (Xstart, Ystart) to (Xend, Yend), where
Xstart Xend
x = Xstart
y = Ystart
m= (Yend-Ystart)/(Xend-Xstart)
b= Ystart – m Xstart
Next: Set pixel (x, Round(y)) with desired color
x= x + 1
y = mx + b
If x Xend then go to Next
20
End
Direct Line Drawing
Algorithm
Note that: the Round function is used to
obtain an integer coordinate value.
There are two fundamental problems with
this method:
The first problem is the computational time
required to draw the line. On many computer
systems the operations of multiplication and
division can take from 50 to 200 times longer
to perform than an addition.
21
Direct Line Drawing
Algorithm
a division operation used to calculate the slop
(m)
a multiplication operation used to calculate b
There are (Xend-Xstart+1) multiplication
operations used to calculate the y value
The second problem concerns lines with a slop
whose absolute value is greater than 1 (m > 1).
These lines will be near the vertical. Using the
direct method, the displayed lines will have gaps
between plotted points. 22
Direct Line Drawing
Algorithm
The following example illustrates this problem. We
will use the direct method to draw a line with
starting point (1,1) and ending point (3,6) on a pixel
based display.
Yend – 6–1 5
Ystart
m= = =
Xend – 3–1 2
Xstart
23
Direct Line Drawing
Algorithm
Round(
x y
y)
1 1 1
(5/2)*2-(3/2) =
2 4
7/2=3.5
(5/2)*3-(3/2) = 12/2
3 6
=6
24
Direct Line Drawing
Algorithm
There are several methods of correcting both
problems.
Performing incremental calculations at each
step based on the previous step can eliminate
repeated multiplication.
The second problem can be resolved by
incrementing y by one unit and solving the
straight line equation for x when absolute value of
the slope is greater than 1 (abs(m) > 1).
25
Simple (DDA) Line Drawing Algorithm
28
Simple DDA Line Drawing
Algorithm
Equation 2 enables us to calculate successive
values of y from the previous value by replacing
the repeated multiplication with floating point
addition.
This method of obtaining the current value by
adding a constant to the previous value is an
example of incremental calculation. Using
knowledge of one point to compute the next is a
great time saving technique.
The following code can be used to draw a line from
(Xstart, Ystart) to (Xend, Yend) using simple DDA29
Simple DDA Line Drawing Algorithm
m = (Yend-Ystart) / (Xend-Xstart)
If (abs(m)<1 and Xstart>Xend) then
Swap endpoints Xstart Xend and Ystart Yend
end if
Set pixel (Xstart, Ystart) with desired color
If abs(m) < 1 then
y = Ystart
x = Xstart + 1
Next: y = y + m
Set pixel (x, Round(y)) with desired color
x=x+1
If x Xend-1 then go to Next
endif
Set pixel (Xend, Yend) with desired color 30
Simple DDA Line Drawing
Algorithm
We will use the simple DDA algorithm to draw a line with
starting point (2,0) and ending point (7,4) on a pixel based
display. Firstly, we compute the slope m: m
=(Yend–Ystart)/(Xend–Xstart)=(4–0)/(7–2)=4/5 = 0.8
y = Ystart = 0 x = Xstart + 1 = 2 + 1 =3
x y Round(y)
2 0
y=y+m=0 +
3 1
0.8=0.8
y = y + m = 0.8 +
4 2
0.8=1.6
y = y + m = 1.6 +
5 2
0.8=2.4 31
Simple DDA Line Drawing Algorithm
32
Simple DDA Line Drawing
Algorithm
Case 2: For abs(m) > 1 and Ystart < Yend,
we generate the line by reversing the above
procedure. Namely, we increment the y value
one unit until Yend is reached and then solve
for x. if Ystart > Yend, swap the two
endpoints. For these consecutive points:
y2 = y1 + 1 or y2 – y1 = 1
Substituting this difference into equation 1
yields:
1/(x2 – x1) = m or x2 = x1 + 1/m 33
Simple DDA Line Drawing
Algorithm
34
Simple DDA Line Drawing Algorithm
m = (Yend-Ystart) / (Xend-Xstart)
If (abs(m)>1 and Ystart>Yend) then
Swap endpoints Xstart Xend and Ystart Yend
end if
Set pixel (Xstart, Ystart) with desired color
If abs(m) > 1 then m = 1/m , y = Ystart + 1
x = Xstart
Next: x= x + m
Set pixel (Round(x), y) with desired color
y=y+1
If y Yend-1 then go to Next
endif
Set pixel (Xend, Yend) with desired color 35
Simple DDA Line Drawing
Algorithm
We will use the simple DDA algorithm to draw a line with
starting point (2,2) and ending point (6,7) on a pixel based
display. Firstly, we compute the slope m: m
=(Yend–Ystart)/(Xend–Xstart)=(7–2)/(6–2)=5/4
m=1/m = 0.8, y = Ystart + 1 = 2 + 1 = 3 , x= Xstart =2
y x Round(x)
2 2
x=x+m=2 +
3 3
0.8=2.8
x = x + m = 2.8 +
4 4
0.8=3.6
x = x + m = 3.6 +
5 4
0.8=4.4 36
Simple DDA Line Drawing Algorithm
37
Bresenham ’s Line Drawing Algorithm
40
Bresenham’s Line Algorithm
41
Bresenham’s Line Algorithm
43
Bresenham’s Line Algorithm
44
Bresenham’s Line Algorithm
• At sampling position xk + 1, we label vertical pixel
separations from the mathematical line path as dlower and
dupper (Figure 7).
• The y coordinate on the mathematical line at pixel column
position xk + 1 is calculated as
y = m(xk + 1) + b (10)
Then
dlower = y − yk
= m(xk + 1) + b − yk (11)
and
dupper = (yk + 1) − y
= yk + 1 − m(xk + 1) − b (12)
45
Bresenham’s Line Algorithm
46
Bresenham’s Line Algorithm
47
Bresenham’s Line Algorithm
48
Bresenham’s Line Algorithm
49
Bresenham’s Line Algorithm
50
Bresenham’s Line Algorithm
51
Bresenham’s Line Algorithm
52
Bresenham’s Line Algorithm
53
Bresenham’s Line Algorithm
54
Bresenham’s Line Algorithm
55
Bresenham’s Line Algorithm
56
Bresenham’s Line Algorithm
57
Bresenham’s Line Algorithm
58
Comparation
59
Displaying Polylines