8

Click here to load reader

Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

Embed Size (px)

Citation preview

Page 1: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

1

Bresenham’s Line Generation

The Bresenham algorithm is another incremental scan conversion algorithm. The big

advantage of this algorithm is that, it uses only integer calculations.

Difference Between DDA Line Drawing Algorithm and Bresenhams Line Drawing

Algorithm.

Digital Differential Analyzer

Bresenhams Line Drawing Algorithm

Arithmetic DDA algorithm uses floating

points i.e. Real Arithmetic.

Bresenhams algorithm uses fixed points

i.e. Integer Arithmetic.

Operations

DDA algorithm uses

multiplication and division in its

operations.

Bresenhams algorithm uses only

subtraction and addition in its operations.

Page 2: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

2

Speed

DDA algorithm is rather slowly

than Bresenhams algorithm in

line drawing because it uses real

arithmetic (floating-point

operations).

Bresenhams algorithm is faster than DDA

algorithm in line drawing because it

performs only addition and subtraction in

its calculation and uses only integer

arithmetic so it runs significantly faster.

Accuracy &

Efficiency

DDA algorithm is not as accurate

and efficient as Bresenham

algorithm.

Bresenhams algorithm is more efficient

and much accurate than DDA algorithm.

Drawing

DDA algorithm can draw circles

and curves but that are not as

accurate as Bresenhams

algorithm.

Bresenhams algorithm can draw circles

and curves with much more accuracy than

DDA algorithm.

Round Off

DDA algorithm round off the

coordinates to integer that is

nearest to the line.

Bresenhams algorithm does not round off

but takes the incremental value in its

operation.

Expensive

DDA algorithm uses an

enormous number of floating-

point multiplications so it is

expensive.

Bresenhams algorithm is less expensive

than DDA algorithm as it uses only

addition and subtraction.

Moving across the x axis in unit intervals and at each step choose between two different

y coordinates.

For example, as shown in the following illustration, from position (2, 3) you need to

choose between (3, 3) and (3, 4). You would like the point that is closer to the original

line.

Page 3: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

3

At sample position Xk+1, the vertical separations from the mathematical line are labeled

as dupper and dlower.

The pixel positions on line can be identified by doing sampling at unit x intervals.

The process of sampling begins from the pixel position (X0,Y0) and proceeds by

plotting the pixels whose ‘Y’ value is nearest to the line path.

If the pixel to be displayed occurs at a position (Xk, Yk) then the next pixel is either

at (Xk+1,Yk) or (Xk+1,Yk+1) i.e, (3,2) or (3,3)

The ‘Y’ coordinate at the pixel position Xk +1 can be obtained from

Y=m(Xk+1)+b . . . . . . . . . . ……. eq 1

the separation between (Xk+1,Yk) and (Xk+1,Y) is d1 and the separation between (Xk+1,Y)

and (Xk+1, Yk+1) is d2 then

d1 = y – yk and d2 = (Yk+1) – Y

Page 4: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

4

Y=m(Xk+1)+b . . . . . . . ……. eq 1

d1=y – yk

d1=m(xk+1)+b – Yk ( from eq (1)) …..( 2)

And d2= (Yk+1) – Y

=(YK+1) – [m(Xk+1)+b]

=(YK+1) – m(Xk+1) – b …….(3)

The difference is given as

d1-d2 = m(Xk+1)+b-Yk-[(Yk+1)-m(Xk+1)-b]

=m(Xk+1)+b-Yk-(Yk+1)+m(Xk+1)+b

=m(Xk+1)+b-Yk-Yk-1+m(Xk+1)+b

d1-d2 = 2m(Xk+1)-2Yk+2b –1 ………(4)

A decision parameter Pk can be obtained by substituting m= dy/dx in equation 4

= 2 dy/dx (Xk+1) – 2Yk + 2b – 1

= (2 dy(Xk+1)-2 dx.Yk + 2b.dx -dx )/dx

dx(d1-d2) = 2 dy(Xk+1)-2 dx.Yk + 2b.dx - dx

= 2 dyXk+2 dy-2 dx.Yk + 2b.dx -dx

(Xk, y

k)

(Xk, y

k+1)

(Xk+1, y

k+1)

(Xk+1,y

k)

P0 d

1

d2

Page 5: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

5

= 2 dyXk- 2 dx.Yk + c where c=2dy+2b.dx-dx

Where, dx(d1-d2) = Pk and

c= 2 dy+ dx(2b-1)

Pk = 2 dyXk- 2 dx.Yk + c . . . . . . . . . . . . . . . . . . . … …(5 )

The value of c is constant and is independent of the pixel position. It can be deleted in the recursive calculations, of for Pk

if d1 < d2 (i.e, Yk is nearer to the line path than Yk+1) then, Pk is negative, then a lower pixel (Yk) is plotted else, an upper pixel (Yk+1) is plotted.

At k+1 step, the value of Pk is given as

PK+1 = 2 dyXk+1- 2 dx.Yk+1 + c …………………………………..(6 ) (from 5)

Eq 6 – eq 5

Pk+1 – Pk = (2 dyXk+1- 2 dx.Yk+1 + c ) - (2 dyXk+2 dx.Yk + c)

= 2dy(Xk+1-Xk ) – 2 dx(Yk+1 – Yk ) ………….(7)

Since Xk+1 = Xk +1 The eqn 7 becomes

Pk+1 – Pk = 2dy(Xk +1 -Xk ) – 2 dx(Yk+1 – Yk )

= 2dy - 2 dx(Yk+1 – Yk )

Pk+1 = Pk + 2dy - 2 dx(Yk+1 – Yk ) ………………..( 8)

Where (Yk+1 – Yk) is either 0 or 1 based on the sign of Pk

If 0 then pk+1=pk+2dy

If 1 then pk+1=pk+2dy-2dx .

The starting parameter P0 at the pixel position (X0,Y0) is given as

P0 = 2dy – dx ………………………(9)

Bresenham’s algorithm

Page 6: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

6

Step 1: Enter the 2 end points for a line and store the left end point in (X0,Y0).

Step 2: Plot the first point be loading (X0,Y0) in the frame buffer.

Setp 3: determine the initial value of the decision parameter by calculating the constants dx, dy, 2dy and 2dy-2dx as

dx=x1-x0, dy=y1-y0, P0 = 2dy –dx

Step 4: for each Xk, conduct the following test, starting from k= 0

If Pk <0, then the next point to be plotted is at (Xk+1, Yk) and, Pk+1 = Pk + 2dy

Else, the next point is (Xk+1, Yk+1) and

Pk+1 = Pk + 2dy –2dx (step 3)

Step 5: iterate through step (4) dx times.

Or

Step 1: Enter the 2 end points for a line (X0,Y0), (X1,Y1).

Step 2: Plot (X0,Y0,colo).

Setp 3: determine the initial value of the decision parameter by calculating the constants dx=x1-x0, dy=y1-y0, P0 = 2dy –dx

for k=0 to dx do

If Pk <0, then

the next point to be plotted is at (Xk+1, Yk)

Pk+1 = Pk + 2dy

Else the next point is (Xk+1, Yk+1)

Pk+1 = Pk + 2dy –2dx

Endfor

Page 7: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

7

Example

Let the given end points for the line be (30,20) and (40, 28)

dy = y1 – y0 = 28 – 20 = 8

dx = x1 – x0 = 40 – 30 = 10

m = 0.8

The initial decision parameter P0 is

P0 = 2dy – dx = 2(8) – 10 = 16 – 10 = 6

The constants 2dy and 2dy-2dx are

2dy = 2(8) = 16 2dy-2dx = 2(8)- 2(10) =16 – 20=-4

The starting point (x0, y0)=(30,20) and the successive pixel positions are given in the following table

K Pk (X

k+1, Y

k+1)

0 6 (31,21)

1 2 (32,22

2 -2 33,22

3 14 34,23

4 10 35,24

5 6 36,25

Page 8: Bresenham’s Line Generation - University of Babylon Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this

8

6 2 37,26

7 -2 38,26

8 14 39,27

9 10 40,28