Docsity

Line Drawing Algorithms, Lecture Notes - Animation, Study notes of Computer Graphics and Animation

Toggle sidebar
bg1
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]
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Line Drawing Algorithms, Lecture Notes - Animation and more Study notes Computer Graphics and Animation in PDF only on Docsity!

6.837 LECTURE 4

  1. Line-Drawing Algorithms^ 2. Line-Drawing Algorithms
  2. Quest for the^ Ideal Line^ 4. Simple Line
  3. lineSimple( ) Demonstration^ 6. Let's Make it Work!
  4. lineImproved( ) Demonstration^ 8. Optimize Inner Loops
  5. Modified Algorithm 10. lineDDA( ) Demonstration
  6. Was Our Objective Met?^ 12. Low-Level Optimizations
  7. Applications of Low-level Optimizations^ 13a. Geometric Interpretation
  8. More Low-level Optimizations^ 15. More Low-level Optimizations
  9. The resulting method is known as Bresenham's line drawing algorithm^ 17. lineBresenham( ) Demonstration
  10. Was it worth it?^ 19. Question Infrastructure
  11. Faster Bresenham Algorithm^ 21. Was it worth it?
  12. Beyond Bresenham 23. Was it worth it?
  13. Epilogue^ 25. A Line in Sheep's Clothing
  14. The Plot Thickens... 27. Generalized Pull Down
  15. Next Time

Lecture 4 (^) Outline 6.837 Fall '

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

Line-Drawing Algorithms

A Study in Optimization

Make it work

Make it right

Make it fast

Lecture 4 (^) Slide 1 6.837 Fall '

Quest for the Ideal Line

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

Important line qualities:

n Continuous appearence

n Uniform thickness and brightness

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

n Speed (How fast is the line generated)

Lecture 4 (^) Slide 3 6.837 Fall '

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, Color

color) {

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 '

Let's Make it Work!

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

Solution: symmetry

public 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 - mx0; dx = (dx < 0)? -1 : 1; while (x0 != x1) { x0 += dx; raster.setPixel(pix, x0, Math.round(mx0 + b)); } } else if (dy != 0) { // slope >= 1 float m = (float) dx / (float) dy; // compute slope float b = x0 - my0; dy = (dy < 0)? -1 : 1; while (y0 != y1) { y0 += dy; raster.setPixel(pix, Math.round(my0 + b), y0); } } }

Lecture 4 (^) Slide 6 6.837 Fall '

lineImproved( ) Demonstration

Draw a line by clicking and dragging on the pixel grid shown with the left mouse button. An ideal line is displayed until the left button is released. Upon release a discrete approximation of the line is drawn on the display grid using the lineImproved() method described in the previous slide. An ideal 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 '

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 '

lineDDA( ) Demonstration

Draw a line by clicking and dragging on the pixel grid shown with the left mouse button. An ideal line is displayed until the left button is released. Upon release a discrete approximation of the line is drawn on the display grid 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 in

the lines generated by this method and the lineImproved( ) method

mentioned previously.

Lecture 4 (^) Slide 10 6.837 Fall '

Low-Level Optimizations

Low-level optimizations are dubious, because

they often depend on specific machine details.

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

l Addition and Subtraction are generally faster than Multiplication.

l Multiplication is generally faster than Division.

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

l Integer caluculations are faster than floating-point calculations.

l 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).

l

Lecture 4 (^) Slide 12 6.837 Fall '

Applications of Low-level Optimizations

Notice 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 fraction exceeded 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 '

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

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

Geometric Interpretation

fraction = 0.5; // Initial

fraction += m; // Increment

if 0.5 <= m <= 1 then fraction >= 1

Plot(1,1)

if 0 < m < 0.5 then fraction < 1

Plot(1,0)

fraction += m;

if (fraction >= 1) {

y = y + 1;

fraction -= 1;

}

Lecture 4 Slide 13a 6.837 Fall '

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

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 //

2dx(dy/dx)

and our test must be modified to reflect the new scaling

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

Lecture 4 (^) Slide 14 6.837 Fall '

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 2dy dx <<= 1; // dx is now 2dx raster.setPixel(pix, x0, y0); if (dx > dy) { int fraction = dy - (dx >> 1); // same as 2dy - dx while (x0 != x1) { if (fraction >= 0) { y0 += stepy; fraction -= dx; // same as fraction -= 2dx } 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 '

lineBresenham( ) Demonstration

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

The lineBresenham() method:

Does it work?

Lecture 4 (^) Slide 17 6.837 Fall '