30
The Problem Of Scan Conversion A line segment in a scene is defined by the coordinate positions of the line end-points x y (2, 2) (7, 5) 1 B.Tulasi, Dept Of CS,Christ University Bangalore

Computer Graphics-Output Primitives

  • Upload
    btulasi

  • View
    1.650

  • Download
    1

Embed Size (px)

DESCRIPTION

line segment in a scene is defined by the coordinate positions of the line end-pointsy`A(7, 5)(2, 2)xB.Tulasi, Dept Of CS,Christ University Bangalore 1drawn on a pixel based display . Which pixel has to be chosen?`WhenB.Tulasi, Dept Of CS,Christ University Bangalore2Points to be considered ` Line should not be jarred ` It should be fast Line equation The slope intercept form of a line is y=m.x+b Where m=y2-y1/x2-x1 b=y-m.xB.Tulasi, Dept Of CS,Christ University Bangalore3

Citation preview

Page 1: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

1

The Problem Of Scan Conversion

A line segment in a scene is defined by the coordinate positions of the line end-points

x

y

(2, 2)

(7, 5)

Page 2: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

2

The Problem When drawn on a pixel based display . Which pixel has to be chosen?

Page 3: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

3

Points to be considered Line should not be jarred It should be fastLine equationThe slope intercept form of a line is y=m.x+bWhere m=y2-y1/x2-x1

b=y-m.x

Page 4: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

4

Lines & SlopesThe slope of a line (m) is defined by its start and end coordinatesThe diagram below shows some examples of lines and their slopes

m = 0

m = -1/3

m = -1/2

m = -1

m = -2

m = -4m = ∞

m = 1/3

m = 1/2

m = 1

m = 2m = 4

m = 0

Page 5: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

5

Let’s consider the following example:

x

y

(2, 2)

(7, 5)

2 7

2

5

Page 6: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

6

x

y

(2, 2)

(7, 5)

2 3 4 5 6 7

2

5

5

3

27

25

m

5

42

5

32 b

First work out m and b:

Now for each x value work out the y value:

5

32

5

43

5

3)3( y

5

13

5

44

5

3)4( y

5

43

5

45

5

3)5( y

5

24

5

46

5

3)6( y

Page 7: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

7

Round off the results.

35

32)3( y

35

13)4( y

45

43)5( y

45

24)6( y

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

Page 8: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

8

The approach is slow Multiplication of m,x Rounding of y value Alternate solutionx=y-b/m

Page 9: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

9

X(3)=2,x(4)=5

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

Page 10: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

10

If the slope of a line is between -1 and 1 then we work out the y coordinates for a line based on it’s unit x coordinatesOtherwise we do the opposite – x coordinates are computed based on unit y coordinates

m = 0

m = -1/3

m = -1/2

m = -1m = -2

m = -4 m = ∞

m = 1/3

m = 1/2

m = 1

m = 2m = 4

m = 0

Page 11: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

11

The DDA AlgorithmThe digital differential analyzer (DDA) algorithm takes an incremental approach in order to speed up scan conversionSimply calculate yk+1

based on yk

The original differential analyzer was a physical machine developed by Vannevar Bush at MIT in the 1930’s in order to solve ordinary differential equations.

Page 12: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

12

Consider the list of points that we determined for the line in our previous example: (2, 2), (3, 23/5), (4, 31/5), (5, 34/5), (6, 42/5), (7, 5)Notice that as the x coordinates go up by one, the y coordinates simply go up by the slope of the lineThis is the key insight in the DDA algorithm

Page 13: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

13

When the slope of the line is between -1 and 1 begin at the first point in the line and, by incrementing the x coordinate by 1, calculate the corresponding y coordinates as follows:

When the slope is outside these limits, increment the y coordinate by 1 and calculate the corresponding x coordinates as follows:

myy kk 1

mxx kk

11

Page 14: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

14

The DDA Algorithm (cont…)

Again the values calculated by the equations used by the DDA algorithm must be rounded to match pixel values

(xk, yk) (xk+1, yk+m)

(xk, round(yk))

(xk+1, round(yk+m))

(xk, yk) (xk+ 1/m, yk+1)

(round(xk), yk)

(round(xk+ 1/m), yk+1)

Page 15: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

15

Try out the following ◦ A(2,2) B(7,5)◦ C(3,2),D(2,7)

Page 16: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

16

The DDA Algorithm Summary

The DDA algorithm is much faster than previous attempt

◦ In particular, there are no longer any multiplications involved

However, there are still two big issues:◦ 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.

Page 17: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

17

The Bresenham Line Algorithm

The Bresenham algorithm is another incremental scan conversion algorithmThe big advantage of this algorithm is that it uses only integer calculations

Jack Bresenham worked for 27 years at IBM before entering academia. Bresenham developed his famous algorithms at IBM in the early 1960s

Page 18: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

18

The Big IdeaMove across the x axis in unit intervals and at each step choose between two different y coordinates

2 3 4 5

2

4

3

5For 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

(xk, yk)

(xk+1, yk)

(xk+1, yk+1)

Page 19: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

19

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

bxmy k )1(

y

yk

yk+1

xk+1

dlower

dupper

Page 20: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

20

So, dupper and dlower are given as follows:

and:

We can use these to make a simple decision about which pixel is closer to the mathematical line

klower yyd

kk ybxm )1(

yyd kupper )1(

bxmy kk )1(1

Page 21: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

21

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:

122)1(2 byxmdd kkupperlower

)122)1(2()(

byxx

yxddx kkupperlower

)12(222 bxyyxxy kk

cyxxy kk 22

Page 22: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

22

So, a decision parameter pk for the kth 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

cyxxy

ddxp

kk

upperlowerk

22

)(

Page 23: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

23

Remember coordinate changes occur along

the x axis in unit steps so we can do everything with integer calculationsAt step k+1 the decision parameter is given as:

Subtracting pk from this we get:

cyxxyp kkk 111 22

)(2)(2 111 kkkkkk yyxxxypp

Page 24: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

24

But, xk+1 is the same as xk+1 so:

where yk+1 - yk is either 0 or 1 depending on

the sign of pkThe first decision parameter p0 is evaluated at (x0, y0) is given as:

)(22 11 kkkk yyxypp

xyp 20

Page 25: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

25

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 (x0, y0)

2. Plot the point (x0, y0)

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 pk < 0, the next point to plot is (xk+1, yk) and:

xyp 20

ypp kk 21

Page 26: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

26

ACHTUNG! The algorithm and derivation above assumes slopes are less than 1. for other slopes we need to adjust the algorithm slightly

Otherwise, the next point to plot is (xk+1, yk+1) and:

5. Repeat step 4 (Δx – 1) times

xypp kk 221

Page 27: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

27

Bresenham ExamplePlot 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: -4

Calculate the initial decision parameter p0:

◦p0 = 2Δy – Δx = 6

Page 28: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

28

Bresenham Example (cont…)

17

16

15

14

13

12

11

10

18

292726252423222120 28 30

k pk (xk+1,yk+1)

0

1

2

3

4

5

6

7

8

9

Page 29: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

29

Bresenham ExerciseGo through the steps of the Bresenham line drawing algorithm for a line going from (21,12) to (29,16)

Page 30: Computer Graphics-Output Primitives

B.Tulasi, Dept Of CS,Christ University Bangalore

30

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