Bresenham’s algorithm for drawing a line with a slope m having an absolute value
ID: 3683477 • Letter: B
Question
Bresenham’s algorithm for drawing a line with a slope m having an absolute value less than 1 (that is, |m| < 1.0) is given in the text on pages 143-4 and is also displayed below. Assume we want to modify the algorithm so it performs line stippling in the same way as glLineStipple. Modify the function so it accepts two additional parameters (repeatFactor and pattern, the same as those used with glLineStipple), and so it will display the appropriately-stippled line. You do not need to actually compile the code; just provide a display of the modified code. #include #include /* Bresenham line-drawing procedure for |m| < 1.0. */ 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); } }
Explanation / Answer
Hi below i have wriiten the sample function for your reference ,
/* Invoke a procedure here to draw coordinate axes. */
glEnable (GL_LINE_STIPPLE);
/* Input first set of (x, y) data values. */
glLineStipple (1, 0x1C47); // Plot a dash-dot, standard-width polyline.
linePlot (dataPts);
/* Input second set of (x, y) data values. */
glLineStipple (1, 0x00FF); // Plot a dashed, double-width polyline.
glLineWidth (2.0);
linePlot (dataPts);
/* Input third set of (x, y) data values. */
glLineStipple (1, 0x0101); // Plot a dotted, triple-width polyline.
glLineWidth (3.0);
linePlot (dataPts);
glDisable (GL_LINE_STIPPLE);