Skip to content

Instantly share code, notes, and snippets.

@ClickerMonkey
Created September 2, 2014 15:34
Show Gist options
  • Select an option

  • Save ClickerMonkey/83f3eba7ac19eaf6155f to your computer and use it in GitHub Desktop.

Select an option

Save ClickerMonkey/83f3eba7ac19eaf6155f to your computer and use it in GitHub Desktop.
Interpolation between two points on a grid. The resulting points from iteration can be imagined as hitting all pixels on a screen that intersect a line that's drawn from any pixel to any pixel on the screen.
public class Veclerp
{
public int x, y;
private int x_inc, y_inc, dx, dy, error, n;
public void start( int x0, int y0, int x1, int y1 )
{
dx = Math.abs( x1 - x0 );
dy = Math.abs( y1 - y0 );
x = x0;
y = y0;
n = 1 + dx + dy;
x_inc = (x1 > x0) ? 1 : -1;
y_inc = (y1 > y0) ? 1 : -1;
error = dx - dy;
dx *= 2;
dy *= 2;
}
public boolean next()
{
if ( error > 0 )
{
x += x_inc;
error -= dy;
}
else
{
y += y_inc;
error += dx;
}
return --n > 0;
}
public boolean atEnd()
{
return n == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment