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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 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?