CoolFace
Apppublic

Omnibus-archive/sim-1

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
calculatePathBetweenCells.js29 linesDownload Raw Back to utils
1export default function (start, end) {2  let dx = end.x - start.x;3  let dy = end.y - start.y;4 5  let nx = Math.abs(dx);6  let ny = Math.abs(dy);7 8  let sx = dx > 0? 1 : -1;9  let sy = dy > 0? 1 : -1;10 11  let point = { x: start.x, y: start.y };12  let cells = [];13 14  cells.push({ x: start.x, y: start.y });15 16  for (let ix = 0, iy = 0; ix < nx || iy < ny;) {17    if ((0.5 + ix) / nx < (0.5 + iy) / ny) {18      point.x += sx;19      ix++;20    } else {21      point.y += sy;22      iy++;23    }24    25    cells.push({ x: point.x, y: point.y });26  }27 28  return cells;29}