























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























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]
Lecture 4 (^) Slide 1 6.837 Fall '
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 '
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.
Lecture 4 (^) Slide 7 6.837 Fall '
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 '
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.
Lecture 4 (^) Slide 10 6.837 Fall '
Lecture 4 (^) Slide 12 6.837 Fall '
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]
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 '
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.
Lecture 4 (^) Slide 17 6.837 Fall '