Using integer coordinates, you can divide the differences by the Greatest common divisor:
var gcd = Gcd(x2 - x1, y2 - y1, z2 - z1);
var stepX = (x2 - x1) / gcd;
var stepY = (y2 - y1) / gcd;
var stepZ = (z2 - z1) / gcd;
Some lines will only have two steps, since they can't be divided evenly in all three dimensions. (E.g. (0,0,0)-(17,19,23)
)
Alternativly, you could use Bresenham's line algorithm
public static IEnumerable<Point> GetPointsOnLine(int x0, int y0, int z0, int x1, int y1, int z1)
{
bool swapXY = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
bool swapXZ = Math.Abs(z1 - z0) > Math.Abs(x1 - x0);
if (swapXY)
{
int tmp;
tmp = x0; x0 = y0; y0 = tmp;
tmp = x1; x1 = y1; y1 = tmp;
}
if (swapXZ)
{
int tmp;
tmp = x0; x0 = z0; z0 = tmp;
tmp = x1; x1 = z1; z1 = tmp;
}
int deltaX = Math.Abs(x1 - x0);
int deltaY = Math.Abs(y1 - y0);
int deltaZ = Math.Abs(z1 - z0);
int driftXY = deltaX / 2;
int driftXZ = deltaX / 2;
int stepX = x0 <= x1 ? 1 : -1;
int stepY = y0 <= y1 ? 1 : -1;
int stepZ = z0 <= z1 ? 1 : -1;
int y = y0;
int z = z0;
for (int x = x0; x*stepX <= x1*stepX; x += stepX)
{
int xc = swapXY ? y : swapXZ ? z : x;
int yc = swapXY ? swapXZ ? z : x : y;
int zc = swapXZ ? x : z;
yield return new Point(xc, yc, zc);
driftXY -= deltaY;
if (driftXY < 0)
{
y += stepY;
driftXY += deltaX;
}
driftXZ -= deltaZ;
if (driftXZ < 0)
{
z += stepZ;
driftXZ += deltaX;
}
}
}
