Unit-1.2.1 CG Scan Conversioin
Unit-1.2.1 CG Scan Conversioin
3
Scan Converting A Pixel
Each pixel on graphic display does not represent a mathematical point like
P(2.6,3.33). But it can be accommodated to the nearest position by applying few
mathematical functions such as
CEIL P (3,4)
FLOOR P (2,3)
GREATEST INTEGER FUNCTION P (3,3) A pixel (10, 6)
ROUND P (3,3) X
Y
A point (-12.3,
10.3, 0)
X
Z
Y
Fig. 3.1 Coordinate axis [1] Fig. 3.2 Screen Coordinates [2]
4
Scan Converting A Line
5
The Problem of Scan Converting A
Line
A line segment is defined by the coordinate positions of the line
endpoints.
We have a line with the endpoints (2,2) and (8,7) and we want to draw
this line on a pixel based display.
Ystart x
Xstart Xend
Fig. 3.4 Representation of line end points
7
Slope-Intercept Line Equation
Line Equation is defined as: y = m x + b
Where m is the slope of the line, and defined as the change in y values divided
by the change in x values:
Yend – Ystart
m=
Xend – Xstart
b is the y-intercept. Recall that the y-intercept is the line’s y value when x
equals zero.
For example, the line defined by equation y=5x+3 the y-intercept is b=3.
8
Slope-Intercept Line Equation
continued..
The y-intercept can be calculated by the following equation in terms of the
coordinate of the starting points.
b = Ystart – m Xstart
The y-intercept can also be calculated by the following equation in terms of the
coordinate of the ending points.
b = Yend – m Xend
9
Slope-Intercept Line Equation
continued..
The slope of a line (m) is defined by its start and end coordinates. The diagram
below shows some examples of lines and their slopes, in all cases b=0.
10
Horizontal Line Drawing Algorithm
The screen coordinates of the points on a horizontal Line are obtained by
keeping the value of y constant and repeatedly incrementing the x value by one
unit.
11
Horizontal Line Drawing Algorithm
The following code can be used to draw a horizontal line from (Example)
(Xstart, Y) to (Xend, Y) Draw a Horizontal line between points (1,4) and (9,4)
where Xstart Xend
x = Xstart
y=Y 8 Fig. 3.7 Pixel
7 by pixel
Next: Set pixel (x, y) with desired color
6
x=x+1 5 display of
If x Xend then go to Next 4 Horizontal
3 1 2 3 4 5 6 7 8 9 line
End
2
Table 3.1 – Calculation of horizontal line pixel coordinates 1
S. No. x = x +1 y Pixel
1 1 4 (1, 4)
2 2 4 (2, 4)
3 3 4 (3, 4)
4 4 4 (4, 4)
5 5 4 (5, 4)
6 6 4 (6, 4)
7 7 4 (7, 4)
8 8 4 (8, 4)
9 9 4 (9, 4)
12
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.
13
Vertical Line Drawing Algorithm
The following code can be used to draw a vertical line from (X, Ystart) to
(Example)
(X, Yend), Draw a Horizontal line between points (3,1) and
where Ystart Yend (3, 9)
x=X 9
8
y = Ystart 7
Next: Set pixel (x, y) with desired color 6
5
y = y+ 1 4
If y Yend then go to Next 3
End 2
1
Table 3.2 – Calculations of vertical line pixel coordinates 1 2 3 4 5 6 7 8 9
S. No. x y=y+1 Pixel
Fig. 3.9 Pixel by pixel display of vertical line
1 3 1 (3, 1)
2 3 y = 1 + 1 =2 (2, 4)
3 3 y = 2 + 1 =3 (3, 4)
4 3 y = 3 + 1 =4 (4, 4)
5 3 y = 4 + 1 =5 (5, 4)
6 3 y = 5 + 1 =6 (6, 4)
7 3 y = 6 + 1 =7 (7, 4)
8 3 y = 7 + 1 =8 (8, 4)
9 3 y = 8 + 1 =9 (9, 4) 14
Diagonal Line Drawing Algorithm
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.
15
Diagonal Line Drawing Algorithm
(Example)
The following code can be used to draw a diagonal line
Draw a Horizontal line between points (2, 2) and (7, 7)
from (Xstart, Ystart) to (Xend, Yend), where Xstart
Xend and Ystart Yend
9
x = Xstart 8
y = Ystart 7
6
Next: Set pixel (x, y) with desired color 5
4
y = y + 1, x = x + 1 3
If x Xend then go to Next 2
1
End 1 2 3 4 5 6 7 8 9
Fig. 3.11 Pixel by pixel display of diagonal
line with m =1
S. No. x =x+1 y=y+1 Pixel
1 2 2 (2, 2)
2 x=2+1=3 y = 2 + 1 =3 (3. 3)
Table 3.3 – Calculations of diagonal line pixel coordinates
with m = 1 3 x=3+1=4 y = 3 + 1 =4 (4, 4)
4 x=4+1=5 y = 4 + 1 =5 (5, 5)
5 x=5+1=6 y = 5 + 1 =6 (6, 6)
6 x=7 y=7 (7, 7)
16
Diagonal Line Drawing Algorithm
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.
17
Diagonal Line Drawing Algorithm
The following code can be used to draw a diagonal line from (Example)
(Xstart, Ystart) to (Xend, Yend), where Xstart Xend and Ystart Draw a Horizontal line between points (2, 7) and
Yend
x = Xstart (7, 2)
y = Ystart
9
Next: Set pixel (x, y) with desired color 8
y=y– 1 7
x=x+1 6
5
If x Xend then go to Next 4
End 3
2
Table 3.4 – Calculations of diagonal line pixel coordinates 1
with m = -1 1 2 3 4 5 6 7 8 9
S. No. x =x+1 y=y-1 Pixel Fig. 3.13 Pixel by pixel display of diagonal
1 2 7 (2, 7) line with m = -1
2 x=2+1=3 y=7-1=6 (3. 6)
3 x=3+1=4 y=6-1=5 (4, 5)
4 x=4+1=5 y=5-1=4 (5, 4)
5 x=5+1=6 y = 4 - 1 =3 (6, 3)
6 x=7 y=2 (7, 2)
18
References
1. Computer Graphics, 2nd Ed., Hearn & Baker –PHI, New Delhi.
19
THANK YOU
20