SlideShare a Scribd company logo
1 of 12
Line Drawing Algorithms

A line in Computer graphics typically refers to
   line segment, which is a portion of straight
   line that extends indefinitely in opposite
   direction. It is defined by its two end points &
   the slope intercept equation for a line:
                      y = mx + b               (1)
where, m = Slope of the line
         b = the y intercept of a line
The two endpoints of a line segment are
  specified at positions (x1,y1) and (x2,y2).
          y
                              P2(x2,y2)


                  P1(x1,y1)
          b

              0
                                x
We can determine the value for slope m & b
  intercept as
            m = y2-y1/x2-x1                (2)
And,         b = y1 – mx1                   (3)
For a given x interval Δx along a line, we can
compute the corresponding y interval Δy from
            Δy = m Δx                      (4)
Similarly, we can obtain x interval Δx by Δy:
            Δx = Δy/m                      (5)
If |m|<1, then for every integer value of x
    between and excluding x1 and x2, calculate
    the corresponding value of y using
    equation Δy = m Δx & scan convert (x,y).
If |m|>1, then for every integer value of y
    between and excluding y1 and y2, calculate
    the corresponding value of x using
    equation Δx = Δy/m & scan convert (x,y).
If |m|=1, Δx = Δy. In each case, a smooth line
    with slope m is generated between the
    specific endpoints.
Example 1 The endpoints of line are(0,0) &
  (6,18). Compute each value of y as x steps
  from 0 to 6 and plot the result.
Solution : Equation of line is y= mx +b
m = y2-y1/x2-x1= 18-0/6-0 = 3
Next the y intercept b is found by plugging
  y1& x1 into the equation y = 3x + b,
  0 = 3(0) + b. Therefore, b=0, so the
  equation for the line is y= 3x.
While this approach is mathematically sound,
 it involves floating-point computation
 (multiplication & addition) in every step
 that uses the line equation since m & b are
 generally real numbers. The challenge is to
 find a way to achieve the same goal as
 quickly as possible.
DDA Algorithm
     The digital differential analyzer (DDA)
algorithm is an incremental scan-
conversion method. Such an approach is
characterized by performing calculations at
each step using results from the preceding
step. Suppose, at step i we have
calculated(xi, yi) to be a point on the
line.Since the next point (xi+1,yi+1) should
satisfy Δy/Δx= m where Δy= yi+1 – yi &
Δx = xi+1 – xi.
We have, yi+1 = yi + mΔx
            yi+1 = yi + Δy            (1)
Or          xi+1 = xi + Δy/m          (2)
Algorithm:
(x1,y1) (x2,y2) are the end points and dx, dy are the
   float variables.
(i) If abs(x2-x1) > abs(y2-y1) then
          length = abs(x2-x1)
     else
          length = abs(y2-y1)
     endif
(ii)    dx = (x2-x1)/length
        dy = (y2-y1)/length
(iii)   x = x1 + 0.5
        y = y1 + 0.5
(iv)    i=0
(v)     Plot (trunc(x), trunc(y))
(vi)     x = x + dx
          y = y + dy
(vii)    i=i+1
(viii) If i < length then go to step (v)
(ix) Stop
Example 2 Scan convert a line having end
     points (3,2) & (4,7) using DDA.
Solution: x2 - x1 = 4-3 = 1
              y2 - y1 = 7-2 = 5
As, abs(x2-x1) < abs(y2-y1) then
       length = y2-y1 = 5
    dx = (x2-x1)/ length = 1/5 = .2
    dy = (y2-y1)/ length = 5/5 = 1
x1 y1 x2 y2 L dx dy i x     y     Result     Plot
3 2 4 7 5 .2 1 0 3.5        2.5   3.5, 2.5   3,2
                    1 3.7   3.5   3.7,3.5    3,3
                    2 3.9   4.5   3.9,4.5    3,4
                    3 4.1   5.5   4.1,5.5    4,5
                    4 4.3   6.5   4.3,6.5    4,6
                    5 4.5   7.5   4.5,7.5    4,7
Limitations of DDA:
(1) The rounding operation & floating point
    arithmetic are time consuming procedures.
(2) The accumulation of round-off error in
    successive addition of floating point
    increment can cause the calculated pixel
    position to drift away from the true line
    path for long line segment.

More Related Content

What's hot

Window to viewport transformation
Window to viewport transformationWindow to viewport transformation
Window to viewport transformation
Ankit Garg
 
Window to viewport transformation&amp;matrix representation of homogeneous co...
Window to viewport transformation&amp;matrix representation of homogeneous co...Window to viewport transformation&amp;matrix representation of homogeneous co...
Window to viewport transformation&amp;matrix representation of homogeneous co...
Mani Kanth
 
Attributes of output primitives( curve attributes & area fill attributes)
Attributes of output primitives( curve attributes & area fill attributes)Attributes of output primitives( curve attributes & area fill attributes)
Attributes of output primitives( curve attributes & area fill attributes)
shalinikarunakaran1
 

What's hot (20)

Window to viewport transformation
Window to viewport transformationWindow to viewport transformation
Window to viewport transformation
 
Depth Buffer Method
Depth Buffer MethodDepth Buffer Method
Depth Buffer Method
 
COMPUTER GRAPHICS-"Projection"
COMPUTER GRAPHICS-"Projection"COMPUTER GRAPHICS-"Projection"
COMPUTER GRAPHICS-"Projection"
 
Back face detection
Back face detectionBack face detection
Back face detection
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 
Cohen sutherland line clipping
Cohen sutherland line clippingCohen sutherland line clipping
Cohen sutherland line clipping
 
Clipping
ClippingClipping
Clipping
 
Window to viewport transformation&amp;matrix representation of homogeneous co...
Window to viewport transformation&amp;matrix representation of homogeneous co...Window to viewport transformation&amp;matrix representation of homogeneous co...
Window to viewport transformation&amp;matrix representation of homogeneous co...
 
3 d display methods
3 d display methods3 d display methods
3 d display methods
 
Graphics software and standards
Graphics software and standardsGraphics software and standards
Graphics software and standards
 
Attributes of output primitives( curve attributes & area fill attributes)
Attributes of output primitives( curve attributes & area fill attributes)Attributes of output primitives( curve attributes & area fill attributes)
Attributes of output primitives( curve attributes & area fill attributes)
 
Video display devices
Video display devicesVideo display devices
Video display devices
 
Matrix representation- CG.pptx
Matrix representation- CG.pptxMatrix representation- CG.pptx
Matrix representation- CG.pptx
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics
 
Three dimensional concepts - Computer Graphics
Three dimensional concepts - Computer GraphicsThree dimensional concepts - Computer Graphics
Three dimensional concepts - Computer Graphics
 
Character attributes
Character attributesCharacter attributes
Character attributes
 
Raster scan system & random scan system
Raster scan system & random scan systemRaster scan system & random scan system
Raster scan system & random scan system
 
line attributes.pptx
line attributes.pptxline attributes.pptx
line attributes.pptx
 
Polygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesPolygons - Computer Graphics - Notes
Polygons - Computer Graphics - Notes
 

Viewers also liked

Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
Mohd Arif
 
Vector graphics
Vector graphicsVector graphics
Vector graphics
lenance
 
Parametric equations
Parametric equationsParametric equations
Parametric equations
Tarun Gehlot
 

Viewers also liked (20)

Dda line-algorithm
Dda line-algorithmDda line-algorithm
Dda line-algorithm
 
Clipping in Computer Graphics
Clipping in Computer GraphicsClipping in Computer Graphics
Clipping in Computer Graphics
 
Segments in Graphics
Segments in GraphicsSegments in Graphics
Segments in Graphics
 
dda algorithm
dda  algorithmdda  algorithm
dda algorithm
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
Anti aliasing Computer Graphics
Anti aliasing Computer GraphicsAnti aliasing Computer Graphics
Anti aliasing Computer Graphics
 
Lecture15 anti aliasing
Lecture15 anti aliasingLecture15 anti aliasing
Lecture15 anti aliasing
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 
Vector graphics
Vector graphicsVector graphics
Vector graphics
 
Analytical chemistry_Instrumentation_Introduction
Analytical chemistry_Instrumentation_IntroductionAnalytical chemistry_Instrumentation_Introduction
Analytical chemistry_Instrumentation_Introduction
 
Perspective projection
Perspective projectionPerspective projection
Perspective projection
 
hermite cubic spline curve
hermite cubic spline curvehermite cubic spline curve
hermite cubic spline curve
 
[Download] rev chapter-5-june26th
[Download] rev chapter-5-june26th[Download] rev chapter-5-june26th
[Download] rev chapter-5-june26th
 
Hermite spline english_20161201_jintaeks
Hermite spline english_20161201_jintaeksHermite spline english_20161201_jintaeks
Hermite spline english_20161201_jintaeks
 
Presentation non parametric
Presentation non parametricPresentation non parametric
Presentation non parametric
 
Parametric equations
Parametric equationsParametric equations
Parametric equations
 
Presentation on bezier curve
Presentation on bezier curvePresentation on bezier curve
Presentation on bezier curve
 

Similar to Line drawing algo.

Line drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniquesLine drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniques
Ankit Garg
 

Similar to Line drawing algo. (20)

Computer graphics LINE DRAWING algorithm.pptx
Computer graphics LINE DRAWING algorithm.pptxComputer graphics LINE DRAWING algorithm.pptx
Computer graphics LINE DRAWING algorithm.pptx
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output Primitives
 
OUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxOUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptx
 
OUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxOUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptx
 
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
 
Output Primitive and Brenshamas Line.pptx
Output Primitive and Brenshamas Line.pptxOutput Primitive and Brenshamas Line.pptx
Output Primitive and Brenshamas Line.pptx
 
Lab lecture 2 bresenham
Lab lecture 2 bresenhamLab lecture 2 bresenham
Lab lecture 2 bresenham
 
Rasterization.pptx
Rasterization.pptxRasterization.pptx
Rasterization.pptx
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
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
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniya
 
Applications of Differential Calculus in real life
Applications of Differential Calculus in real life Applications of Differential Calculus in real life
Applications of Differential Calculus in real life
 
Line drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniquesLine drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniques
 
Nis differentiation1
Nis differentiation1Nis differentiation1
Nis differentiation1
 
Line circle draw
Line circle drawLine circle draw
Line circle draw
 
Computer graphics 2
Computer graphics 2Computer graphics 2
Computer graphics 2
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 

More from Mohd Arif

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcp
Mohd Arif
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
Mohd Arif
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
Mohd Arif
 
Project identification
Project identificationProject identification
Project identification
Mohd Arif
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
Mohd Arif
 
Presentation
PresentationPresentation
Presentation
Mohd Arif
 
Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
Mohd Arif
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
Mohd Arif
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
Mohd Arif
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
Mohd Arif
 
Network management
Network managementNetwork management
Network management
Mohd Arif
 
Networing basics
Networing basicsNetworing basics
Networing basics
Mohd Arif
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platform
Mohd Arif
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
Mohd Arif
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
Mohd Arif
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
Mohd Arif
 

More from Mohd Arif (20)

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcp
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
 
Project identification
Project identificationProject identification
Project identification
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
 
Presentation
PresentationPresentation
Presentation
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
 
Network management
Network managementNetwork management
Network management
 
Networing basics
Networing basicsNetworing basics
Networing basics
 
Loaders
LoadersLoaders
Loaders
 
Lists
ListsLists
Lists
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platform
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
 
Heap sort
Heap sortHeap sort
Heap sort
 

Line drawing algo.

  • 1. Line Drawing Algorithms A line in Computer graphics typically refers to line segment, which is a portion of straight line that extends indefinitely in opposite direction. It is defined by its two end points & the slope intercept equation for a line: y = mx + b (1) where, m = Slope of the line b = the y intercept of a line
  • 2. The two endpoints of a line segment are specified at positions (x1,y1) and (x2,y2). y P2(x2,y2) P1(x1,y1) b 0 x
  • 3. We can determine the value for slope m & b intercept as m = y2-y1/x2-x1 (2) And, b = y1 – mx1 (3) For a given x interval Δx along a line, we can compute the corresponding y interval Δy from Δy = m Δx (4) Similarly, we can obtain x interval Δx by Δy: Δx = Δy/m (5)
  • 4. If |m|<1, then for every integer value of x between and excluding x1 and x2, calculate the corresponding value of y using equation Δy = m Δx & scan convert (x,y). If |m|>1, then for every integer value of y between and excluding y1 and y2, calculate the corresponding value of x using equation Δx = Δy/m & scan convert (x,y). If |m|=1, Δx = Δy. In each case, a smooth line with slope m is generated between the specific endpoints.
  • 5. Example 1 The endpoints of line are(0,0) & (6,18). Compute each value of y as x steps from 0 to 6 and plot the result. Solution : Equation of line is y= mx +b m = y2-y1/x2-x1= 18-0/6-0 = 3 Next the y intercept b is found by plugging y1& x1 into the equation y = 3x + b, 0 = 3(0) + b. Therefore, b=0, so the equation for the line is y= 3x.
  • 6. While this approach is mathematically sound, it involves floating-point computation (multiplication & addition) in every step that uses the line equation since m & b are generally real numbers. The challenge is to find a way to achieve the same goal as quickly as possible.
  • 7. DDA Algorithm The digital differential analyzer (DDA) algorithm is an incremental scan- conversion method. Such an approach is characterized by performing calculations at each step using results from the preceding step. Suppose, at step i we have calculated(xi, yi) to be a point on the line.Since the next point (xi+1,yi+1) should satisfy Δy/Δx= m where Δy= yi+1 – yi & Δx = xi+1 – xi.
  • 8. We have, yi+1 = yi + mΔx yi+1 = yi + Δy (1) Or xi+1 = xi + Δy/m (2) Algorithm: (x1,y1) (x2,y2) are the end points and dx, dy are the float variables. (i) If abs(x2-x1) > abs(y2-y1) then length = abs(x2-x1) else length = abs(y2-y1) endif
  • 9. (ii) dx = (x2-x1)/length dy = (y2-y1)/length (iii) x = x1 + 0.5 y = y1 + 0.5 (iv) i=0 (v) Plot (trunc(x), trunc(y)) (vi) x = x + dx y = y + dy (vii) i=i+1
  • 10. (viii) If i < length then go to step (v) (ix) Stop Example 2 Scan convert a line having end points (3,2) & (4,7) using DDA. Solution: x2 - x1 = 4-3 = 1 y2 - y1 = 7-2 = 5 As, abs(x2-x1) < abs(y2-y1) then length = y2-y1 = 5 dx = (x2-x1)/ length = 1/5 = .2 dy = (y2-y1)/ length = 5/5 = 1
  • 11. x1 y1 x2 y2 L dx dy i x y Result Plot 3 2 4 7 5 .2 1 0 3.5 2.5 3.5, 2.5 3,2 1 3.7 3.5 3.7,3.5 3,3 2 3.9 4.5 3.9,4.5 3,4 3 4.1 5.5 4.1,5.5 4,5 4 4.3 6.5 4.3,6.5 4,6 5 4.5 7.5 4.5,7.5 4,7
  • 12. Limitations of DDA: (1) The rounding operation & floating point arithmetic are time consuming procedures. (2) The accumulation of round-off error in successive addition of floating point increment can cause the calculated pixel position to drift away from the true line path for long line segment.