SlideServe
Menu
Préférences
© SlideServe · v2026

Bresenham's Algorithm for Line Drawing and Polygon Filling

delila avatar
17 diapositives · 332 Views · Sep 26, 2013

Bresenham's Algorithm, a fundamental approach for drawing lines and filling polygons using integer arithmetic. Originally published in 1965 for plotters, this efficient, iterative algorithm approximates continuous lines on a discrete grid, handling various cases, including horizontal, vertical, and sloped lines. Key formulas govern decision-making at each step based on an error term. Learn to adapt this method for polygon filling while handling complex shapes and ensuring smooth rendering. Ideal for enhancing computer graphics skills through practical coding exercises.

Bresenham's Algorithm for Line Drawing and Polygon Filling - slide 1
01 / 17Bresenham's Algorithm for Line Drawing and Polygon Filling
1 / 17

Télécharger la présentation

Bresenham's Algorithm for Line Drawing and Polygon Filling
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Drawing Lines The Bresenham Algorithm for drawing lines and filling polygons

  2. Plotting a line-segment • Bresenham published algorithm in 1965 • It was originally to be used with a plotter • It adapts well to raster “scan conversion” • It uses only integer arithmetic operations • It is an “iterative” algorithm: each step is based on results from the previous step • The sign of an “error term” governs the choice among two alternative actions

  3. Scan conversion The actual line is comprised of points drawn from a continuum, but it must be “approximated” using pixels from a discrete grid.

  4. The various cases • Horizontal or vertical lines are easy cases • Lines that have slope 1 or -1 are easy, too • Symmetries leave us one remaining case: 0 < slope < 1 • As x-coodinate is incremented, there are just two possibilities for the y-coordinate: (1) y-coordinate is increased by one; or (2) y-coordinate remains unchanged

  5. 0 < slope < 1 Y-axis X-axis y increases by 1 y does not change

  6. Integer endpoints ΔY = Y1 – Y0 ΔX = X1 – X0 (X1,Y1) 0 < ΔY < ΔX ΔY (X0,Y0) ΔX slope = ΔY/ΔX

  7. Which point is closer? y = mx + b A yi -1+1 yi -1 B ideal line xi xi -1 error(A) = (yi -1 + 1) – y* error(B) = y* - (yi -1)

  8. The Decision Variable • Choose B if and only if error(B)<error(A) • Or equivalently: error(B) – error(A) < 0 • Formula: error(B) – error(A) = 2m(xi – x0) + 2(y0 – yi -1) -1 • Remember: m = Δy/Δx (slope of line) • Multiply through by Δx (to avoid fractions) • Let di = Δx( error(B) – error(A) ) • Rule is: choose B if and only if di < 0

  9. Computing di+1 from di di+1 = 2(Δy)(xi+1 – x0) +2(Δx)(y0 – yi) – Δx di = 2(Δy)(xi – x0) + 2(Δx)(y0 – yi-1) – Δx The difference can be expressed as: di+1 = di + 2(Δy)(xi+1 – xi) – 2(Δy)(yi – yi-1) Recognize that xi+1 – xi = 1 at every step And also: yi – yi-1 will be either 0 or 1 (depending on the sign of the previous d)

  10. How does algorithm start? • At the outset we start from point (x0,y0) • Thus, at step i =1, our formula for di is: d1 = 2(Δy) - Δx • And, at each step thereafter: if ( d i < 0 ) { di+1 = di + 2(Δy); yi+1 = yi; } else { di+1 = di + 2(Δy-Δx); yi+1 = yi + 1; } xi+1 = xi + 1;

  11. ‘bresdemo.cpp’ • The example-program is on class website: http://nexus.cs.usfca.edu/~cruse/cs686/ • It draws line-segments with various slopes • The Michener algorithm (for a circle-fill) is also included, for comparative purposes • Extreme slopes (close to zero or infinity) are not displayed in this demo program • They can be added by you as an exercise

  12. Filling a triangle or polygon • The Bresenham’s method can be adapted • But an efficient data-structure is needed • All the sides need to be handled together • We let the y-coordinate steadily increment • For sides which are “nearly horizontal” the x-coordinates can change by more than 1

  13. Triangle Illustration

  14. Non-Convex Polygons

  15. Bucket-Sort Y 0 XLO XHI 1 2 8 8 3 7 9 4 6 10 5 5 11 6 7 12 7 9 13 8 11 14 10 13 15 11 15 16 12 17 17 13

  16. Handling Corners

  17. In-class exercises • For the ‘bresdemo.cpp’ program: • Supply a function that tests the capability of the Breshenham line-drawing algorithm to draw lines having the full range of slopes • For the ‘fillpoly.cpp’ program: • Modify the program code so that it will work with polygons having more than three sides