Download presentation
Presentation is loading. Please wait.
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 increases by 1y does not change X-axis Y-axis
6
Integer endpoints ΔXΔX ΔYΔY slope = ΔY/ΔX (X0,Y0) (X1,Y1) ΔY = Y1 – Y0 ΔX = X1 – X0 0 < ΔY < ΔX
7
Which point is closer? A B x i -1 xixi y = mx + b error(A) = (y i -1 + 1) – y* error(B) = y* - (y i -1 ) ideal line y i -1 y i -1 +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(x i – x 0 ) + 2(y 0 – y i -1 ) -1 Remember: m = Δy/Δx (slope of line) Multiply through by Δx (to avoid fractions) Let d i = Δx( error(B) – error(A) ) Rule is: choose B if and only if d i < 0
9
Computing d i+1 from d i d i+1 = 2(Δy)(x i+1 – x 0 ) +2(Δx)(y 0 – y i ) – Δx d i = 2(Δy)(x i – x 0 ) + 2(Δx)(y 0 – y i-1 ) – Δx The difference can be expressed as: d i+1 = d i + 2(Δy)(x i+1 – x i ) – 2(Δy)(y i – y i-1 ) Recognize that x i+1 – x i = 1 at every step And also: y i – y i-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 (x 0,y 0 ) Thus, at step i =1, our formula for d i is: d 1 = 2(Δy) - Δx And, at each step thereafter: if ( d i < 0 ) { d i+1 = d i + 2(Δy); y i+1 = y i ; } else { d i+1 = d i + 2(Δy-Δx); y i+1 = y i + 1; } x i+1 = x i + 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 0 1 2 3 4 5 6 7 8 10 11 12 88 79 610 11 12 13 14 15 16 5 7 9 11 13 15 Y XLOXHI 13 17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.