# Lecture 5 Rendering lines 1.Steps of line rendering 2.Scan-conversion for line segments 3.A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier.

## Presentation on theme: "Lecture 5 Rendering lines 1.Steps of line rendering 2.Scan-conversion for line segments 3.A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier."— Presentation transcript:

Lecture 5 Rendering lines 1.Steps of line rendering 2.Scan-conversion for line segments 3.A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier University 1

CP411 Computer Graphics 2 1. Line-Drawing Steps 1.Description of a line in WC 2.A line is projected to view plane and clipped by clipping window. It becomes a line segment, with two ends within the clipping window 3.Line segment is transformed to normalized coordinate system 4.Line segment is mapped to screen coordinates, then converted to integers in view port ordinate system of the display 5.A line segment in SC is a graphics output primitives. It needs to be converted to pixels in SC by scan-conversion or rasterization algorithm. i.e. to set up pixels in the frame buffer. Each pixel position (x, y) has a location in frame buffer setPixel(x, y) is to set the color value at pixel (x, y)

CP411 Computer Graphics Fall 2007 Wilfrid Laurier University 3 (x1,y1) (x2,y2) (x1,y1) (x2,y2)

CP411 Computer Graphics 4 Scan conversion From SC description to pixels by scan conversion –Transforming the continuous into this discrete by sampling. Can never be ideal. –The best we can do is a discrete approximation of an ideal line.

CP411 Computer Graphics 5 The Basic idea of drawing lines Let (x0, y0) and (x1, y1) be the screen coordinates of the two end points of a line segment. x0  x1 Step through the x-coordinate from x0 to x1. Set the pixels closest to the line. (x1,y1) (x2,y2)

CP411 Computer Graphics 6 Important line qualities Continuous appearance Uniform thickness and brightness Turn on the pixels nearest the ideal line How fast is the line generated Preferred algorithms –Use integers rater than floating point –Use addition, subtraction rather than multiplication and division –Use iteration or recurrence –Use increment

CP411 Computer Graphics 7 Simple Line Algorithm I int x0=, y0=, x1=, y1= ; int dx = x1 - x0; int dy = y1 - y0; setPixel(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); setPixel(x0, y0); } y = m x + b m = (y1-y0)/(x1-x0) x = x0, x0+1, …, x1 y = mx + b

CP411 Computer Graphics 8 The above algorithm only works well when the angle between the line and the x-axis, , is less than 45 o The left figure is resulted for a line with  = 70 o. The pixels on the line are too sparsely set. A more desirable one is shown on the right.  = 70 o

CP411 Computer Graphics 9 Algorithm I improvement int x0 =, y0=,x1=, y1=..; int dx = x1 - x0; int dy = y1 - y0; setPixel(x0, y0); if (Math.abs(dx) > Math.abs(dy)) { float m = (float) dy / (float) dx; float b = y0 - m*x0; dx = (dx < 0) ? -1 : 1; while (x0 != x1) { x0 += dx; setPixel(x0, Math.round(m*x0 + b)); } } else if (dy != 0) { float m = (float) dx / (float) dy; float b = x0 - m*y0; dy = (dy < 0) ? -1 : 1; while (y0 != y1) { y0 += dy; setPixel(Math.round(m*y0 + b), y0); } }

CP411 Computer Graphics 10 Algorithm II – by increment, remove multiplication int x0 = …, y0 = …, x1 = …, y1 = …; int x = x0; float y = y0, dy = (y1 – y0) / (x1 – x0); for (x = x0; x  x1; ++x) { setPixel( x, round(y) ); y = y + dy; } Round operation is slow as well dx=1 d y

CP411 Computer Graphics 11 Algorithm III error is the distance between the centre of a pixel and the line) int x0 = …, y0 = …, x 1 …, y1 = …; int x, y = y0; float error = 0.0, dy = (y1 – y0) / (x1 – x0); for (x = x0; x  x1; ++x) { setPixel( x, y ); error = error + dy; if (error > 0.5) { ++y; error = error - 1.0; } }

CP411 Computer Graphics 12 Algorithm III _ improvement The floating point arithmetic can be entirely eliminated by scaling up error and  y by 2(x2-x1) int x0 = …, x =1 …, y0 = …, y1 = …; int x, y = y0, dx = 2*(x1 – x0), d y = 2*(y1 – y0), error = 0; for (x = x1; x  x1; ++x) { setPixel( x, y ); error = error + d y; if (error > dx/2) { ++y; error = error - dx; } }

CP411 Computer Graphics 13 Algorithm III _improvement The program runs even faster if we offset error by dx/2 int x0 = …, x1 = …, y0 = …, y1 = …; int x, y = y0, dx = 2*(x1 – x0), d y = 2*(y1 – y0), error = -dx / 2; for (x = x0; x  x1; ++x) { setPixel( x, y ); error = error + d y; if (error > 0) { ++y; error = error - dx; } }

CP411 Computer Graphics 14 Algorithm V The Bresenham’s line drawing algorithm Combine the operations error = error + d y and error = error – dx into one when error > 0 ; int x0 = …, x1 = …, y0 = …, y1 = …; int x, y = y0, dx = 2*(x1 – x0), d y = 2*(y1 – y0), error = -dx / 2 - d y, dymx = d y - dx; for (x = x1; x  x2; ++x) { setPixel( x, y ); if (error < 0) error = error + d y; else { ++y; error = error + dymx; } }

CP411 Computer Graphics 15 void lineBres (int x0, int y0, int xEnd, int yEnd) { int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0); int p = 2 * dy - dx; int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx); int x, y; /* Determine which endpoint to use as start position. */ if (x0 > xEnd) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; } setPixel (x, y); while (x < xEnd) { x++; if (p < 0) p += twoDy; else { y++; p += twoDyMinusDx; } setPixel (x, y); }

Download ppt "Lecture 5 Rendering lines 1.Steps of line rendering 2.Scan-conversion for line segments 3.A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier."

Similar presentations