Fastest method of referencing all pixels in a line?

Currently, to find all pixels in a line I am using Bresenham’s line algorithm. I converted the method from pseudo-code (on wikipedia) to java, but it’s rather slow. Here is the code:

	private void getPixelsInLine(int x0, int y0, int x1, int y1) {
		boolean steep = Math.abs(y1 - y0) > Math.abs(x1 - x0);
		if (steep) {
			int tmp = x0;
			x0 = y0;
			y0 = tmp;
			tmp = x1;
			x1 = y1;
			y1 = tmp;
		}
		if (x0 > x1) {
			int tmp = x0;
			x0 = x1;
			x1 = tmp;
			tmp = y0;
			y0 = y1;
			y1 = tmp;
		}
		int deltax = x1 - x0;
		int deltay = Math.abs(y1 - y0);
		int error = deltax / 2;
		int ystep = -1;
		int y = y0;
		if (y0 < y1)
			ystep = 1;
		for (int x = x0; x <= x1; x++) {
			if (steep)
				mark(y, x);
			else
				mark(x, y);
			error -= deltay;
			if (error < 0) {
				y += ystep;
				error += deltax;
			}
		}
	}

I was wondering if there is a faster method of obtaining all pixels in a line/better implementation?

That should be very fast. Fast enough that you could do it thousands of times a second and not worry about at all.

Unless you’re doing something hideously slow in the “mark” function. If this were actually slow, that’s where I’d look…

You’re leaving out the most important piece of information: what do you really want to do?

yes it is fast, it seems the problem is linked to the other thread I posted (with the images)

int error = deltax / 2;

Could be:

int error = deltax >>1;

I have allways thinked do Java found these kind of bit shift optimizations automatically?

Not the same if negative.

int error = deltax >>>1;


see post above yours


  int i = -1;
  int r0 = i/2;
  int r1 = i>>1;

Shifting (alone) isnt’t the same for negative. The result depends on the sub-bit, so compilers won’t convert division into a shift unless it can statically determine that all values are positive or zero.