Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Scan Conversion

Line
Dr. Nahla Abid
Scan Conversion

• Scan conversion: the process of representing a continuous graphical objects as a


collection of discrete pixels by identifying their locations and setting them On
• The process of determining which pixels will provide the best approximation to
the desired line is properly known as rasterization.
• Combined with the process of rendering the picture in scan line order it is
known as scan conversion.
Points and Line

• Points:
• A point in two dimensional space is given as an ordered pair (x, y)
• In three dimensions a point is given as an ordered triple (x, y, z)
• Lines: A line is defined using a start point and an end-point
• In 2d: (xstart, ystart) to (xend, yend)
• In 3d: (xstart, ystart , zstart) to (xend, yend , zend)
Line Drawing
y

• Given the specification for a


straight line, find the collection of (2, 7) (6, 7)
addressable pixels which most
closely approximate this line.
The line from
(2, 7) to (7, 3)
(2, 3) (7, 3)

(7, 1)

x
Goals

1. Straight lines should appear straight.


2. Lines should start and end accurately matching endpoints.
3. Lines should have constant brightness
4. The complexity of the algorithm should be minimal
Simple Straight Line Equation

• The slope-intercept Equation of a line is: y  m x b


• where:
y y1  y0
m 
x x1  x0
b  y0  m  x0

• The equation of the line gives us the corresponding y point for every x point
Example of Using Line Equation
3 4
Let’s draw a portion of the line given by the equation: y  x
5 5

3 4 3 4 4
y (2)   2   2 y (5)   5   3 y
5 5 5 5 5 5
3 4 3 3 4 2
y (3)   3   2 y (6)   6   4
5 5 5 5 5 5 2
3 4 1 3 4
y (4)   4   3 y (7 )   7   5
5 5 5 5 5 2 3 4 5 6 7 x
Digital Differential Analyser ( DDA) for a line

Let endpoints coordinates are (x1,y1 ) and (x2,y2) respectively , so we have

y y2  y1
m 
x x2  x1

1
y  mx or x  y
m
Digital Differential Analyser ( DDA) for a line…

• A very simple procedure for scan converting a straight line is given as follows:
1. Plot the first pixel at (x,y)= (x1,y1 )
2. increment the x- and y-values by dx and dy respectively to get the next pixel . We
consider either dx or dy to be unity and compute the other . Plot pixel at
(x+dx,y+dy)
if dx=1 yi 1  yi  m
1
else dy=1 xi 1  xi 
m
3. Continue the above steps until the final (x2,y2) is reached
DDA Algorithm for a line (x1,y1,x2,y2)

Begin for k=1 to steps do


dx=x2-x1, dy=y2-y1; begin x=x+Xinc; y=y+Yinc;
if |dx|>|dy| then setpixel (round(x),round(y));
steps=|dx| end
else steps=|dy| end /*of DDA/
Xinc=dx/steps
Yinc=dy/steps
x=x1; y=y1;
DDA: Example1

• Trace the DDA algorithm for drawing a line segment from (2,3) to (8,7)
• solution :

dx = 8 – 2 = 6
dy = 7 – 3 = 4
|dx|>|dy| then steps=|dx|= 6
Xinc = dx/steps = 1
Yinc = dy/steps = 0.67

• Now starting with the point (2,3) ,generate the next points in sequence to reach
the point (8,7). the computational steps are shown in the next slide.
DDA: Example1…

Xold Yold Xnew Ynew Round(x) Round(y)


DDA: Example1…

Xold Yold Xnew Ynew Round(x) Round(y)


2 3 3 3.67 3 4
3 3.67 4 4.34 4 4
4 4.34 5 5.01 5 5
5 5.01 6 5.68 6 6
6 5.68 7 6.35 7 6
7 6.35 8 7.02 8 7
DDA: Example2

• Trace the DDA algorithm for drawing a line segment from (2,3) to (8,10)
• solution:

dx = 8 – 2 = 6
dy = 10 – 3 = 7
|dy|>|dx| then steps=|dy|=7
Xinc = dx/steps = 0.86
Yinc = dy/steps = 1
• Now starting with the point (2,3) ,generate the next points in sequence to reach
the point (8,10). the computational steps are shown in the next slide.
DDA: Example2…

Xold Yold Xnew Ynew Round(x) Round(y)


DDA: Example2…

Xold Yold Xnew Ynew Round(x) Round(y)


2 3 2.86 4 3 4
2.86 4 3.72 5 4 5
3.72 5 4.58 6 5 6
4.58 6 5.44 7 5 7
5.44 7 6.3 8 6 8
6.3 8 7.16 9 7 9
7.16 9 8.02 10 8 10
Line Drawing Algorithm Drawbacks

• DDA is the simplest line drawing algorithm but


• Not very efficient
• Round operation is expensive
• Optimized algorithms typically used.
• Integer DDA
• E.g.Bresenham’s algorithm
Bresenham’s Algorithm

• Assumptions:
• Drawing a line from the left hand side
• X2 > X1 and |dx| > |dy|
Big Idea

• Move across the x axis in unit intervals and at each step choose
between two different y coordinates
5
• For example, from position (2,
(xk+1, yk+1)
3) we have to choose between
4
(3, 3) and (3, 4)
(xk, yk)
• We would like the point that is 3
closer to the original line (xk+1, yk)
2

2 3 4 5
Bresenham’s Algorithm

1.Uses only integer operations and does not use multiplication or


division.
2. Algorithm always increments by one unit in either X or Y depending
on the slope of the line.
3. The increment in the other variable, either zero or one, is
determined by examining the distance (error) between the actual line
location and the nearest grid locations.
Bresenham’s Algorithm

• At sample position x+1 the vertical separations from the


k
mathematical line are labelled dupper and dlower
• The y coordinate on the mathematical line at xk+1 is
y  m( xk  1)  b yk+1
dupper
y
dlower
yk

xk+1
5

Bresenham’s Algorithm… (xk+1, yk+1)


4
(xk, yk)
• So, dupper and dlower are given as follows: 3
(xk+1, yk)
d lower  y  yk d upper  ( yk  1)  y
2
 m( xk  1)  b  yk  yk  1  m( xk  1)  b
2 3 4 5

• We can use these to make a simple decision about which pixel is


closer to the mathematical line

d lower  d upper  2m( xk  1)  2 yk  2b  1


Bresenham’s Algorithm…

• Let’s substitute m with ∆y/∆x where ∆x and ∆y are the differences


between the end-points:
y
x( d lower  d upper )  x(2 ( xk  1)  2 yk  2b  1)
x
 2y  xk  2x  yk  2y  x(2b  1)
 2y  xk  2x  yk  c
• So, a decision parameter pk for the kth step along a line is given by:
pk  x(d lower  d upper )
 2y  xk  2x  yk  c
Bresenham’s Algorithm…
pk  x(d lower  d upper )
• At step k+1 the decision parameter is given as:  2y  xk  2x  yk  c
pk 1  2y  xk 1  2x  yk 1  c
pk 1  pk  2y ( xk 1  xk )  2x( yk 1  yk ) Subtracting pk

• But, xk+1 is the same as xk+1 so:


pk 1  pk  2y  2x( yk 1  yk )
• where yk+1 - yk is either 0 or 1 depending on the sign of pk
• The first decision parameter p0 is evaluated at (x0, y0) is given as:
p0  2y  x
Bresenham’s Line Drawing Algorithm (for |m| < 1.0)

1. Input the two line end-points, storing the left end-point in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for
the decision parameter as:
p0  2y  x
4. 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) and: pk 1  pk  2y
Otherwise, the next point to plot is (xk+1, yk+1) and: pk 1  pk  2y  2x

5. Repeat step 4 (Δx ) times


pk 1  pk  2y  2x( yk 1  yk )
For k=1 to dx do
Bresenham Algorithm Begin
If(p<0) then
Begin
Begin
If (x<xf) then x=x+1;
Else x=x-1; p=p+2dy;
End
Else
Begin if(y<yf) then y=y+1
else y=y-1
If(slope>1)
If (x<xf) then x=x+1;
Begin
else x=x-1; p=p+2(dy-
temp=dx;dx=dy;dy=temp
dx);
temp=x;x=y;y=temp
End
temp=xf;xf=yf;yf=temp
If (slope>1) then setpixel(y,x)
End
Else setpixel(x,y)
P=2dy-dx
End;
Setpixel(x,y)
For k=1 to dx do
Bresenham Algorithm Begin
If(p<0) then
Begin
Begin
If (x<xf) then x=x+1;
Else x=x-1; p=p+2dy;
End
Else
Begin if(y<yf) then y=y+1
else y=y-1
If(slope>1)
If (x<xf) then x=x+1;
Begin
else x=x-1; p=p+2(dy-
temp=dx;dx=dy;dy=temp
dx);
temp=x;x=y;y=temp
End
temp=xf;xf=yf;yf=temp
If (slope>1) then setpixel(y,x)
End
Else setpixel(x,y)
P=2dy-dx
End;
Setpixel(x,y)
Bresenham’s algorithm: Example 1

• Trace the Bresenham’s algorithm for drawing the line segment from
(2,3) to (8,7)
P0=2dy-dx=2*4-6=2
dx=6 and dy=4
Pk<0pk+1=pk+2dy;
xk+1=xk+1; and yk+1=yk
Pk>=0pk+1=pk+2dy-2dx;
xk+1=xk+1; and yk+1=yk+1
Example of Performing Bresenham’s algorithm

k xk yk pk Pk+1 Xk+1 Yk+1


Example of Performing Bresenham’s algorithm

k xk yk pk Pk+1 Xk+1 Yk+1


0 2 3 2 -2 3 4
1 3 4 -2 6 4 4
2 4 4 6 2 5 5
3 5 5 2 -2 6 6
4 6 6 -2 6 7 6
5 7 6 6 2 8 7
6 8 7 2 -2 9 8
Bresenham’s Line Drawing Algorithm (for |m| < 1.0)
P0 = 2
pk+1 = pk + 2 )

At each xk along the line, starting at k = 0, perform the following test.


If pk < 0, the next point to plot is (xk, yk+1) and
pk+1 = pk + 2
Otherwise, the next point to plot is (xk+1, yk+1) and
pk+1 = pk + 2
Bresenham’s algorithm: Example 2

• Trace the Bresenham’s algorithm for drawing the line segment from
(1,1) to (6,7)
k xk yk pk Pk+1 Xk+1 Yk+1

You might also like