Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms."— Presentation transcript:

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

2 CS-321 Dr. Mark L. Hornick 2 Line Drawing Algorithms (x 0, y 0 ) (x 1, y 1 ) Which pixels should be set to represent the line?

3 CS-321 Dr. Mark L. Hornick 3 Line Equations (x 0, y 0 ) (x 1, y 1 )

4 CS-321 Dr. 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

5 CS-321 Dr. Mark L. Hornick 5 Implementation How does this work? // SimplePixelCalc - elementary pixel calculation using // variation of y=mx+b void 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 integer int iy = (int)(fy+0.5); SetPixel(ix, iy, 1); // write pixel value } How well does this work?

6 CS-321 Dr. Mark L. Hornick 6 Use the lab1 program on the following coordinates Example 1 (x 0, y 0 ) = (0,1) (x 1, y 1 ) = (10,4) Example 2 (x 0, y 0 ) = (0,8) (x 1, y 1 ) = (9,0) Example 3 (x 0, y 0 ) = (0,1) (x 1, y 1 ) = (2,7)

7 CS-321 Dr. Mark L. Hornick 7 Example 1 Results? 0 1 2 3 4 5 6 7 8 01234567891011 m=0.3

8 CS-321 Dr. Mark L. Hornick 8 Example 2 Results? 0 1 2 3 4 5 6 7 8 01234567891011 m=-0.89

9 CS-321 Dr. Mark L. Hornick 9 Example 3 Results? 0 1 2 3 4 5 6 7 8 01234567891011 m=3

10 CS-321 Dr. Mark L. Hornick 10 Simple Algorithm Adjustment for m If |m|<1 If |m|>1 If |m|=1 x = y

11 11 Simple Algorithm Summary Evaluate y = f(x) at each x position Requires floating multiplication for each evaluation of y

12 CS-321 Dr. 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 x k+1 = x k + 1 y k+1 = y k + m If |m|>1 x k+1 = x k + 1/m y k+1 = y k + 1 If |m|=1 x k+1 = x k + 1 y k+1 = y k + 1

13 CS-321 Dr. Mark L. Hornick 13 DDA Implementation void dda( int x0, int xn, int y0, int yn ) { float dx = float(xn - x0); // total span in x float dy = float(yn - y0); // total span in y float y = float(y0); float x = float(x0); float Dx, Dy; // incremental steps in x & y // determine if slope m GT or LT 1 if( 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 coords for( int k=0; k dy)? dx:dy); k++ ) { ix = int(x + 0.5); // round to nearest pixel coordinate iy = int(y + 0.5); x += Dx;// floating point calculations y += Dy; }

14 CS-321 Dr. Mark L. Hornick 14 DDA Results? 0 1 2 3 4 5 6 7 8 01234567891011

15 CS-321 Dr. 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)

16 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!


Download ppt "CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms."

Similar presentations


Ads by Google