Modified Algorithm

This line drawing method is called a Digital Differential Analyzer or DDA for short.
    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 5 Slide 9 6.837 Fall '98

Incorporating these changes into our previous lineImproved( ) algorithm gives the following result:

    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);
            }
        }
    }

This method of line drawing is called a Digital Differential Analyzer or DDA for short.