SlideShare a Scribd company logo
1 of 46
Chapter 3
Scan Conversion Algorithms
(Point and Line)
April 23, 2023 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
April 23, 2023 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
April 23, 2023 Computer Graphics 4
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
April 23, 2023 Computer Graphics 5
Scan Converting a Point
MODELLING CO-ORDINATES
• Mathematically vectors are defined in an
infinite, “real-number” Cartesian co-
ordinate system
SCREEN COORDINATES
• a.k.a device co-ordinates, pixel co-ordinates
• On display hardware we deal with finite, discrete
coordinates
• X, Y values in positive integers
• 0,0 is measured from top-left usually with +Y
pointing down
Y
X
A pixel (10, 6)
X
Y
Z
A point (-12.3,
10.3, 0)
April 23, 2023 Computer Graphics 6
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)
April 23, 2023 Computer Graphics 7
Scan Conversion Algorithms
1. Scan Conversion of Point
2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse
April 23, 2023 Computer Graphics 8
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
April 23, 2023 Computer Graphics 9
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 = 0
m = -1/3
m = -1/2
m = -1
m = -2
m = -4
m = ∞
m = 1/3
m = 1/2
m = 1
m = 2
m = 4
m = 0
April 23, 2023 Computer Graphics 10
Line Drawing Algorithms
1. DDA Line Algorithm
2. Bresenham Line Algorithm
April 23, 2023 Computer Graphics 11
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 (Dx or Dy ) (Incremental Approach)
DDA Algorithm
April 23, 2023 Computer Graphics 12
2. Basic Concept
– For each part of the line the following holds true:
– If Dx = 1 i.e. 1 pixel then …
– 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 (Dy) for every step to the right (Dx)
DDA Algorithm
m
y 
D
x
m
y
x
y
m D

D

D
D

April 23, 2023 Computer Graphics 13
3. Derivation
Assume that 0<m<1, Dx > 0 and Dy >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
DDA Algorithm
April 23, 2023 Computer Graphics 14
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));
}
April 23, 2023 Computer Graphics 15
DDA ALGORITHM
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 16
DDA ALGORITHM
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 17
DDA ALGORITHM
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 18
DDA ALGORITHM
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 19
DDA ALGORITHM
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 20
DDA ALGORITHM
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 21
DDA ALGORITHM
Consider endpoints:
P1(0,0), P2(7,4)
Sample at unit x:
1
1


D



k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1



D



D



m
y
x
m
y
y
y
y
k
k
k
k
5. Example
April 23, 2023 Computer Graphics 22
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??????
April 23, 2023 Computer Graphics 23
DDA Algorithm
7. Y-Version (|m|>1)
– If the slope is very steep (i.e. Dy is
much greater than Dx) 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
April 23, 2023 Computer Graphics 24
8. Generic solution
– Modify Basic idea
– 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
y
m
x
m
x
m
y
D

D

D

D
1
0
1
m
0
:
1
:
1
1








m
y
y
x
x
i
i
i
i
or
m
1
1
:
1
:
1
1







m
x
x
y
y
i
i
i
i
DDA Algorithm
Why?
discontinuity !!
April 23, 2023 Computer Graphics 25
– What is m<0 ?
– We must see the sign of Dx and Dy
– Or can we have simple solution???????
DDA Algorithm - Generic solution
Vario
us
Cases
Dx>0
Dy>0
Dx>0
Dy<0
Dx<0
Dy>0
Dx<0
Dy<0
|m|<1
xi+1 = xi + 1
yi+1 = yi +
m
xi+1 = xi + 1
yi+1 = yi - m
xi+1 = xi - 1
yi+1 = yi +
m
xi+1 = xi - 1
yi+1 = yi – m
(or swap
points)
|m|>1
xi+1 = xi +
1/m
yi+1 = yi + 1
xi+1 = xi +
1/m
yi+1 = yi - 1
xi+1 = xi -
1/m
yi+1 = yi +
xi+1 = xi - 1/m
yi+1 = yi – 1
(or swap
April 23, 2023 Computer Graphics 26
Incremental DDA Algorithm
1. Input (x1,y1) and (x2,y2)
2. Compute
dx = x2-x1 and dy = y2-y1
m = dy/dx
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));
}
April 23, 2023 Computer Graphics 27
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
DDA Algorithm
April 23, 2023 Computer Graphics 28
DDA Algorithm
Rounding Error
– Note that the actual pixel position is
actually stored as a REAL number
(in C/C++/java a float or a double)
– But we Round Off to the nearest
whole number just before we draw
the pixel.
– e.g. if m=.333 …
1.0
2.0
3.0
4.0
X Y Rounded { x, y }
0.33
0.66
0.99
1.32
{ 1, 0 }
{ 2, 1 }
{ 3, 1 }
{ 4, 1 }
April 23, 2023 Computer Graphics 29
Line Drawing Algorithms
1. DDA Line Algorithm
2. Bresenham Line Algorithm
April 23, 2023 Computer Graphics 30
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
Bresenham’s Line Algorithm
April 23, 2023 Computer Graphics 31
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
– No division, Efficient comparison, No floating point operations
Specified
Line Path
1
0 
 m
Specified
Line Path
April 23, 2023 Computer Graphics 32
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
April 23, 2023 Computer Graphics 33
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)
l
k
x
x
m l
k 


 ,
,
1
0
1

k
x
k
y
1

k
y
y
2
d
1
d
b
mx
y 

k
x 1

k
x 2

k
x
2

k
y
1

k
y
k
y P
P
Q
1
Q
2
Q
1
Q
2
Q
Q
April 23, 2023 Computer Graphics 34
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
April 23, 2023 Computer Graphics 35
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)
)
1
2
2
)
1
(
2
( 



D
D
D
 b
y
x
x
y
x k
k
)
1
2
(
2
2
2 
D

D


D


D
 b
x
y
y
x
x
y k
k
constant
a
1
2
2
where
2
2
)
b
Δx(
Δy
c
k
k c
y
x
x
y





D


D

)
1
2
2
)
1
(
2
( 



D
 b
y
x
m
x k
k
April 23, 2023 Computer Graphics 36
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
April 23, 2023 Computer Graphics 37



D

D


D










D

D



D


D



D

D




D

D












otherwise
x
y
p
p
if
y
p
p
otherwise
y
p
if
y
y
y
y
x
y
p
p
y
y
x
x
x
y
p
p
p
p
c
y
x
x
y
p
c
y
x
x
y
p
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
2
2
0
2
1
0
)
(
2
2
)
(
2
)
(
2
from
subtract
.
2
.
2
1
k
k
Replacing
.
2
.
2
1
1
1
1
1
1
1
1
1
1
1
Bresenham’s Line Algorithm
To put pk in the iterative form, we derived that
April 23, 2023 Computer Graphics 38
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.
April 23, 2023 Computer Graphics 39
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
April 23, 2023 Computer Graphics 40
April 23, 2023 Computer Graphics 41
Bresenham’s Line Algorithm
4. General solution
– The algorithm can be generalized for all slopes
Cases/
slope
Case I
Dx>0 , Dy>0
Case II
Dx>0, Dy<0
Case III
Dx<0, Dy>0
Case IV
Dx<0, Dy<0
|m|<1
p = 2dy-dx
for x = x1 to x2
if p<0
p = p+ 2dy
else { y=y+1
p = p+2(dy-dx)}
p = 2dy-dx
for x = x1 to x2
if p<0
p = p+ 2dy
else { y=y-1
p = p+2(dy-dx)}
swap points
Case II
swap points
Case I
|m|>1
p = 2dx-dy
for y= y1 to y2
if p<0
p = p+ 2dx
else { x=x+1
p = p+2(dx-dy)}
p = 2dx-dy
for y= y2 to y1
if p<0
p = p+ 2dx
else { x=x+1
p = p+2(dx-dy)}
swap points
Case II
swap points
Case I
April 23, 2023 Computer Graphics 42
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
2. Dx = ?, Dy =?, 2Dy = ?, 2Dy – 2Dx =?
1. (x0, y0) = ?
3. p0 = 2Dy – Dx =?
k pk (xk+1, yk+1)
April 23, 2023 Computer Graphics 43
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2
1. (x0, y0) = (12,10)
3. p0 = 2Dy – Dx =3
k pk (xk+1, yk+1)
0 3
1
2
April 23, 2023 Computer Graphics 44
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2
1. (x0, y0) = (12,10)
3. p0 = 2Dy – Dx =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)
April 23, 2023 Computer Graphics 45
Bresenham’s Line Algorithm
17
16
15
14
13
12
11
10
18
29
27
26
25
24
23
22
21
20 28 30
k pk (xk+1,yk+1)
5. Exercise: Trace for (20,10) to (30,18)
April 23, 2023 Computer Graphics 46
Bresenham’s Line Algorithm
17
16
15
14
13
12
11
10
18
29
27
26
25
24
23
22
21
20 28 30
k pk (xk+1,yk+1)
0
1
2
3
4
5
6
7
8
9
6
2
-2
14
10
6
2
-2
14
10
(21,11)
(22,12)
(23,12)
(24,13)
(25,14)
(26,15)
(27,16)
(28,16)
(29,17)
(30,18)
Answer

More Related Content

What's hot

Color Models Computer Graphics
Color Models Computer GraphicsColor Models Computer Graphics
Color Models Computer Graphicsdhruv141293
 
Attributes of output primitive(line attributes)
Attributes of output primitive(line attributes)Attributes of output primitive(line attributes)
Attributes of output primitive(line attributes)shalinikarunakaran1
 
Bresenham's line algorithm
Bresenham's line algorithmBresenham's line algorithm
Bresenham's line algorithmPooja Dixit
 
Composite transformation
Composite transformationComposite transformation
Composite transformationPooja Dixit
 
Video display devices
Video display devicesVideo display devices
Video display devicesMohd Arif
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics University of Potsdam
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)Timbal Mayank
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.Mohd Arif
 
Histogram Processing
Histogram ProcessingHistogram Processing
Histogram ProcessingAmnaakhaan
 
Attributes of Output Primitives
Attributes of Output PrimitivesAttributes of Output Primitives
Attributes of Output PrimitivesRenita Santhmayora
 
Computer graphics basic transformation
Computer graphics basic transformationComputer graphics basic transformation
Computer graphics basic transformationSelvakumar Gna
 
B. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath YogiB. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath YogiTekendra Nath Yogi
 
Hidden surface removal
Hidden surface removalHidden surface removal
Hidden surface removalAnkit Garg
 
Computer Graphics - Output Primitive
Computer Graphics - Output PrimitiveComputer Graphics - Output Primitive
Computer Graphics - Output PrimitiveRupesh Mishra
 
Character generation techniques
Character generation techniquesCharacter generation techniques
Character generation techniquesMani Kanth
 

What's hot (20)

Color Models Computer Graphics
Color Models Computer GraphicsColor Models Computer Graphics
Color Models Computer Graphics
 
Attributes of output primitive(line attributes)
Attributes of output primitive(line attributes)Attributes of output primitive(line attributes)
Attributes of output primitive(line attributes)
 
Bresenham's line algorithm
Bresenham's line algorithmBresenham's line algorithm
Bresenham's line algorithm
 
Composite transformation
Composite transformationComposite transformation
Composite transformation
 
Video display devices
Video display devicesVideo display devices
Video display devices
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics
 
Region based segmentation
Region based segmentationRegion based segmentation
Region based segmentation
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
 
Histogram Processing
Histogram ProcessingHistogram Processing
Histogram Processing
 
Attributes of Output Primitives
Attributes of Output PrimitivesAttributes of Output Primitives
Attributes of Output Primitives
 
Computer graphics basic transformation
Computer graphics basic transformationComputer graphics basic transformation
Computer graphics basic transformation
 
Computer graphics notes
Computer graphics notesComputer graphics notes
Computer graphics notes
 
B. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath YogiB. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 1.3 By Tekendra Nath Yogi
 
Boundary fill algm
Boundary fill algmBoundary fill algm
Boundary fill algm
 
Hidden surface removal
Hidden surface removalHidden surface removal
Hidden surface removal
 
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 lecture2  computer graphics graphics hardware(Computer graphics tutorials) lecture2  computer graphics graphics hardware(Computer graphics tutorials)
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 
Computer Graphics - Output Primitive
Computer Graphics - Output PrimitiveComputer Graphics - Output Primitive
Computer Graphics - Output Primitive
 
Character generation techniques
Character generation techniquesCharacter generation techniques
Character generation techniques
 

Similar to Lecture _Line Scan Conversion.ppt

Notes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdfNotes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdfPranavRawat14
 
Unit 2
Unit 2Unit 2
Unit 2ypnrao
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniyaTutorialsDuniya.com
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsAmol Gaikwad
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfRajJain516913
 
Rasterization.pptx
Rasterization.pptxRasterization.pptx
Rasterization.pptxAhmadAbba6
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptxssuser255bf1
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2SanthiNivas
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualAnkit Kumar
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output PrimitivesPrathimaBaliga
 
Bresenham circlesandpolygons
Bresenham circlesandpolygonsBresenham circlesandpolygons
Bresenham circlesandpolygonsaa11bb11
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 

Similar to Lecture _Line Scan Conversion.ppt (20)

Computer graphics notes watermark
Computer graphics notes watermarkComputer graphics notes watermark
Computer graphics notes watermark
 
Notes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdfNotes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdf
 
Unit 2
Unit 2Unit 2
Unit 2
 
03.Scan Conversion.ppt
03.Scan Conversion.ppt03.Scan Conversion.ppt
03.Scan Conversion.ppt
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniya
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
 
lecture 1.pptx
lecture 1.pptxlecture 1.pptx
lecture 1.pptx
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Rasterization.pptx
Rasterization.pptxRasterization.pptx
Rasterization.pptx
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
Dda algorithm
Dda algorithmDda algorithm
Dda algorithm
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output Primitives
 
Bresenham circlesandpolygons
Bresenham circlesandpolygonsBresenham circlesandpolygons
Bresenham circlesandpolygons
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Lecture _Line Scan Conversion.ppt

  • 1. Chapter 3 Scan Conversion Algorithms (Point and Line)
  • 2. April 23, 2023 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
  • 3. April 23, 2023 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
  • 4. April 23, 2023 Computer Graphics 4 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
  • 5. April 23, 2023 Computer Graphics 5 Scan Converting a Point MODELLING CO-ORDINATES • Mathematically vectors are defined in an infinite, “real-number” Cartesian co- ordinate system SCREEN COORDINATES • a.k.a device co-ordinates, pixel co-ordinates • On display hardware we deal with finite, discrete coordinates • X, Y values in positive integers • 0,0 is measured from top-left usually with +Y pointing down Y X A pixel (10, 6) X Y Z A point (-12.3, 10.3, 0)
  • 6. April 23, 2023 Computer Graphics 6 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)
  • 7. April 23, 2023 Computer Graphics 7 Scan Conversion Algorithms 1. Scan Conversion of Point 2. Scan Conversion of Line 3. Scan Conversion of Circle 4. Scan Conversion of Ellipse
  • 8. April 23, 2023 Computer Graphics 8 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
  • 9. April 23, 2023 Computer Graphics 9 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 = 0 m = -1/3 m = -1/2 m = -1 m = -2 m = -4 m = ∞ m = 1/3 m = 1/2 m = 1 m = 2 m = 4 m = 0
  • 10. April 23, 2023 Computer Graphics 10 Line Drawing Algorithms 1. DDA Line Algorithm 2. Bresenham Line Algorithm
  • 11. April 23, 2023 Computer Graphics 11 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 (Dx or Dy ) (Incremental Approach) DDA Algorithm
  • 12. April 23, 2023 Computer Graphics 12 2. Basic Concept – For each part of the line the following holds true: – If Dx = 1 i.e. 1 pixel then … – 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 (Dy) for every step to the right (Dx) DDA Algorithm m y  D x m y x y m D  D  D D 
  • 13. April 23, 2023 Computer Graphics 13 3. Derivation Assume that 0<m<1, Dx > 0 and Dy >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 DDA Algorithm
  • 14. April 23, 2023 Computer Graphics 14 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)); }
  • 15. April 23, 2023 Computer Graphics 15 DDA ALGORITHM Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 16. April 23, 2023 Computer Graphics 16 DDA ALGORITHM Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 17. April 23, 2023 Computer Graphics 17 DDA ALGORITHM Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 18. April 23, 2023 Computer Graphics 18 DDA ALGORITHM Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 19. April 23, 2023 Computer Graphics 19 DDA ALGORITHM Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 20. April 23, 2023 Computer Graphics 20 DDA ALGORITHM Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 21. April 23, 2023 Computer Graphics 21 DDA ALGORITHM Consider endpoints: P1(0,0), P2(7,4) Sample at unit x: 1 1   D    k k k x x x x Corresponding y pos.: ) 1 ( 1    D    D    m y x m y y y y k k k k 5. Example
  • 22. April 23, 2023 Computer Graphics 22 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??????
  • 23. April 23, 2023 Computer Graphics 23 DDA Algorithm 7. Y-Version (|m|>1) – If the slope is very steep (i.e. Dy is much greater than Dx) 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
  • 24. April 23, 2023 Computer Graphics 24 8. Generic solution – Modify Basic idea – 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 y m x m x m y D  D  D  D 1 0 1 m 0 : 1 : 1 1         m y y x x i i i i or m 1 1 : 1 : 1 1        m x x y y i i i i DDA Algorithm Why? discontinuity !!
  • 25. April 23, 2023 Computer Graphics 25 – What is m<0 ? – We must see the sign of Dx and Dy – Or can we have simple solution??????? DDA Algorithm - Generic solution Vario us Cases Dx>0 Dy>0 Dx>0 Dy<0 Dx<0 Dy>0 Dx<0 Dy<0 |m|<1 xi+1 = xi + 1 yi+1 = yi + m xi+1 = xi + 1 yi+1 = yi - m xi+1 = xi - 1 yi+1 = yi + m xi+1 = xi - 1 yi+1 = yi – m (or swap points) |m|>1 xi+1 = xi + 1/m yi+1 = yi + 1 xi+1 = xi + 1/m yi+1 = yi - 1 xi+1 = xi - 1/m yi+1 = yi + xi+1 = xi - 1/m yi+1 = yi – 1 (or swap
  • 26. April 23, 2023 Computer Graphics 26 Incremental DDA Algorithm 1. Input (x1,y1) and (x2,y2) 2. Compute dx = x2-x1 and dy = y2-y1 m = dy/dx 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)); }
  • 27. April 23, 2023 Computer Graphics 27 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 DDA Algorithm
  • 28. April 23, 2023 Computer Graphics 28 DDA Algorithm Rounding Error – Note that the actual pixel position is actually stored as a REAL number (in C/C++/java a float or a double) – But we Round Off to the nearest whole number just before we draw the pixel. – e.g. if m=.333 … 1.0 2.0 3.0 4.0 X Y Rounded { x, y } 0.33 0.66 0.99 1.32 { 1, 0 } { 2, 1 } { 3, 1 } { 4, 1 }
  • 29. April 23, 2023 Computer Graphics 29 Line Drawing Algorithms 1. DDA Line Algorithm 2. Bresenham Line Algorithm
  • 30. April 23, 2023 Computer Graphics 30 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 Bresenham’s Line Algorithm
  • 31. April 23, 2023 Computer Graphics 31 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 – No division, Efficient comparison, No floating point operations Specified Line Path 1 0   m Specified Line Path
  • 32. April 23, 2023 Computer Graphics 32 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
  • 33. April 23, 2023 Computer Graphics 33 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) l k x x m l k     , , 1 0 1  k x k y 1  k y y 2 d 1 d b mx y   k x 1  k x 2  k x 2  k y 1  k y k y P P Q 1 Q 2 Q 1 Q 2 Q Q
  • 34. April 23, 2023 Computer Graphics 34 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
  • 35. April 23, 2023 Computer Graphics 35 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) ) 1 2 2 ) 1 ( 2 (     D D D  b y x x y x k k ) 1 2 ( 2 2 2  D  D   D   D  b x y y x x y k k constant a 1 2 2 where 2 2 ) b Δx( Δy c k k c y x x y      D   D  ) 1 2 2 ) 1 ( 2 (     D  b y x m x k k
  • 36. April 23, 2023 Computer Graphics 36 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
  • 37. April 23, 2023 Computer Graphics 37    D  D   D           D  D    D   D    D  D     D  D             otherwise x y p p if y p p otherwise y p if y y y y x y p p y y x x x y p p p p c y x x y p c y x x y p k k k k k k k k k k k k k k k k k k k k k k k k k k 2 2 0 2 1 0 ) ( 2 2 ) ( 2 ) ( 2 from subtract . 2 . 2 1 k k Replacing . 2 . 2 1 1 1 1 1 1 1 1 1 1 1 Bresenham’s Line Algorithm To put pk in the iterative form, we derived that
  • 38. April 23, 2023 Computer Graphics 38 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.
  • 39. April 23, 2023 Computer Graphics 39 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
  • 40. April 23, 2023 Computer Graphics 40
  • 41. April 23, 2023 Computer Graphics 41 Bresenham’s Line Algorithm 4. General solution – The algorithm can be generalized for all slopes Cases/ slope Case I Dx>0 , Dy>0 Case II Dx>0, Dy<0 Case III Dx<0, Dy>0 Case IV Dx<0, Dy<0 |m|<1 p = 2dy-dx for x = x1 to x2 if p<0 p = p+ 2dy else { y=y+1 p = p+2(dy-dx)} p = 2dy-dx for x = x1 to x2 if p<0 p = p+ 2dy else { y=y-1 p = p+2(dy-dx)} swap points Case II swap points Case I |m|>1 p = 2dx-dy for y= y1 to y2 if p<0 p = p+ 2dx else { x=x+1 p = p+2(dx-dy)} p = 2dx-dy for y= y2 to y1 if p<0 p = p+ 2dx else { x=x+1 p = p+2(dx-dy)} swap points Case II swap points Case I
  • 42. April 23, 2023 Computer Graphics 42 Bresenham’s Line Algorithm 5. Exercise Calculate pixel positions that made up the line connecting endpoints: (12, 10) and (17, 14). 2. Dx = ?, Dy =?, 2Dy = ?, 2Dy – 2Dx =? 1. (x0, y0) = ? 3. p0 = 2Dy – Dx =? k pk (xk+1, yk+1)
  • 43. April 23, 2023 Computer Graphics 43 Bresenham’s Line Algorithm 5. Exercise Calculate pixel positions that made up the line connecting endpoints: (12, 10) and (17, 14). 2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2 1. (x0, y0) = (12,10) 3. p0 = 2Dy – Dx =3 k pk (xk+1, yk+1) 0 3 1 2
  • 44. April 23, 2023 Computer Graphics 44 Bresenham’s Line Algorithm 5. Exercise Calculate pixel positions that made up the line connecting endpoints: (12, 10) and (17, 14). 2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2 1. (x0, y0) = (12,10) 3. p0 = 2Dy – Dx =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)
  • 45. April 23, 2023 Computer Graphics 45 Bresenham’s Line Algorithm 17 16 15 14 13 12 11 10 18 29 27 26 25 24 23 22 21 20 28 30 k pk (xk+1,yk+1) 5. Exercise: Trace for (20,10) to (30,18)
  • 46. April 23, 2023 Computer Graphics 46 Bresenham’s Line Algorithm 17 16 15 14 13 12 11 10 18 29 27 26 25 24 23 22 21 20 28 30 k pk (xk+1,yk+1) 0 1 2 3 4 5 6 7 8 9 6 2 -2 14 10 6 2 -2 14 10 (21,11) (22,12) (23,12) (24,13) (25,14) (26,15) (27,16) (28,16) (29,17) (30,18) Answer