Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 48

Chapter 3

Scan Conversion Algorithms


(Point and Line)
Four Major Tasks
• Four major task for rendering geometric objects
– modeling
– geometric processing Modeling
• transformations
• clipping Geometry
• shading
• hidden-surface removal
Rasterize
• projection
– Rasterization (Scan Conversion)
Display
– display

January 19, 2021 Computer Graphics 2


Scan Conversion
• Rasterization or Scan Conversion is defined as the
process of representing continuous graphic object as a
collection of discrete pixels.
– Various graphic objects are
• Point
• Line
• Rectangle, Square
• Circle, Ellipse
• Sector, Arc
• Polygons……
– Hardware, Software, Firmware Implementation

January 19, 2021 Computer Graphics 3


Scan Conversion
• Scan Conversion is required in order to convert vector
data to Raster format for a scan line display device
– convert each graphic object to a set of regular pixels.
– determine inside/outside areas when filling polygons.
– scan-convert curves

January 19, 2021 Computer Graphics 4


Scan Conversion
• The scan conversion process is interleaved with other
processes to deliver and improve the final image, some of
which are not entirely restricted to the discretization of vectors
but are involved in generally determining the colour of each
pixel in the raster. e.g:
– shading & illumination,
– hidden surface removal
– texture mapping
– depth testing

January 19, 2021 Computer Graphics 5


Scan Conversion Algorithms

1. Scan Conversion of Point


2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse
5. Scan Conversion of Polygons

January 19, 2021 Computer Graphics 6


Scan Converting a Point
A pixel (10, 6)
Y
X
A point (-12.3,
10.3, 0)

Z
Y

MODELLING CO-ORDINATES SCREEN COORDINATES


• Mathematically vectors are defined in an • a.k.a device co-ordinates, pixel co-ordinates
infinite, “real-number” Cartesian co- • On display hardware we deal with finite, discrete
ordinate system coordinates
• X, Y values in positive integers
• 0,0 is measured from top-left usually with +Y
pointing down
January 19, 2021 Computer Graphics 7
Scan Converting a Point
• 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)
– ROUND P  (3,3)

January 19, 2021 Computer Graphics 8


Scan Conversion Algorithms

1. Scan Conversion of Point


2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse

January 19, 2021 Computer Graphics 9


Scan Converting a Line
How does a machine draw Lines?
1. Give it a start and end position.
2. Figure out which pixels to colour in between these…
– How do we do this?
– Line-Drawing Algorithms: DDA, Bresenham’s Algorithm

January 19, 2021 Computer Graphics 10


Scan Converting a Line
Line and its slope
– 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

m=∞
m = -4 m=4
m = -2 m=2

m = -1 m=1

m = - 1 /2 m = 1/2
m = -1/3 m = 1/3

January 19, 2021 Computer Graphics 11


m=0 m=0
Line Drawing Algorithms

1. DDA Line Algorithm


2. Bresenham Line Algorithm

January 19, 2021 Computer Graphics 12


DDA Algorithm
1. Introduction
– Digital Differential Analyser is an
an algorithm for scan-converting lines
– The original differential analyzer
was a physical machine developed by
Vannevar Bush at MIT in the 1930’s
in order to solve ordinary differential equations.
– It calculates pixel positions along a line by taking unit step
increment along one direction and calculating corresponding
coordinate position based on the rate of change of the
coordinate (x or y ) (Incremental Approach)
January 19, 2021 Computer Graphics 13
DDA Algorithm
2. Basic Concept
– For each part of the line the following holds true:
y
m  y  mx
x

– If x = 1 i.e. 1 pixel then … y  m


– i.e. for each pixel we move right (along the x axis), we need to
move down (along the y-axis) by m pixels.
– In pixels, the gradient represents how many pixels we step
upwards (y) for every step to the right (x)

January 19, 2021 Computer Graphics 14


DDA Algorithm
3. Derivation
Assume that 0<m<1, x > 0 and y >0
For a point P(xi, yi ) on a line we know that
yi = mxi + b
At next position P(xi+1, yi+1 )
yi+1 = mxi+1+ b
Having unit step increment along x-axis means xi+1 = xi + 1
Therefore yi+1 = m(xi + 1)+ b
= mxi + m + b
= mxi + b + m
= yi + m
January 19, 2021 Computer Graphics 15
DDA Algorithm
4. Simple Algorithm
1. Input (x1,y1) and (x2,y2)
2. Let x = x1; y = y1;
m = (y2-y1)/(x2-x1);
3. Draw pixel (x, y)
4. WHILE (x < x2) //i.e. we reached the second
endpoint
5. {
x = x + 1; //step right by one pixel
y = y + m; //step down by m pixels
Draw pixel (ROUND(x), ROUND(y));
}

January 19, 2021 Computer Graphics 16


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)

January 19, 2021 Computer Graphics 17


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)

January 19, 2021 Computer Graphics 18


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)

January 19, 2021 Computer Graphics 19


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)

January 19, 2021 Computer Graphics 20


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)

January 19, 2021 Computer Graphics 21


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)

January 19, 2021 Computer Graphics 22


DDA ALGORITHM
5. Example
Sample at unit x:
xk 1  xk  x
 xk  1
Corresponding y pos.:
y k 1  yk  y
 y k  m  x
 y k  m  (1)
Consider endpoints:
P1(0,0), P2(7,4)

January 19, 2021 Computer Graphics 23


DDA ALGORITHM
6. Exercise
1. Consider endpoints:
P1(0,0), P2(6, 4)
Calculate the points that made up the line P1P2
2. Now, consider endpoints:
P3(0,0), P4(4, 6)
Calculate the points that made up the line P3P4

What happened with P3P4??????

January 19, 2021 Computer Graphics 24


DDA Algorithm
7. Y-Version (|m|>1)

– If the slope is very steep (i.e. y is


much greater than x) then there’s
a problem. i.e. m is much greater
than 1

– This is because we only draw one


pixel for each x value (each time
we step right)

– Solution: if the slope is too big, step


for y instead
January 19, 2021 Computer Graphics 25
DDA Algorithm
8. Generic solution
– Modify Basic idea y  mx m0
1
x  y
m

– Take unit step increment along direction having more rate of change, and
compute the coordinates for the other position to maximize the number of
points to be computed along the path of line and avoid discontinuities
xi  1 : xi  1
yi  1 : yi  m 0  m 1

or Why?
yi  1 : yi  1
1
xi  1 : xi  1 m
m
January 19, 2021 Computer Graphics discontinuity !! 26
DDA Algorithm - Generic solution
– What is m<0 ?
– We must see the sign of x and y
Various x>0 x>0 x<0 x<0
Cases y>0 y<0 y>0 y<0
xi+1 = xi + 1 xi+1 = xi + 1 xi+1 = xi - 1 xi+1 = xi - 1
|m|<1 yi+1 = yi + m yi+1 = yi - m yi+1 = yi + m yi+1 = yi – m
(or swap
points)
xi+1 = xi + 1/m xi+1 = xi + 1/m xi+1 = xi - 1/m xi+1 = xi - 1/m
|m|>1 yi+1 = yi + 1 yi+1 = yi - 1 yi+1 = yi + 1 yi+1 = yi – 1
(or swap
– Or can we have simple solution??????? points)
January 19, 2021 Computer Graphics 27
DDA Algorithm
1. Input (x1,y1) and (x2,y2)
2. Compute
dx = x2-x1 and dy = y2-y1
m = dx/dy
3. If abs(dx)>abs(dy)
step = abs(dx)
else
step = abs(dy)
4. Compute xinc = dx/step
yinc = dy/step
5. Initialize x = x1 and y = y1
6. Draw pixel (x, y)
7. For k = 1 to steps do //we reach the other endpoint
{
x = x + xinc;
y = y + yinc;
Draw pixel (ROUND(x), ROUND(y));
}
January 19, 2021 Computer Graphics 28
DDA Algorithm
8. Limitations
– Rounding integers takes time

– Variables y and m must be a real or fractional binary because


the slope is a fraction

– Real variables have limited precision, summing an inexact


slope m repetitively introduces a cumulative error buildup

January 19, 2021 Computer Graphics 29


DDA Algorithm
X Y Rounded { x, y }
Rounding Error
– Note that the actual pixel position is 1.0 0.33 { 1, 0 }
actually stored as a REAL number
2.0 0.66 { 2, 0 }
(in C/C++/java a float or a double)
3.0 0.99 { 3, 1 }
– But we Round Off to the nearest
4.0 1.32 { 4, 1 }
whole number just before we draw
the pixel.

– e.g. if m=.333 …

January 19, 2021 Computer Graphics 30


Line Drawing Algorithms

1. DDA Line Algorithm


2. Bresenham Line Algorithm

January 19, 2021 Computer Graphics 31


Bresenham’s Line Algorithm
1. Introduction
– One disadvantage of DDA is the ROUNDing part which can
be expensive
– Developed by Jack Bresenham
at IBM in the early 1960s
– One of the earliest algorithms in
computer graphics
– The Algorithm is based on essentially
the same principles but is completely
based on integer variables

January 19, 2021 Computer Graphics 32


Bresenham’s Line Algorithm
2. Basic Concept
– Find the closest integer coordinates to the actual line path
using only integer arithmetic
– Candidate for the next pixel position

Specified
Line Path
0  m 1
Specified
Line Path

– No division, Efficient comparison, No floating point operations


January 19, 2021 Computer Graphics 33
Bresenham’s Line Algorithm
3. Derivation
– The algorithm is derived for a line having slope 0< m < 1
in the first quadrant.
– Pixel position along the line are plotted by taking unit step
increments along the x-direction and determining y-
coordinate value of the nearest pixel to the line at each
step.
– Can be generalized for all the cases

January 19, 2021 Computer Graphics 34


Bresenham’s Line Algorithm
– Let us assume that P(xk, yk ) is the currently plotted pixel.
Q(xk+1, yk+1 ) (xk+1, y ) is the next point along the actual path
of line. We need to decide next pixel to be plotted from two
candidate positions Q1(xk+1, yk ) or Q2(xk+1, yk+1)

Q2
yk  1 d2
yk  2 y Q

yk  1 Q2 d1
Q
y  mx  b yk Q1
yk P Q1 P

xk  1
xk xk  1 xk  2
0  m  1, xk  xl , k  l

January 19, 2021 Computer Graphics 35


Bresenham’s Line Algorithm
Given the equation of line
y = mx + b
Thus actual value of y at x = xk+1 is given by
y = mxk+1 + b = m(xk + 1) + b
Let d1 = | QQ1|= distance of yk from actual value of y
= y – yk = m(xk + 1) + b – yk
d2 = | QQ2| = distance of actual value of y from yk +1
= yk+1 – y = (yk + 1)– [m(xk + 1) + b]
The difference between these 2 separations is
d1-d2 = 2m(xk + 1) + 2b –yk– (yk+ 1)
= 2m(xk + 1) – 2 yk + 2b – 1
January 19, 2021 Computer Graphics 36
Bresenham’s Line Algorithm
we can define a decision parameter pk for the kth step to by
simplifying above equation such that the sign of Pk is the same as
the sign of d1-d2, but involves only integer calculations.
Define Pk = Δx ( d1-d2)
 x(2m( xk  1)  2 yk  2b  1)
y
 x(2 ( xk  1)  2 yk  2b  1)
x
 2y  xk  2x  yk  2y  x(2b  1)
 2 y  x k  2 x  y k  c
where c  2 Δy  Δx( 2 b  1 ) a constant

January 19, 2021 Computer Graphics 37


Bresenham’s Line Algorithm
If pk< 0
 ( d1-d2) <0
 distance d1 is less than d2
 yk is closer to line-path
Hence Q1(xk+1, yk ) is the better choice
else
Q2(xk+1, yk+1) is the better choice

Thus if the parameter pk is negative lower pixel is plotted else


upper pixel is plotted
January 19, 2021 Computer Graphics 38
Bresenham’s Line Algorithm
To put pk in the iterative form, we derived that
pk  2y.xk  2x. yk  c
Replacing k  k  1
pk  1  2y.xk  1  2x. yk  1  c
subtract pk from pk  1
pk  1  pk  2y ( xk  1  xk )  2x ( yk  1  yk )
pk  1  pk  2y  2x ( yk  1  yk )
 yk if pk  0
yk  1  
 yk  1 otherwise
 pk  2y if pk  0
 pk  1  
 pk  2y  2x otherwise
January 19, 2021 Computer Graphics 39
Bresenham’s Line Algorithm
The first parameter p0 is directly computed as:
p0 = 2 Δy.x0 - 2 Δx.y0 + c
= 2 Δy.x0 - 2 Δx.y0 + 2 Δy + Δx (2b-1)

Since (x0,y0) satisfies the line equation , we also have


y0 = Δy/ Δx * x0 + b
b = y0 - Δy/ Δx * x0

Combining the above 2 equations , we will have


p0 = 2Δy – Δx
The constants 2Δy, 2Δy – Δx and 2Δy-2Δx are calculated once.
January 19, 2021 Computer Graphics 40
Bresenham’s Line Algorithm
STEPS FOR BRESENHAM’S LINE DRAWING ALGORITHM (for |m| < 1.0)
1. Input the two line end-points (x0, y0) and (x1, y1)
2. Plot the point (x0, y0)
3. Compute Δx = x1 - x0 , Δy = y1 - y0
4. Initialize p0 = 2Δy – Δx
5. At each xk along the line, starting at k = 0, perform the
following test.
If pk < 0
the next point to plot is (xk+1, yk)
pk = pk +2Δy
else
the next point to plot is (xk+1, yk+1)
pk = pk +2Δy – 2Δx
6. Repeat step 5 (Δx – 1) times
7. Plot the point (x1, y1)
8. Exit
January 19, 2021 Computer Graphics 41
January 19, 2021 Computer Graphics 42
Bresenham’s Line Algorithm
4. General solution
– The algorithm can be generalized for all slopes
Cases/ CaseI CaseII Case III Case IV
slope x>0 , y>0 x>0, y<0 x<0, y>0 x<0, y<0
p = 2dy-dx p = 2dy-dx swap points swap points
|m|<1 for x = x1 to x2 for x = x1 to x2
Case II Case I
if p<0 if p<0
p = p+ 2dy p = p+ 2dy
else { y=y+1 else { y=y-1
p = p+2(dy-dx)} p = p+2(dy-dx)}
p = 2dx-dy p = 2dx-dy swap points swap points
for y= y1 to y2 for y= y2 to y1
|m|>1 Case II Case I
if p<0 if p<0
p = p+ 2dx p = p+ 2dx
else { x=x+1 else { x=x+1
p = p+2(dx-dy)} p = p+2(dx-dy)}
January 19, 2021 Computer Graphics 43
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
1. (x0, y0) = ?
2. x = ?, y =?, 2y = ?, 2y – 2x =?
3. p0 = 2y – x =?

k pk (xk+1, yk+1)

January 19, 2021 Computer Graphics 44


Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
1. (x0, y0) = (12,10)
2. x = 5, y =4, 2y = 8, 2y – 2x =-2
3. p0 = 2y – x =3

k pk (xk+1, yk+1)
0 3

January 19, 2021 Computer Graphics 45


Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
1. (x0, y0) = (12,10)
2. x = 5, y =4, 2y = 8, 2y – 2x =-2
3. p0 = 2y – x =3
k pk (xk+1, yk+1)
0 3 (13, 11)

1 1 (14, 12)

2 -1 (15, 12)

3 7 (16, 13)

4 5 (17, 14)
January 19, 2021 Computer Graphics 46
Bresenham’s Line Algorithm
5. Exercise: Trace for (20,10) to (30,18)
k pk (xk+1,yk+1)

18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30
January 19, 2021 Computer Graphics 47
Bresenham’s Line Algorithm
Answer k pk (xk+1,yk+1)
18
0 6 (21,11)
17 1 2 (22,12)
16 2 -2 (23,12)
15 3 14 (24,13)
14 4 10 (25,14)
13 5 6 (26,15)
12 6 2 (27,16)
11 7 -2 (28,16)
10 8 14 (29,17)
20 21 22 23 24 25 26 27 28 29 30 9 10 (30,18)
January 19, 2021 Computer Graphics 48

You might also like