Line and Curve Drawing Algorithms. Line Drawing x0x0 y0y0 x end y end.

  • Published on
    14-Dec-2015

  • View
    218

  • Download
    3

Transcript

  • Slide 1

Line and Curve Drawing Algorithms Slide 2 Line Drawing x0x0 y0y0 x end y end Slide 3 Line Drawing x0x0 y0y0 x end y end Slide 4 Line Drawing x0x0 y0y0 x end y end Slide 5 DDA Algorithm if |m|1 y k+1 = y k + 1 x k+1 = x k + 1/m x0x0 y0y0 x end y end x0x0 y0y0 x end y end Slide 6 DDA Algorithm #include inline int round (const float a) { return int (a + 0.5); } void lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs (dx) > fabs (dy)) steps = fabs (dx);/* |m|=1 */ xIncrement = float (dx) / float (steps); yIncrement = float (dy) / float (steps); setPixel (round (x), round (y)); for (k = 0; k < steps; k++) { x += xIncrement; y += yIncrement; setPixel (round (x), round (y)); } Slide 7 Bresenhams Line Algorithm xkxk ykyk x k+1 y k+1 y dudu dldl xkxk x k+1 ykyk y k+1 Slide 8 Bresenhams Line Algorithm #include /* Bresenham line-drawing procedure for |m| 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); } Slide 9 Circle Drawing Pythagorean Theorem: x 2 + y 2 = r 2 (x-x c ) 2 + (y-y c ) 2 = r 2 (x c -r) x (x c +r) y = y c r 2 - (x-x c ) 2 xcxc ycyc r (x, y) Slide 10 Circle Drawing change x change y Slide 11 Circle Drawing using polar coordinates x = x c + r. cos y = y c + r. sin change with step size 1/r r (x, y) (x c, y c ) Slide 12 Circle Drawing using polar coordinates x = x c + r. cos y = y c + r. sin change with step size 1/r use symmetry if >45 0 r (x, y) (x c, y c ) (x, y) (x c, y c ) 45 0 (y, x)(y, -x) (-x, y) Slide 13 Midpoint Circle Algorithm f(x,y) = x 2 + y 2 - r 2 0 if (x,y) is outside circle use symmetry if x>y xkxk x k+1 y k -1 ykyk y k - 1/2 Slide 14 Midpoint Circle Algorithm #include class scrPt { public: GLint x, y; }; void setPixel (GLint x, GLint y) { glBegin (GL_POINTS); glVertex2i (x, y); glEnd ( ); } void circleMidpoint (scrPt circCtr, GLint radius) { scrPt circPt; GLint p = 1 - radius; circPt.x = 0; circPt.y = radius; void circlePlotPoints (scrPt, scrPt); /* Plot the initial point in each circle quadrant. */ circlePlotPoints (circCtr, circPt); /* Calculate next points and plot in each octant. */ while (circPt.x < circPt.y) { circPt.x++; if (p < 0) p += 2 * circPt.x + 1; else { circPt.y--; p += 2 * (circPt.x - circPt.y) + 1; } circlePlotPoints (circCtr, circPt); } void circlePlotPoints (scrPt circCtr, scrPt circPt); { setPixel (circCtr.x + circPt.x, circCtr.y + circPt.y); setPixel (circCtr.x - circPt.x, circCtr.y + circPt.y); setPixel (circCtr.x + circPt.x, circCtr.y - circPt.y); setPixel (circCtr.x - circPt.x, circCtr.y - circPt.y); setPixel (circCtr.x + circPt.y, circCtr.y + circPt.x); setPixel (circCtr.x - circPt.y, circCtr.y + circPt.x); setPixel (circCtr.x + circPt.y, circCtr.y - circPt.x); setPixel (circCtr.x - circPt.y, circCtr.y - circPt.x); } Slide 15 OpenGL #include // (or others, depending on the system in use) void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glMatrixMode (GL_PROJECTION); // Set projection parameters. gluOrtho2D (0.0, 200.0, 0.0, 150.0); } void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); // Clear display window. glColor3f (0.0, 0.0, 1.0); // Set line segment color to blue. glBegin (GL_LINES); glVertex2i (180, 15); // Specify line-segment geometry. glVertex2i (10, 145); glEnd ( ); glFlush ( ); // Process all OpenGL routines as quickly as possible. } void main (int argc, char** argv) { glutInit (&argc, argv); // Initialize GLUT. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glutInitWindowPosition (50, 100); // Set top-left display-window position. glutInitWindowSize (400, 300); // Set display-window width and height. glutCreateWindow ("An Example OpenGL Program"); // Create display window. init ( ); // Execute initialization procedure. glutDisplayFunc (lineSegment); // Send graphics to display window. glutMainLoop ( ); // Display everything and wait. } Slide 16 OpenGL Point Functions glVertex*( ); * : 2, 3, 4 i (integer) s (short) f (float) d (double) Ex: glBegin(GL_POINTS); glVertex2i(50, 100); glEnd(); Ex: int p1[ ]={50, 100}; glBegin(GL_POINTS); glVertex2iv(p1); glEnd(); Slide 17 OpenGL Line Functions GL_LINES GL_LINE_STRIP GL_LINE_LOOP Ex: glBegin(GL_LINES); glVertex2iv(p1); glVertex2iv(p2); glEnd(); Slide 18 OpenGL glBegin(GL_LINES);GL_LINESGL_LINE_STRIP glVertex2iv(p1); glVertex2iv(p2); glVertex2iv(p3); glVertex2iv(p4); glVertex2iv(p5); glEnd(); GL_LINE_LOOP p1 p2 p3 p2 p4 p3 p5 p4 p5 Slide 19 Antialiasing 19 No Antialiasing With Antialiasing Ideal Slide 20 Antialiasing No AntialiasingWith Antialiasing Slide 21 Antialiasing Supersampling Count the number of subpixels that overlap the line path. Set the intensity proportional to this count. Slide 22 Antialiasing Supersampling Actual Screen Pixels 3x3 Virtual Pixels (255, 159, 159) Example 11 112 2 224 (255,255,255)(255,0,0)(255,255,255) (255,0,0)(255,255,255) (255,0,0) Slide 23 Antialiasing Area Sampling Line is treated as a rectangle. Calculate the overlap areas for pixels. Set intensity proportional to the overlap areas. 80%25% Slide 24 Antialiasing Pixel Sampling Micropositioning Electron beam is shifted 1/2, 1/4, 3/4 of a pixel diameter. Slide 25 Line Intensity differences Change the line drawing algorithm: For horizontal and vertical lines use the lowest intensity For 45 o lines use the highest intensity

Recommended

View more >