31
6.837 LECTURE 4 1. Line-Drawing Algorithms 2. Line-Drawing Algorithms 3. Quest for the Ideal Line 4. Simple Line 5. lineSimple( ) Demonstration 6. Let's Make it Work! 7. lineImproved( ) Demonstration 8. Optimize Inner Loops 9. Modified Algorithm 10. lineDDA( ) Demonstration 11. Was Our Objective Met? 12. Low-Level Optimizations 13. Applications of Low-level Optimizations 13a. Geometric Interpretation 14. More Low-level Optimizations 15. More Low-level Optimizations 16. The resulting method is known as Bresenham's line drawing algorithm 17. lineBresenham( ) Demonstration 18. Was it worth it? 19. Question Infrastructure 20. Faster Bresenham Algorithm 21. Was it worth it? 22. Beyond Bresenham 23. Was it worth it? 24. Epilogue 25. A Line in Sheep's Clothing 26. The Plot Thickens... 27. Generalized Pull Down 28. Next Time Lecture 4 Outline 6.837 Fall '01 Lecture 4 --- 6.837 Fall '01 http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/index.html [9/20/2001 5:29:05 PM]

Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

  • Upload
    dangnhu

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

6.837 LECTURE 41. Line-Drawing Algorithms 2. Line-Drawing Algorithms

3. Quest for the Ideal Line 4. Simple Line

5. lineSimple( ) Demonstration 6. Let's Make it Work!

7. lineImproved( ) Demonstration 8. Optimize Inner Loops

9. Modified Algorithm 10. lineDDA( ) Demonstration

11. Was Our Objective Met? 12. Low-Level Optimizations

13. Applications of Low-level Optimizations 13a. Geometric Interpretation

14. More Low-level Optimizations 15. More Low-level Optimizations

16. The resulting method is known as Bresenham's line drawing algorithm 17. lineBresenham( ) Demonstration

18. Was it worth it? 19. Question Infrastructure

20. Faster Bresenham Algorithm 21. Was it worth it?

22. Beyond Bresenham 23. Was it worth it?

24. Epilogue 25. A Line in Sheep's Clothing

26. The Plot Thickens... 27. Generalized Pull Down

28. Next Time

  Lecture 4   Outline   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/index.html [9/20/2001 5:29:05 PM]

Page 2: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Line-Drawing Algorithms

A Study in Optimization

Make it workMake it rightMake it fast

  Lecture 4   Slide 1   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide01.html [9/20/2001 5:29:18 PM]

Page 3: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Line-Drawing Algorithms

Our first adventure into scan conversion.

Scan-conversion or rasterization●

Due to the scanning nature of raster displays●

Algorithms are fundamental to both2-D and 3-D computer graphics

Transforming the continuous into this discrete(sampling)

Line drawing was easy for vector displays●

Most incremental line-drawing algorithmswere first developed for pen-plotters

Most of the early scan-conversion algorithms developedfor plotters can be attributed to one man, Jack Bresenham.

Lecture 4   Slide 2   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide02.html [9/20/2001 5:29:20 PM]

Page 4: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Quest for the Ideal Line

The best we can do is a discrete approximation of an ideal line.

Important line qualities:

Continuous appearence■

Uniform thickness and brightness■

Accuracy (Turn on the pixels nearest the ideal line)■

Speed (How fast is the line generated)■

Lecture 4   Slide 3   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide03.html [9/20/2001 5:29:41 PM]

Page 5: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Simple Line

Based on the simple slope-intercept algorithm from algebra

y = m x + b

public void lineSimple(int x0, int y0, int x1, int y1, Colorcolor) { int pix = color.getRGB(); int dx = x1 - x0; int dy = y1 - y0;

raster.setPixel(pix, x0, y0); if (dx != 0) { float m = (float) dy / (float) dx; float b = y0 - m*x0; dx = (x1 > x0) ? 1 : -1; while (x0 != x1) { x0 += dx; y0 = Math.round(m*x0 + b); raster.setPixel(pix, x0, y0); } } }

Lecture 4   Slide 4   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide04.html [9/20/2001 5:29:43 PM]

Page 6: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

lineSimple( ) Demonstration

Draw a line by clicking and dragging on the pixel grid shown with the leftmouse button. An ideal line is displayed until the left button is released.Upon release a discrete approximation of the line is drawn on the displaygrid using the lineSimple() method described in the previous slide. Anideal line is then overlaid for comparison.

The lineSimple() method:

Does it work?

Try various slopes.

Lecture 4   Slide 5   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide05.html [9/20/2001 5:29:44 PM]

Page 7: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Let's Make it Work!

Problem: lineSimple( ) does not give satisfactory results for slopes > 1

Solution: symmetrypublic void lineImproved(int x0, int y0, int x1, int y1, Color color) { int pix = color.getRGB(); int dx = x1 - x0; int dy = y1 - y0;

raster.setPixel(pix, x0, y0); if (Math.abs(dx) > Math.abs(dy)) { // slope < 1 float m = (float) dy / (float) dx; // compute slope float b = y0 - m*x0; dx = (dx < 0) ? -1 : 1; while (x0 != x1) { x0 += dx; raster.setPixel(pix, x0, Math.round(m*x0 + b)); } } else if (dy != 0) { // slope >= 1 float m = (float) dx / (float) dy; // compute slope float b = x0 - m*y0; dy = (dy < 0) ? -1 : 1; while (y0 != y1) { y0 += dy; raster.setPixel(pix, Math.round(m*y0 + b), y0); } }}

Lecture 4   Slide 6   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide06.html [9/20/2001 5:29:46 PM]

Page 8: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

lineImproved( ) Demonstration

Draw a line by clicking and dragging on the pixel grid shown with the leftmouse button. An ideal line is displayed until the left button is released.Upon release a discrete approximation of the line is drawn on the displaygrid using the lineImproved() method described in the previous slide. Anideal line is then overlaid for comparison.

The lineImproved() method:

Notice that the slope-intercept equation of the line is executed at each step of the inner loop.

Lecture 4   Slide 7   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide07.html [9/20/2001 5:29:47 PM]

Page 9: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Optimize Inner LoopsOptimize those code fragments where the algorithm spends most of its time.

Often these fragmentsare inside loops.

Overall code organization:

lineMethod( ) { // 1. general set up // 2. special case set up while (notdone) { // 3. inner loop } }

remove unnecessary method invocations

replace Math.round(m*x0 + b)with (int)(m*x0 + b + 0.5)Does this always work?

use incremental calculations

Consider the expression

y = (int)(m*x + b + 0.5)

The value of y is known at x0 (i.e. it is y0 + 0.5)Future values of y can be expressed in terms of previous valueswith a difference equation:

yi+1 = yi + m;or

yi+1 = yi - m;

Lecture 4   Slide 8   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide08.html [9/20/2001 5:31:03 PM]

Page 10: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Modified Algorithm

This line drawing method is called a Digital Differential Analyzer or DDA for short.

public void lineDDA(int x0, int y0, int x1, int y1, Color color) { int pix = color.getRGB(); int dy = y1 - y0; int dx = x1 - x0; float t = (float) 0.5; // offset for rounding raster.setPixel(pix, x0, y0); if (Math.abs(dx) > Math.abs(dy)) { // slope < 1 float m = (float) dy / (float) dx; // compute slope t += y0; dx = (dx < 0) ? -1 : 1; m *= dx; while (x0 != x1) { x0 += dx; // step to next x value t += m; // add slope to y value raster.setPixel(pix, x0, (int) t); } } else { // slope >= 1 float m = (float) dx / (float) dy; // compute slope t += x0; dy = (dy < 0) ? -1 : 1; m *= dy; while (y0 != y1) { y0 += dy; // step to next y value t += m; // add slope to x value raster.setPixel(pix, (int) t, y0); } } }

Lecture 4   Slide 9   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide09.html [9/20/2001 5:31:05 PM]

Page 11: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

lineDDA( ) Demonstration

Draw a line by clicking and dragging on the pixel grid shown with the leftmouse button. An ideal line is displayed until the left button is released.

Upon release a discrete approximation of the line is drawn on the displaygrid using the lineDDA() method described in the previous slide. An ideal

line is then overlaid for comparison.

The lineDDA() method:

You should not see any difference inthe lines generated by this method and the lineImproved( ) methodmentioned previously.

Lecture 4   Slide 10   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide10.html [9/20/2001 5:31:08 PM]

Page 12: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Was Our Objective Met?

This applet above allows you to select from the various line drawingalgorithms discussed. You can draw lines using the selected algorithm byclicking and dragging with the first mouse button. You can also time thealgorithms by clicking on Benchmark. In order to get more accuratetimings the pattern is drawn five times (without clearing), and the finalresult is displayed.

To the left is a benchmarking applet

Modern compilers will often find these sorts of optimizations

Dilemma:

Is it better to retain readable code, and depend a compiler to do the optimization implicitly,or code the optimization explicitly with some loss in readability?

Lecture 4   Slide 11   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide11.html [9/20/2001 5:31:09 PM]

Page 13: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Low-Level Optimizations

Low-level optimizations are dubious, becausethey often depend on specific machine details.

However, a set of general rules that are more-or-less consistent across machines include:

Addition and Subtraction are generally faster than Multiplication.●

Multiplication is generally faster than Division.●

Using tables to evaluate discrete functions is faster than computing them●

Integer caluculations are faster than floating-point calculations.●

Avoid unnecessary computation by testing for various special cases.●

The intrinsic tests available to most machines are greater than,less than, greater than or equal, and less than or equal to zero (not an arbitrary value).

Lecture 4   Slide 12   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide12.html [9/20/2001 5:31:11 PM]

Page 14: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Applications of Low-level OptimizationsNotice that the slope is always rational (a ratio of two integers).

m = (y1 - y0) / (x1 - x0)

Note that the incremental part of the algorthim never generates a new y that is more than one unit away from the old one(because the slope is always less than one)

yi+1 = yi + m

Thus, if we maintained the only the only fractional part of y we could still draw a line by noting when this fractionexceeded one. If we initialize fraction with 0.5, then we will also handle the rounding correctly as in our DDA routine.

fraction += m; if (fraction >= 1) { y = y + 1; fraction -= 1; }

Lecture 4   Slide 13   6.837 Fall '01

For our line drawing algorithm we'll investigate applying several of these optimizations. The incremental calculation effectively removed multiplications in favor of

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide13.html (1 of 2) [9/20/2001 5:33:08 PM]

Page 15: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

additions. Our next optimization will use three of the mentioned methods. It will remove floating-point calculations in favor of integer operations, and it will remove thesingle divide operation (it makes a difference on short lines), and it will normalize the tests to tests for zero.

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide13.html (2 of 2) [9/20/2001 5:33:08 PM]

Page 16: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Geometric Interpretation

fraction = 0.5; // Initial

fraction += m; // Increment

if 0.5 <= m <= 1 then fraction >= 1Plot(1,1)

if 0 < m < 0.5 then fraction < 1Plot(1,0)

fraction += m; if (fraction >= 1) { y = y + 1; fraction -= 1; }

Lecture 4   Slide 13a   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide13a.html [9/20/2001 5:33:36 PM]

Page 17: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

More Low-level Optimizations

Note that y is now an integer.Can we represent the fraction as an integer?

After we draw the first pixel (which happens outside our main loop) the correct fraction is:

fraction = 1/2 + dy/dx

If we scale the fraction by 2*dx the following expression results:

scaledFraction = dx + 2*dy

and the incremental update becomes:

scaledFraction += 2*dy //2*dx*(dy/dx)

and our test must be modified to reflect the new scaling

if (scaledFraction >= 2*dx) { ... }

Lecture 4   Slide 14   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide14.html [9/20/2001 5:33:38 PM]

Page 18: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

More Low-level Optimizations

This test can be made against a value of zero if the inital value of scaledFraction has 2*dxsubtracted from it. Giving, outside the loop:

OffsetScaledFraction = dx + 2*dy - 2*dx =2*dy - dx

and the inner loop becomes

OffsetScaledFraction += 2*dy if (OffsetScaledFraction >= 0) { y = y + 1; fraction -= 2*dx; }

We might as well double the values of dy and dx (this can be accomplished with either anadd or a shift outside the loop).

Lecture 4   Slide 15   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide15.html [9/20/2001 5:33:40 PM]

Page 19: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

The resulting method is known as Bresenham's line drawing algorithm public void lineBresenham(int x0, int y0, int x1, int y1, Color color) { int pix = color.getRGB(); int dy = y1 - y0; int dx = x1 - x0; int stepx, stepy; if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; } if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; } dy <<= 1; // dy is now 2*dy dx <<= 1; // dx is now 2*dx raster.setPixel(pix, x0, y0); if (dx > dy) { int fraction = dy - (dx >> 1); // same as 2*dy - dx while (x0 != x1) { if (fraction >= 0) { y0 += stepy; fraction -= dx; // same as fraction -= 2*dx } x0 += stepx; fraction += dy; // same as fraction -= 2*dy raster.setPixel(pix, x0, y0); } } else { int fraction = dx - (dy >> 1); while (y0 != y1) { if (fraction >= 0) { x0 += stepx; fraction -= dy; } y0 += stepy; fraction += dx; raster.setPixel(pix, x0, y0); } } }

Lecture 4   Slide 16   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide16.html [9/20/2001 5:34:26 PM]

Page 20: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

lineBresenham( ) Demonstration

Draw a line by clicking and dragging on the pixel grid shown with the leftmouse button. An ideal line is displayed until the left button is released.Upon release a discrete approximation of the line is drawn on the displaygrid using the lineBresenham() method described in the previous slide. Anideal line is then overlaid for comparison.

The lineBresenham() method:

Does it work?

Lecture 4   Slide 17   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide17.html [9/20/2001 5:34:29 PM]

Page 21: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Was it worth it?

This applet above allows you to select from the various line drawingalgorithms discussed. You can draw lines using the selected algorithm byclicking and dragging with the first mouse button. You can also time thealgorithms by clicking on Benchmark. In order to get more accuratetimings the pattern is drawn five times (without clearing), and the finalresult is displayed.

To the left is a benchmarking applet

Lecture 4   Slide 18   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide18.html [9/20/2001 5:34:31 PM]

Page 22: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Question Infrastructure

There is still a hidden multiply inside of our inner loop

/** * Sets a pixel to a given value */ public final boolean setPixel(int pix, int x, int y) { pixel[y*width+x] = pix; return true; }

Our next optimization of Bresenham's algorithmeliminates even this multiply.

Lecture 4   Slide 19   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide19.html [9/20/2001 5:34:32 PM]

Page 23: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Faster Bresenham Algorithm public void lineFast(int x0, int y0, int x1, int y1, Color color) { int pix = color.getRGB(); int dy = y1 - y0; int dx = x1 - x0; int stepx, stepy; int width = raster.getWidth(): int pixel = raster.getPixelBuffer(); if (dy < 0) { dy = -dy; stepy = -width; } else { stepy = -width; } if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; } dy <<= 1; dx <<= 1; y0 *= width; y1 *= width; pixel[x0+y0] = pix; if (dx > dy) { int fraction = dy - (dx >> 1); while (x0 != x1) { if (fraction >= 0) { y0 += stepy; fraction -= dx; } x0 += stepx; fraction += dy; pixel[x0+y0] = pix; } } else { int fraction = dx - (dy >> 1); while (y0 != y1) { if (fraction >= 0) { x0 += stepx; fraction -= dy; } y0 += stepy; fraction += dx; pixel[x0+y0] = pix; } } }

Lecture 4   Slide 20   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide20.html [9/20/2001 5:34:48 PM]

Page 24: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Was it worth it?

This applet above allows you to select from the various line drawingalgorithms discussed. You can draw lines using the selected algorithm byclicking and dragging with the first mouse button. You can also time thealgorithms by clicking on Benchmark. In order to get more accuratetimings the pattern is drawn five times (without clearing), and the finalresult is displayed.

To the left is a benchmarking applet

Can we go even faster?

Lecture 4   Slide 21   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide21.html [9/20/2001 5:34:50 PM]

Page 25: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Beyond BresenhamMost books would have you believe that the developmentof line drawing algorithms ended with Bresenham'sfamous algorithm. But there has been some signifcantwork since then. The following 2-step algorithm,developed by Xiaolin Wu, is a good example. Theinteresting story of this algorithm's development isdiscussed in an article that appears in Graphics Gems I byBrian Wyvill.

The two-step algorithm takes the interesting approach oftreating line drawing as a automaton, or finite statemachine. If one looks at the possible configurations thatthe next two pixels of a line, it is easy to see that only afinite set of possiblities exist.

The two-step algorithm also exploits the symmetry ofline-drawing by simultaneously drawn from both endstowards the midpoint.

The code, which is here, is a bit long to show an a slide.

Lecture 4   Slide 22   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide22.html [9/20/2001 5:34:52 PM]

Page 26: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Was it worth it?Note: Compare the speed of lineBresenham( ) tolineTwoStep( ). This comparision is most fair sinceI did not remove the calls to raster.setPixel() in thetwo-step algorithm.

Hardly!

Lecture 4   Slide 23   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide23.html [9/20/2001 5:34:55 PM]

Page 27: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Epilogue

There are still many important issues associated with line drawing

Examples:

Non-integer endpoints(occurs frequently when rendering 3D lines)

Can we make lines appear less "jaggy"?●

What if a line endpoint lies outside the viewing area?●

How do you handle thick lines?●

Optimizations for connected line segments●

Lines show up in the strangest places●

Lecture 4   Slide 24   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide24.html [9/20/2001 5:34:58 PM]

Page 28: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

A Line in Sheep's Clothing

Movies developed for theaters are usually shot at 24 frames per second, while video ontelevision is displayed at 30 frames per second. Thus, when motion pictures are transferredto video formats they must undergo a process called "3-2 pull-down", where every fourthframe is repeated thus matching the frame rates of the two media, as depicted below.

Lecture 4   Slide 25   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide25.html [9/20/2001 5:35:01 PM]

Page 29: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

The Plot Thickens...Once upon a time, there was a company with a similar, yet more complicated problem.They had developed a multimedia movie-player technology that they wished to displayon a workstation's CRT screen. They also made screens with various update ratesincluding 60, 66, 72, and 75 frames-per-second. Their movie player had to support awide range of source materials including, but not limited to:

Source Frame Rate

Motion Pictures 24 fps

NTSC Video (frames) 30 fps

NTSC Video (fields) 60 fps

PAL Video (frames) 25 fps

PAL Video (fields) 50 fps

Common Multimedia 10 fps

These source materials were to be displayed as close as possible to the correct frame rate.We were not expected to support frame rates higher than the CRT's update rate. Uponhaving the problem explained, someone commented, "It's just a line-drawing algorithm!"

Lecture 4   Slide 26   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide26.html [9/20/2001 5:35:04 PM]

Page 30: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Generalized Pull DownThe generalized pull-down problem is nothing more that drawing the best discreteapproximation of the ideal line from (1, 1) to (desired frame rate, source frame rate), asshown in the following diagram for the (30 fps, 24 fps).

Moreover, it is a very special case of a line. Can you think of addition optimizations to ourBresenham line drawing code that could be applyed for the special case of pull-down?

Lecture 4   Slide 27   6.837 Fall '01

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide27.html [9/20/2001 5:35:08 PM]

Page 31: Lecture 4 --- 6.837 Fall '01 - MIT CSAILgroups.csail.mit.edu/graphics/classes/6.837/F01/Lecture04/lecture... · The resulting method is known as Bresenham's line drawing algorithm

Next Time

Lecture 4   Slide 28   6.837 Fall '01  

Lecture 4 --- 6.837 Fall '01

http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide28.html [9/20/2001 5:35:15 PM]