Docsity

Bresenham Line - Computer Graphics - Lecture Slides, Slides of Computer Graphics

Partial preview of the text

Download Bresenham Line - Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

Bresenham Line

Drawing Algorithm,

Circle Drawing &

Polygon Filling

Contents

  • In today’s lecture we’ll have a look at:
    • Bresenham’s line drawing algorithm
    • Line drawing algorithm comparisons
    • Circle drawing algorithms
      • A simple technique
      • The mid-point circle algorithm
    • Polygon fill algorithms
    • Summary of raster drawing algorithms

The Big Idea

  • Move across the x axis in unit intervals and at each step choose between two different y coordinates

2 3 4 5

2

4

3

5

For example, from position (2, 3) we have to choose between (3, 3) and (3, 4) We would like the point that is closer to the original line

( x k, y k)

( x k+1, y k)

( x k+1, y k+1)

The y coordinate on the mathematical line at

xk + 1 is:

Deriving The Bresenham Line Algorithm

  • At sample position xk + 1 the vertical separations from the mathematical

line are labelled dupper

and dlower

y = m ( xk + 1 ) + b

y yk

yk+ 1

x (^) k + 1

d (^) lower

d (^) upper

Deriving The Bresenham Line Algorithm (cont…)

  • This simple decision is based on the difference between the two pixel positions:
  • Let’s substitute m with ∆ y /∆ x where ∆ x and

∆ y are the differences between the end-points:

d lower − dupper = 2 m ( xk + 1 ) − 2 yk + 2 b − 1

( ) ( 2 ( + 1 )− 2 + 2 − 1 ) ∆

∆ ∆ − = ∆ x y b x

y x dlower dupper x k k

= 2 ∆ y ⋅ xk − 2 ∆ x ⋅ yk + 2 ∆ y + ∆ x ( 2 b − 1 ) = 2 ∆ y ⋅ xk − 2 ∆ x ⋅ yk + c Docsity.com

Deriving The Bresenham Line Algorithm (cont…)

  • So, a decision parameter pk for the k th step along a line is given by:
  • The sign of the decision parameter pk is the

same as that of dlower – dupper

  • If pk is negative, then we choose the lower pixel, otherwise we choose the upper pixel

y x x y c

p x d d

k k

k lower upper

Deriving The Bresenham Line Algorithm (cont…)

  • But, xk+ 1 is the same as xk + 1 so:
  • where yk+1 - yk is either 0 or 1 depending on

the sign of pk

  • The first decision parameter p0 is evaluated at (x0, y0) is given as:

p k + 1 = pk + 2 ∆ y − 2 ∆ x ( yk + 1 − yk )

p 0 = 2 ∆ y −∆ x

The Bresenham Line Algorithm

BRESENHAM’S LINE DRAWING ALGORITHM (for | m | < 1.0)

  1. Input the two line end-points, storing the left end-point in ( x 0 , y 0 )
  2. Plot the point ( x 0 , y 0 )
  3. Calculate the constants Δ x , Δ y , 2 Δ y , and ( 2 Δ y - 2 Δ x ) and get the first value for the decision parameter as:
  4. At each xk along the line, starting at k = 0 , perform the following test. If p (^) k < 0 , the next point to plot is (xk+1, y (^) k) and:

p (^) 0 = 2 ∆ y −∆ x

p (^) k + 1 = pk + 2 ∆ y Docsity.com

Bresenham Example

  • Let’s have a go at this
  • Let’s plot the line from (20, 10) to (30, 18)
  • First off calculate all of the constants:
    • Δ x : 10
    • Δ y : 8
    • 2Δ y : 16
    • 2Δ y - 2Δ x : -

• Calculate the initial decision parameter p0 :

  • p0 = 2Δ y – Δ x = 6 Docsity.com

Bresenham Example (cont…)

17

16

15

14

13

12

11

10

18

20 21 22 23 24 25 26 27 28 29 30

k pk (x^ k+1,y^ k+1) 0 1 2 3 4 5 6 7 8

Bresenham Exercise (cont…)

17

16

15

14

13

12

11

10

18

20 21 22 23 24 25 26 27 28 29 30

k pk (x^ k+1,y^ k+1) 0 1 2 3 4 5 6 7 8

Bresenham Line Algorithm Summary

  • The Bresenham line algorithm has the following advantages: - An fast incremental algorithm - Uses only integer calculations
  • Comparing this to the DDA algorithm, DDA has the following problems: - Accumulation of round-off errors can make the pixelated line drift away from what was intended - The rounding operations and floating point arithmetic involved are time consuming Docsity.com

A Simple Circle Drawing Algorithm (cont…)

 - y 0 = 20 2 − 02 ≈ - y 1 = 20 2 − 12 ≈ - y 2 = 20 2 − 22 ≈ 
  • y 19 = 20 2 − 192 ≈
  • y 20 = 20 2 − 202 ≈

A Simple Circle Drawing Algorithm (cont…)

  • However, unsurprisingly this is not a brilliant solution!
  • Firstly, the resulting circle has large gaps where the slope approaches the vertical
  • Secondly, the calculations are not very efficient - The square (multiply) operations - The square root operation – try really hard to avoid these!
  • We need a more efficient, more accurate solution Docsity.com