cs-321 dr. mark l. hornick 1 line drawing algorithms

16
CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

Upload: clement-gregory

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

1

Line Drawing Algorithms

Page 2: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

2

Line Drawing Algorithms

(x0, y0)

(x1, y1)

Which pixels should be set to represent the line?

Page 3: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

3

Line Equations

(x0, y0)

(x1, y1)

1 0

1 0

0 0

0 0

y mx b

y ym

x x

y y m x x

y m x x y

Page 4: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

4

Simple Algorithm

Start x at origin of line Evaluate y value Set nearest pixel (x,y) Move x one pixel

toward other endpoint Repeat until done

0 0y m x x y

Page 5: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

5

Implementation

How does this work?

// SimplePixelCalc - elementary pixel calculation using// variation of y=mx+bvoid SimplePixelCalc( int x0, int xn, int y0, int yn ){

for( int ix=x0; ix<=xn; ix++ ){

float fy = y0 + float(yn-y0)/float(xn-x0)*ix; // round to nearest whole integerint iy = (int)(fy+0.5);SetPixel(ix, iy, 1); // write pixel value

}}

How well does this

work?

Page 6: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

6

Use the lab1 program on the following coordinates

Example 1 (x0, y0) = (0,1) (x1, y1) = (10,4)

Example 2 (x0, y0) = (0,8) (x1, y1) = (9,0)

Example 3 (x0, y0) = (0,1) (x1, y1) = (2,7)

Page 7: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

7

Example 1 Results?

0

1

2

3

4

5

6

7

8

0 1 2 3 4 5 6 7 8 9 10 11

m=0.3

Page 8: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

8

Example 2 Results?

0

1

2

3

4

5

6

7

8

0 1 2 3 4 5 6 7 8 9 10 11

m=-0.89

Page 9: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

9

Example 3 Results?

0

1

2

3

4

5

6

7

8

0 1 2 3 4 5 6 7 8 9 10 11

m=3

Page 10: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

10

Simple Algorithm Adjustment for m

If |m|<1

If |m|>1

If |m|=1x = y

0 0y m x x y

0 0(1/ )x m y y x

Page 11: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

11

Simple Algorithm Summary

Evaluate y = f(x) at each x position Requires floating multiplication for each

evaluation of y

Page 12: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

12

Digital Differential Analyzer(DDA) Algorithm

Step through either x or y based on slope Build the line parametrically

If |m|<1 xk+1 = xk + 1 yk+1 = yk + m

If |m|>1 xk+1 = xk + 1/m yk+1 = yk + 1

If |m|=1 xk+1 = xk + 1 yk+1 = yk + 1

Page 13: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

13

DDA Implementationvoid dda( int x0, int xn, int y0, int yn ){

float dx = float(xn - x0); // total span in xfloat dy = float(yn - y0); // total span in yfloat y = float(y0); float x = float(x0);

float Dx, Dy; // incremental steps in x & y// determine if slope m GT or LT 1if( dx > dy ){ Dx = 1; Dy = dy/dx;// floating division, but only done once per line}else{ Dx = dx/dy; Dy = 1;}int ix, iy; // pixel coordsfor( int k=0; k<=((dx>dy)? dx:dy); k++ ){

ix = int(x + 0.5); // round to nearest pixel coordinateiy = int(y + 0.5);x += Dx; // floating point calculationsy += Dy;

}}

Page 14: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

14

DDA Results?

0

1

2

3

4

5

6

7

8

0 1 2 3 4 5 6 7 8 9 10 11

Page 15: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

CS-321Dr. Mark L. Hornick

15

DDA Algorithm Highlights

Reduction in strength Replaces multiplication with addition

Still some limitations Round-off error accumulates Floating point addition (e.g. not on PPC)

Page 16: CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms

16

Line-Drawing Algorithms

Simple algorithm Evaluate y = f(x) at each x position Floating multiplication

DDA algorithm Floating addition only

Can we do better? Tomorrow: Bresenham’s algorithm!