SlideShare a Scribd company logo
Computer Graphics
Computer Graphics
Lecture 05
Lecture 05
Line Drawing Techniques
Line Drawing Techniques
Muhammad Munawar Ahmed
Muhammad Munawar Ahmed
What is Point
 A point( a position in a plane) is specified by a set of ordered
pair(x, y) where x is the horizontal distance from the origin
and y is the vertical distance from the origin.
 Two points will specify a line.
Line Cont…
 Lines are described by line equation such that if a point
satisfies the line equation, then the point is on the line
 Given two points, in Euclidean geometry, one can
always find exactly one line that passes through the two
points.
 The slope-intercept form of the line, slope m is change
in y divided by change in x and intercept b is the height
at which the line crossed the y-axis.i.e (0, b)
y = mx + b
Line Cont…
A line segment is specified by its two end points. i.e (x1,y1)
and (x2, y2). Any point will be on the line , if it satisfies
the following conditions.
 y3 = mx3 + b
 Min(x1, x2)<= x3 <= max(x1, x2)
 Min(y1, y2)<= y3 <= max(y1, y2)
Line Cont…
A line may have three forms with respect to slope:
 slope = 1
 slope > 1 // Sharp Slope
 slope < 1 // Gentle Slope
Line Cont…
figure (a) figure (b) figure (c)
Line Drawing Techniques
There are three techniques to be discussed to draw a line
involving different time complexities that will be discussed
along. These techniques are:
 Incremental line algorithm
 DDA line algorithm
 Bresenham line algorithm
Incremental Line Algorithm
This algorithm exploits simple line equation
y = m x + b
where m = dy / dx
and b = y – m x
now check if
|m| < 1 then
x = x + 1 whereas
y = m x + b
why to check |m|
suppose a line has points
p1 (10, 10) – p2 (20, 18)
dy = y2 – y1 = 18 – 10 = 8
dx = x2 – x1 = 20 – 10 = 10
This means that there will be 10 pixels on the line in which for
x-axis there will be distance of 1 between each pixel and for
y-axis the distance will be 0.8.
Now consider the reverse case
suppose a line has points
p1 (10, 10) , p2 (16, 20)
dy = y2 – y1 = 20 – 10 = 10
dx = x2 – x1 = 16 – 10 = 6
This means that there will be 10 pixels on the line in which for
x-axis there will be distance of 0.6 between each pixel and for
y-axis the distance will be 1.
Now sum-up all discussion in algorithm to fully understand.
The algorithm will take two points P1 and P2 and draw line
between them whereas each point consists of x,y
coordinates.
dx = p2.x – p1. x
dy = p2.y – p1. y
m = dy / dx
x = p1.x
y = p1.y
b = y – m * x
if |m| < 1
for counter = p1.x to p2.x
drawPixel (x, y)
x = x + 1
y = m * x + b
else
for counter = p1.y to p2.y
drawPixel (x, y)
y = y + 1
x = ( y – b ) / m
Discussion on algorithm:
 quite simple and easy to understand
 but involves a lot of mathematical calculations 24
We have another algorithm that works fine in all directions
and involves less calculations; mostly only additions.
DDA Algorithm
DDA abbreviated for digital differential analyzer has a very
simple technique.
Find difference, dx and dy as:
dy = y2 – y1
dx = x2 – x1
if |dx| > |dy| then
step = |dx|
else
step = |dy|
Now very simple to say that step is the total number of pixels
required for a line.
Next step is to find xIncrement and yIncrement:
xIncrement = dx/step
yIncrement = dy/step
Next a loop is required that will run ‘step’ times.
In the loop drawPixel and add xIncrement to x1 and
yIncrement to y1.
Now sum-up all above in the algorithm:
DDA_Line (Point p1, Point p2)
dx = p2.x – p1. x
dy = p2.y – p1. y
x1=p1.x
y1=p1.y
if |dx| > |dy| then
step = |dx|
else
step = |dy|
xIncrement = dx/step
yIncrement = dy/step
for counter = 1 to step
drawPixel (x1, y1)
x1 = x1 + xIncrement
y1 = y1 + yIncrement
Criticism on Algorithm:
Use of floating point calculation
An algorithm based on integer type calculations is likely to be
more efficient
Therefore, after some effort, finally we came up with an
algorithm called “Bresenham Line Drawing Algorithm”
which would be discussed next.
Bresenham Algorithm
Bresenham's algorithm finds the closest integer
coordinates to the actual line, using only integer math.
Assuming that the slope is positive and less than 1,
moving 1 step in the x direction, y either stays the same,
or increases by 1. A decision function is required to
resolve this choice.
computer_graphics_line_algorithm in Computer Graphics
Y=mx+b x=xi+1
d1
= y – yi
= m * (xi
+1)+b – yi
d2
= yi
+ 1 – y
= yi
+ 1 – m ( xi
+ 1 ) – b
pi
= dx (d1
-d2
) put value of d1 and d2
pi
= dx (2m * (xi
+1) + 2b – 2yi
–1 )
pi
= 2dy (xi
+1) – 2dx yi
+ dx (2b–1 ) ------
---------------- (i)
pi
= 2 dy xi
– 2 dx yi
+ k ---------------- (ii)
where k = 2 dy + dx (2b-1)
Then we can calculate pi+1 in terms of pi without any xi
, yi
or
k .
pi+1
= 2 dy xi+1
– 2 dx yi+1
+ k
pi+1
= 2 dy (xi
+ 1) – 2 dx yi+1
+ k since xi+1
= xi
+ 1
pi+1
= 2 dy xi
+ 2 dy- 2 dx yi+1
+ k ---
------------ (iii)
Now subtracting (ii) from (iii), we get
pi+1
– pi
=2 dy – 2 dx (yi+1
– yi
)
pi+1
= pi
+ 2 dy – 2 dx (yi+1
– yi
)
If the next point is: (xi
+1,yi
) then
d1
< d2
=> d1
– d2
<0
=> pi
<0
=> pi
+1 = pi
+ 2 dy
If the next point is: (xi
+1,yi
+1) then
d1
>d2
=> d1
– d2
>0
=> pi
>0
=> pi
+1= pi
+ 2 dy – 2 dx
The pi
is our decision variable, and calculated using integer
arithmetic from pre-computed constants and its previous
value. Now a question is remaining; i.e. how to calculate
initial value of pi
? For that, we use equation (i) and put
values (x1
, y1
)
pi
= 2 dy (x1
+1) – 2 dx y1
+ dx (2b – 1)
where b = y – m x implies that
pi
= 2 dy x1
+2 dy – 2 dx y1
+ dx ( 2 (y1
– mx1
) – 1)
pi
= 2 dy x1
+ 2 dy – 2 dx y1
+ 2 dx y1
– 2 dy x1
– dx
pi
= 2 dy x1
+ 2 dy – 2 dx y1
+ 2 dx y1
– 2 dy x1
– dx
there are certain figures that will cancel each other (shown
in pairs of different colour)
pi
= 2 dy - dx
dx = x2
-x1
dy = y2
-y1
p = 2dy-dx
c1 = 2dy
c2 = 2(dy-dx)
x = x1
y = y1
plot (x, y, colour)
while (x < x2
)
x++
if (p < 0)
p = p + c1
else
p = p + c2
y++
plot (x,y,colour)
Improving Performance
The use of separate x and y coordinates can be discarded
in favour of direct frame buffer addressing. Most algorithms
can be adapted to calculate only the initial frame buffer
address corresponding to the starting point and to replace:
x++ with Addr++
y++ with Addr+=XResolution
Fixed point representation allows a method for performing
calculations using only integer arithmetic, but still obtaining
the accuracy of floating point values. In fixed point, the
fraction part of a value is stored separately, in another
integer:
M = Mint.Mfrac
Mint = Int(M)
Mfrac = Frac(M)× MaxInt
Addition in fixed point representation occurs by adding
fractional and integer components separately, and only
transferring any carry-over from the fractional result to the
integer result. The sequence could be implemented using
the following two integer additions: ADD Yfrac,Mfrac ; ADC
Yint,Mint
Improved versions of these algorithms exist. For example
the following variations exist on Bresenham's original
algorithm:
Symmetry (forward and backward
simultaneously)
Segmentation (divide into smaller identical segments -
GCD(D x,D y) )
Double step, triple step, n step
Setting a Pixel
Initial Task: Turning on a pixel (loading the frame buffer/bit-
map). Assume the simplest case, i.e., an 8-bit, non-
interlaced graphics system. Then each byte in the frame
buffer corresponds to a pixel in the output display.
computer_graphics_line_algorithm in Computer Graphics
addr(0,0) = the memory address of the initial pixel (0,0)
Number of rows = number of raster lines.
Number of columns = number of pixels/raster line.
To find the address of a particular pixel (X,Y) we use the
following formula:
addr(X, Y) = addr(0,0)
+ Y rows * (Xm + 1)
+ X (all in bytes)
addr(X,Y) = the memory address of
pixel (X,Y)
Example:
For a system with 640 × 480 pixel resolution, find the
address of pixel
(X = 340, Y = 150)
addr(340, 150) = addr(0,0) + 150 * 640
+ 340
= base + 96,340 is the byte location
Graphics systems usually have a command such as set_pixel
(x, y) where x, y are integers.
Lets call it a day!

More Related Content

PDF
Computer Graphics Unit 2
PDF
Chapter 2 Computer graphics by Kushal Bhattarai
PPTX
Output Primitive and Brenshamas Line.pptx
PPTX
Chapter 3 - Part 1 [Autosaved].pptx
PPT
Bresenham circles and polygons derication
PPT
Bresenham circlesandpolygons
PPTX
Chapter 3 Output Primitives
PDF
raster algorithm.pdf
Computer Graphics Unit 2
Chapter 2 Computer graphics by Kushal Bhattarai
Output Primitive and Brenshamas Line.pptx
Chapter 3 - Part 1 [Autosaved].pptx
Bresenham circles and polygons derication
Bresenham circlesandpolygons
Chapter 3 Output Primitives
raster algorithm.pdf

Similar to computer_graphics_line_algorithm in Computer Graphics (20)

PPTX
Study on Fundamentals of Raster Scan Graphics
PDF
Computer graphics notes 2 tutorials duniya
PPT
Lect14 lines+circles
PDF
Unit 2
PPT
1 linedrawing
DOCX
Dda algo notes
PDF
Computer graphics notes watermark
PDF
Computer graphics 2
PDF
Open GL T0074 56 sm1
PPTX
Output primitives in Computer Graphics
PDF
Computer Graphics_Module 2_Output Primitives.pdf
PPTX
Computer Graphics
PPTX
lecture 1.pptx
PPT
Lines and curves algorithms
PPTX
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
PPT
dokumen.tips_scan-conversion-568812b73d987.ppt
PDF
module 1.pdf
PDF
Notes_456_Lines_Drawing2_4 (1).pdf
PPTX
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
PDF
Computer graphics notes
Study on Fundamentals of Raster Scan Graphics
Computer graphics notes 2 tutorials duniya
Lect14 lines+circles
Unit 2
1 linedrawing
Dda algo notes
Computer graphics notes watermark
Computer graphics 2
Open GL T0074 56 sm1
Output primitives in Computer Graphics
Computer Graphics_Module 2_Output Primitives.pdf
Computer Graphics
lecture 1.pptx
Lines and curves algorithms
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
dokumen.tips_scan-conversion-568812b73d987.ppt
module 1.pdf
Notes_456_Lines_Drawing2_4 (1).pdf
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
Computer graphics notes
Ad

More from bsse20142018 (6)

PPT
China Overview of Dynasties in different Regions.ppt
PPTX
Early_Chinese_Civilization_Presentation.pptx
PDF
3. Single Cycle Data Path in computer architecture
PDF
2. ALU and MIPS Arcitecture introduction.pdf
PDF
Arithmatic and logic units of the cpu intro
PPTX
Python Fundamentals for the begginers in programming
China Overview of Dynasties in different Regions.ppt
Early_Chinese_Civilization_Presentation.pptx
3. Single Cycle Data Path in computer architecture
2. ALU and MIPS Arcitecture introduction.pdf
Arithmatic and logic units of the cpu intro
Python Fundamentals for the begginers in programming
Ad

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Nekopoi APK 2025 free lastest update
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
AI in Product Development-omnex systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Essential Infomation Tech presentation.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Reimagine Home Health with the Power of Agentic AI​
Nekopoi APK 2025 free lastest update
Design an Analysis of Algorithms II-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
AI in Product Development-omnex systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Understanding Forklifts - TECH EHS Solution
Essential Infomation Tech presentation.pptx
CHAPTER 2 - PM Management and IT Context
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Design an Analysis of Algorithms I-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
Softaken Excel to vCard Converter Software.pdf
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx

computer_graphics_line_algorithm in Computer Graphics

  • 1. Computer Graphics Computer Graphics Lecture 05 Lecture 05 Line Drawing Techniques Line Drawing Techniques Muhammad Munawar Ahmed Muhammad Munawar Ahmed
  • 2. What is Point  A point( a position in a plane) is specified by a set of ordered pair(x, y) where x is the horizontal distance from the origin and y is the vertical distance from the origin.  Two points will specify a line.
  • 3. Line Cont…  Lines are described by line equation such that if a point satisfies the line equation, then the point is on the line  Given two points, in Euclidean geometry, one can always find exactly one line that passes through the two points.  The slope-intercept form of the line, slope m is change in y divided by change in x and intercept b is the height at which the line crossed the y-axis.i.e (0, b) y = mx + b
  • 4. Line Cont… A line segment is specified by its two end points. i.e (x1,y1) and (x2, y2). Any point will be on the line , if it satisfies the following conditions.  y3 = mx3 + b  Min(x1, x2)<= x3 <= max(x1, x2)  Min(y1, y2)<= y3 <= max(y1, y2)
  • 5. Line Cont… A line may have three forms with respect to slope:  slope = 1  slope > 1 // Sharp Slope  slope < 1 // Gentle Slope
  • 6. Line Cont… figure (a) figure (b) figure (c)
  • 7. Line Drawing Techniques There are three techniques to be discussed to draw a line involving different time complexities that will be discussed along. These techniques are:  Incremental line algorithm  DDA line algorithm  Bresenham line algorithm
  • 8. Incremental Line Algorithm This algorithm exploits simple line equation y = m x + b where m = dy / dx and b = y – m x
  • 9. now check if |m| < 1 then x = x + 1 whereas y = m x + b
  • 10. why to check |m| suppose a line has points p1 (10, 10) – p2 (20, 18) dy = y2 – y1 = 18 – 10 = 8 dx = x2 – x1 = 20 – 10 = 10
  • 11. This means that there will be 10 pixels on the line in which for x-axis there will be distance of 1 between each pixel and for y-axis the distance will be 0.8.
  • 12. Now consider the reverse case suppose a line has points p1 (10, 10) , p2 (16, 20) dy = y2 – y1 = 20 – 10 = 10 dx = x2 – x1 = 16 – 10 = 6
  • 13. This means that there will be 10 pixels on the line in which for x-axis there will be distance of 0.6 between each pixel and for y-axis the distance will be 1.
  • 14. Now sum-up all discussion in algorithm to fully understand. The algorithm will take two points P1 and P2 and draw line between them whereas each point consists of x,y coordinates.
  • 15. dx = p2.x – p1. x dy = p2.y – p1. y m = dy / dx x = p1.x y = p1.y b = y – m * x
  • 16. if |m| < 1 for counter = p1.x to p2.x drawPixel (x, y) x = x + 1 y = m * x + b else for counter = p1.y to p2.y drawPixel (x, y) y = y + 1 x = ( y – b ) / m
  • 17. Discussion on algorithm:  quite simple and easy to understand  but involves a lot of mathematical calculations 24
  • 18. We have another algorithm that works fine in all directions and involves less calculations; mostly only additions.
  • 19. DDA Algorithm DDA abbreviated for digital differential analyzer has a very simple technique. Find difference, dx and dy as: dy = y2 – y1 dx = x2 – x1
  • 20. if |dx| > |dy| then step = |dx| else step = |dy| Now very simple to say that step is the total number of pixels required for a line.
  • 21. Next step is to find xIncrement and yIncrement: xIncrement = dx/step yIncrement = dy/step
  • 22. Next a loop is required that will run ‘step’ times. In the loop drawPixel and add xIncrement to x1 and yIncrement to y1. Now sum-up all above in the algorithm:
  • 23. DDA_Line (Point p1, Point p2) dx = p2.x – p1. x dy = p2.y – p1. y x1=p1.x y1=p1.y if |dx| > |dy| then step = |dx| else step = |dy|
  • 24. xIncrement = dx/step yIncrement = dy/step for counter = 1 to step drawPixel (x1, y1) x1 = x1 + xIncrement y1 = y1 + yIncrement
  • 25. Criticism on Algorithm: Use of floating point calculation An algorithm based on integer type calculations is likely to be more efficient
  • 26. Therefore, after some effort, finally we came up with an algorithm called “Bresenham Line Drawing Algorithm” which would be discussed next.
  • 27. Bresenham Algorithm Bresenham's algorithm finds the closest integer coordinates to the actual line, using only integer math. Assuming that the slope is positive and less than 1, moving 1 step in the x direction, y either stays the same, or increases by 1. A decision function is required to resolve this choice.
  • 29. Y=mx+b x=xi+1 d1 = y – yi = m * (xi +1)+b – yi d2 = yi + 1 – y = yi + 1 – m ( xi + 1 ) – b
  • 30. pi = dx (d1 -d2 ) put value of d1 and d2 pi = dx (2m * (xi +1) + 2b – 2yi –1 ) pi = 2dy (xi +1) – 2dx yi + dx (2b–1 ) ------ ---------------- (i) pi = 2 dy xi – 2 dx yi + k ---------------- (ii) where k = 2 dy + dx (2b-1)
  • 31. Then we can calculate pi+1 in terms of pi without any xi , yi or k . pi+1 = 2 dy xi+1 – 2 dx yi+1 + k pi+1 = 2 dy (xi + 1) – 2 dx yi+1 + k since xi+1 = xi + 1 pi+1 = 2 dy xi + 2 dy- 2 dx yi+1 + k --- ------------ (iii)
  • 32. Now subtracting (ii) from (iii), we get pi+1 – pi =2 dy – 2 dx (yi+1 – yi ) pi+1 = pi + 2 dy – 2 dx (yi+1 – yi )
  • 33. If the next point is: (xi +1,yi ) then d1 < d2 => d1 – d2 <0 => pi <0 => pi +1 = pi + 2 dy
  • 34. If the next point is: (xi +1,yi +1) then d1 >d2 => d1 – d2 >0 => pi >0 => pi +1= pi + 2 dy – 2 dx
  • 35. The pi is our decision variable, and calculated using integer arithmetic from pre-computed constants and its previous value. Now a question is remaining; i.e. how to calculate initial value of pi ? For that, we use equation (i) and put values (x1 , y1 )
  • 36. pi = 2 dy (x1 +1) – 2 dx y1 + dx (2b – 1) where b = y – m x implies that pi = 2 dy x1 +2 dy – 2 dx y1 + dx ( 2 (y1 – mx1 ) – 1) pi = 2 dy x1 + 2 dy – 2 dx y1 + 2 dx y1 – 2 dy x1 – dx pi = 2 dy x1 + 2 dy – 2 dx y1 + 2 dx y1 – 2 dy x1 – dx
  • 37. there are certain figures that will cancel each other (shown in pairs of different colour) pi = 2 dy - dx
  • 38. dx = x2 -x1 dy = y2 -y1 p = 2dy-dx c1 = 2dy c2 = 2(dy-dx) x = x1 y = y1 plot (x, y, colour)
  • 39. while (x < x2 ) x++ if (p < 0) p = p + c1 else p = p + c2 y++ plot (x,y,colour)
  • 40. Improving Performance The use of separate x and y coordinates can be discarded in favour of direct frame buffer addressing. Most algorithms can be adapted to calculate only the initial frame buffer address corresponding to the starting point and to replace: x++ with Addr++ y++ with Addr+=XResolution
  • 41. Fixed point representation allows a method for performing calculations using only integer arithmetic, but still obtaining the accuracy of floating point values. In fixed point, the fraction part of a value is stored separately, in another integer: M = Mint.Mfrac Mint = Int(M) Mfrac = Frac(M)× MaxInt
  • 42. Addition in fixed point representation occurs by adding fractional and integer components separately, and only transferring any carry-over from the fractional result to the integer result. The sequence could be implemented using the following two integer additions: ADD Yfrac,Mfrac ; ADC Yint,Mint Improved versions of these algorithms exist. For example the following variations exist on Bresenham's original algorithm:
  • 43. Symmetry (forward and backward simultaneously) Segmentation (divide into smaller identical segments - GCD(D x,D y) ) Double step, triple step, n step
  • 44. Setting a Pixel Initial Task: Turning on a pixel (loading the frame buffer/bit- map). Assume the simplest case, i.e., an 8-bit, non- interlaced graphics system. Then each byte in the frame buffer corresponds to a pixel in the output display.
  • 46. addr(0,0) = the memory address of the initial pixel (0,0) Number of rows = number of raster lines. Number of columns = number of pixels/raster line.
  • 47. To find the address of a particular pixel (X,Y) we use the following formula: addr(X, Y) = addr(0,0) + Y rows * (Xm + 1) + X (all in bytes) addr(X,Y) = the memory address of pixel (X,Y)
  • 48. Example: For a system with 640 × 480 pixel resolution, find the address of pixel (X = 340, Y = 150) addr(340, 150) = addr(0,0) + 150 * 640 + 340 = base + 96,340 is the byte location Graphics systems usually have a command such as set_pixel (x, y) where x, y are integers.
  • 49. Lets call it a day!