Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't this midpoint line algorithm draw vertical lines (or diagonals that approach the vertical)?
    primarykey
    data
    text
    <p>I implemented this algorithm I found online, and it draws horizontal lines and half of all diagonals just fine. But as the diagonal line passes the "halfway" mark to becoming vertical, it doesn't update y and only draws the line with a smaller slope. It does draw it all the way to x2, but draws a line sloped to a different y2 coordinate. And if you try to draw a vertical line, nothing is drawn. Can anyone find the bug?</p> <pre><code>void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); } void midPoint(int x1, int y1, int x2, int y2) { if (x1 &gt; x2) { midPoint(x2, y2, x1, y1); return; } int slope; int dx, dy, d, x, y; dx = x2 - x1; dy = y2 - y1; d = dx - 2 * dy; y = y1; if (dy &lt; 0) { slope = -1; dy = -dy; } else { slope = 1; } for (x = x1; x &lt; x2; x++) { glBegin(GL_POINTS); glVertex2f(x, y); if (d &lt;= 0) { d += 2 * dx - 2 * dy; y += slope; } else { d += -2 * dy; } glEnd(); } } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 0.0, 0.0); midPoint(10, 10, 110, 210); glColor3f(0.0, 1.0, 0.0); midPoint(10, 10, 210, 110); glColor3f(1.0, 1.0, 0.0); midPoint(210, 10, 10, 110); glFlush(); } int main(int argc, char *argv[]) { glutInit(&amp;argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(50, 50); glutInitWindowSize(500, 500); glutCreateWindow("Bresenham\'s midpoint line algorithm"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload