just a mesh up of
http://en.wikipedia.org/wiki/Xiaolin_Wu’s_line_algorithm and
http://en.wikipedia.org/wiki/Bresenham’s_line_algorithm
and the “super cover” like written here : http://lifc.univ-fcomte.fr/home/~ededu/projects/bresenham/
replace java.Math with your fav. math-utils. dont really remember where i picked up that code, there’s stuff like that everywhere when you google a bit. anyway, i think this version is very simple, easy to understand and not slow.
private void coversuper(float from_x,
float from_y,
float to_x,
float to_y,
float cell_width,
float cell_height)
{
float x0 = from_x/cell_width;
float y0 = from_y/cell_height;
float x1 = to_x/cell_width;
float y1 = to_y/cell_height;
float dx = Math.abs(x1 - x0);
float dy = Math.abs(y1 - y0);
int x = ipart(x0);
int y = ipart(y0);
int num = 1;
int sx,sy;
float err;
if(dx == 0)
{
sx = 0;
err = Float.POSITIVE_INFINITY;
}
else if(x1 > x0)
{
sx = 1;
num += ipart(x1) - x;
err = rfpart(x0)*dy;
}
else
{
sx = -1;
num += x - ipart(x1);
err = fpart(x0)*dy;
}
if(dy == 0)
{
sy = 0;
err = Float.NEGATIVE_INFINITY;
}
else if(y1 > y0)
{
sy = 1;
num += ipart(y1) - y;
err -= rfpart(y0)*dx;
}
else
{
sy = -1;
num += y - ipart(y1);
err -= fpart(y0)*dx;
}
// number of square to be plotted : num
while(true)
{
plot(x,y);
if(--num == 0) break;
if(err > 0)
{
err -= dx;
y += sy;
}
else
{
err += dy;
x += sx;
}
}
}
the helper methods used up there :
private int ipart(float x)
{
return (int)Math.floor(x);
}
private float fpart(float x)
{
return x - ipart(x);
}
private float rfpart(float x)
{
return 1.0f - fpart(x);
}
and of course the most important (line 55) :
private void plot(int x,int y)
{
// glColor4f(0.1f,0.5f,1.0f,0.5f); // etc
// draw a squre or something
// maybe rectangle(x*cellSize_x, y*cellSize_y, cellSize_x, cellSize_y);
// you get the idea
}
shoud make something like :