[ Contact ] [ Links ] [ Previous : 8 / 20 : Optimize Inner Loops ] [ Up ] [ Next : 10 / 20 : Low-Level Optimizations ]

Modified Algorithm : DDA

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 = 0.5f; // 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);
		}
	}
}
 

 

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

 


See the "Links" link above to find out the sources of the proposed informations
Pascal Vuylsteker / eScience / Computer Science / ANU
Last modified: 20/4/2004
TOC - Print
Send your comments at :
<Hugh.Fisher@anu.edu.au>