microboard-temp 0.5.76 → 0.5.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36710,7 +36710,7 @@ function getNeighbors(node2, grid, obstacles) {
36710
36710
  { x: node2.xGrid, y: node2.yGrid + 1 }
36711
36711
  ];
36712
36712
  for (const pos of potentialNeighbors) {
36713
- if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0) {
36713
+ if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0 && grid[pos.x] && grid[pos.x][pos.y]) {
36714
36714
  const newPoint = grid[pos.x][pos.y];
36715
36715
  if (newPoint && !obstacles.some((obstacle) => obstacle.isAlmostInside(newPoint, conf.CONNECTOR_ITEM_OFFSET - 1))) {
36716
36716
  neighbors.push({
@@ -36727,34 +36727,6 @@ function getNeighbors(node2, grid, obstacles) {
36727
36727
  }
36728
36728
  return neighbors;
36729
36729
  }
36730
- function findCenterLine(grid, start, end, middle) {
36731
- const centerLine = [];
36732
- const middlePoint = middle ? middle : new Point((start.x + end.x) / 2, (start.y + end.y) / 2);
36733
- const min2 = new Point(Math.min(start.x, end.x), Math.min(start.y, end.y));
36734
- const max2 = new Point(Math.max(start.x, end.x), Math.max(start.y, end.y));
36735
- const width = max2.x - min2.x;
36736
- const height = max2.y - min2.y;
36737
- if (start.pointType !== "Board" && end.pointType !== "Board") {
36738
- const isInGrid = grid.some((row2) => row2.some((point5) => point5.barelyEqual(middlePoint)));
36739
- return isInGrid ? [middlePoint] : [];
36740
- }
36741
- if (width > height) {
36742
- const centerIdx = grid.findIndex((row2) => row2[0].x === middlePoint.x || Math.abs(row2[0].x - middlePoint.x) < 0.01);
36743
- for (let y = 0;y < grid[0].length && centerIdx !== -1; y++) {
36744
- if (grid[centerIdx][y] && grid[centerIdx][y].x >= min2.x - 0.01 && grid[centerIdx][y].x <= max2.x + 0.01 && grid[centerIdx][y].y >= min2.y - 0.01 && grid[centerIdx][y].y <= max2.y + 0.01) {
36745
- centerLine.push(grid[centerIdx][y]);
36746
- }
36747
- }
36748
- } else {
36749
- const centerIdx = grid[0].findIndex((point5) => point5.y === middlePoint.y || Math.abs(point5.y - middlePoint.y) < 0.01);
36750
- for (let x = 0;x < grid.length && centerIdx !== -1; x++) {
36751
- if (grid[x][centerIdx].x >= min2.x - 0.01 && grid[x][centerIdx].x <= max2.x + 0.01 && grid[x][centerIdx].y >= min2.y - 0.01 && grid[x][centerIdx].y <= max2.y + 0.01) {
36752
- centerLine.push(grid[x][centerIdx]);
36753
- }
36754
- }
36755
- }
36756
- return centerLine;
36757
- }
36758
36730
  function createGrid(start, end, toVisitPoints = []) {
36759
36731
  const startDir = getPointerDirection(start);
36760
36732
  const endDir = getPointerDirection(end);
@@ -36805,18 +36777,27 @@ function createGrid(start, end, toVisitPoints = []) {
36805
36777
  middlePoint: middle
36806
36778
  };
36807
36779
  }
36808
- function findPath(start, end, grid, obstacles, newStart, newEnd) {
36809
- const startIdx = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
36810
- const endIdx = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
36811
- if (startIdx === -1 || endIdx === -1) {
36812
- throw new Error("Start or end point not found in the grid");
36780
+ function findPath(start, end, grid, obstacles, existingPath, newStart, newEnd) {
36781
+ const startRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
36782
+ if (startRowIndex === -1) {
36783
+ throw new Error("Start point not found in the grid row");
36784
+ }
36785
+ const startPointIndex = grid[startRowIndex].findIndex((point5) => point5.barelyEqual(start));
36786
+ if (startPointIndex === -1) {
36787
+ throw new Error("Start point not found in the grid column");
36788
+ }
36789
+ const endRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
36790
+ if (endRowIndex === -1) {
36791
+ throw new Error("End point not found in the grid row");
36792
+ }
36793
+ const endPointIndex = grid[endRowIndex].findIndex((point5) => point5.barelyEqual(end));
36794
+ if (endPointIndex === -1) {
36795
+ throw new Error("End point not found in the grid column");
36813
36796
  }
36814
- const startPointIdx = grid[startIdx].findIndex((point5) => point5.barelyEqual(start));
36815
- const endPointIdx = grid[endIdx].findIndex((point5) => point5.barelyEqual(end));
36816
36797
  const endNode = {
36817
36798
  point: end,
36818
- xGrid: endIdx,
36819
- yGrid: endPointIdx,
36799
+ xGrid: endRowIndex,
36800
+ yGrid: endPointIndex,
36820
36801
  costSoFar: 0,
36821
36802
  heuristic: 0,
36822
36803
  toFinish: 0
@@ -36824,53 +36805,72 @@ function findPath(start, end, grid, obstacles, newStart, newEnd) {
36824
36805
  const startNode = {
36825
36806
  point: start,
36826
36807
  costSoFar: 0,
36827
- heuristic: heuristic({ point: start, xGrid: startIdx, yGrid: startPointIdx }, endNode),
36828
- toFinish: heuristic({ point: start, xGrid: startIdx, yGrid: startPointIdx }, endNode),
36829
- xGrid: startIdx,
36830
- yGrid: startPointIdx
36808
+ heuristic: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
36809
+ toFinish: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
36810
+ xGrid: startRowIndex,
36811
+ yGrid: startPointIndex
36831
36812
  };
36832
36813
  const openSet = [startNode];
36833
36814
  const closedSet = new Set;
36834
36815
  while (openSet.length > 0) {
36835
36816
  openSet.sort((aa, bb) => aa.toFinish - bb.toFinish);
36836
36817
  const current = openSet.shift();
36818
+ const currentKey = `${current.point.x},${current.point.y}`;
36837
36819
  if (current.point.barelyEqual(end)) {
36838
36820
  const path2 = reconstructPath(current);
36839
36821
  return path2;
36840
36822
  }
36841
- closedSet.add(current.point);
36823
+ closedSet.add(currentKey);
36842
36824
  const neighbors = getNeighbors(current, grid, obstacles);
36843
36825
  for (const neighbor of neighbors) {
36844
- if (closedSet.has(neighbor.point)) {
36826
+ const neighborKey = `${neighbor.point.x},${neighbor.point.y}`;
36827
+ if (closedSet.has(neighborKey) || existingPath.has(neighborKey) && !neighbor.point.barelyEqual(end)) {
36845
36828
  continue;
36846
36829
  }
36847
36830
  const extraCost = isChangingDirection(current, neighbor, newStart, newEnd);
36848
- const tentativeCost = current.costSoFar + 1;
36849
- if (!openSet.some((node2) => node2.point.barelyEqual(neighbor.point) && node2.costSoFar <= tentativeCost)) {
36850
- neighbor.costSoFar = tentativeCost + extraCost;
36851
- neighbor.heuristic = heuristic(neighbor, endNode);
36852
- neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
36853
- openSet.push(neighbor);
36831
+ const pathOverlapCost = existingPath.has(neighborKey) ? 1000 : 0;
36832
+ const tentativeCost = current.costSoFar + 1 + pathOverlapCost;
36833
+ let existingNodeInOpenSet = openSet.find((node2) => node2.point.barelyEqual(neighbor.point));
36834
+ if (!existingNodeInOpenSet || tentativeCost < existingNodeInOpenSet.costSoFar) {
36835
+ if (existingNodeInOpenSet) {
36836
+ existingNodeInOpenSet.costSoFar = tentativeCost + extraCost;
36837
+ existingNodeInOpenSet.heuristic = heuristic(neighbor, endNode);
36838
+ existingNodeInOpenSet.toFinish = existingNodeInOpenSet.costSoFar + existingNodeInOpenSet.heuristic;
36839
+ existingNodeInOpenSet.parent = current;
36840
+ } else {
36841
+ neighbor.costSoFar = tentativeCost + extraCost;
36842
+ neighbor.heuristic = heuristic(neighbor, endNode);
36843
+ neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
36844
+ openSet.push(neighbor);
36845
+ }
36854
36846
  }
36855
36847
  }
36856
36848
  }
36857
36849
  return;
36858
36850
  }
36859
36851
  function findPathPoints(points, grid, obstacles, newStart, newEnd) {
36860
- const pathPoints = [];
36852
+ const finalPath = [];
36853
+ const existingPathSegments = new Set;
36854
+ if (points.length > 0) {
36855
+ finalPath.push(points[0]);
36856
+ const startKey = `${points[0].x},${points[0].y}`;
36857
+ existingPathSegments.add(startKey);
36858
+ }
36861
36859
  for (let i = 0;i < points.length - 1; i += 1) {
36862
- const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, newStart, newEnd);
36863
- if (segmentPath) {
36864
- pathPoints.push(...segmentPath.slice(0, -1));
36860
+ const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, existingPathSegments, newStart, newEnd);
36861
+ if (segmentPath && segmentPath.length > 0) {
36862
+ for (let j = 1;j < segmentPath.length; j++) {
36863
+ const point5 = segmentPath[j];
36864
+ const key = `${point5.x},${point5.y}`;
36865
+ finalPath.push(point5);
36866
+ existingPathSegments.add(key);
36867
+ }
36865
36868
  } else {
36866
36869
  points.splice(i + 1, 1);
36867
36870
  i--;
36868
36871
  }
36869
36872
  }
36870
- if (pathPoints.length !== 0) {
36871
- pathPoints.push(points[points.length - 1]);
36872
- }
36873
- return pathPoints;
36873
+ return finalPath;
36874
36874
  }
36875
36875
  function reducePoints(points) {
36876
36876
  const uniquePoints = new Map;
@@ -36881,6 +36881,12 @@ function reducePoints(points) {
36881
36881
  if (uniquePoints.has(key)) {
36882
36882
  const loopStartIndex = uniquePoints.get(key);
36883
36883
  result.splice(loopStartIndex + 1);
36884
+ const removedPoints = points.slice(loopStartIndex + 1, i + 1);
36885
+ removedPoints.forEach((p3) => {
36886
+ uniquePoints.delete(`${p3.x},${p3.y}`);
36887
+ });
36888
+ uniquePoints.set(key, result.length);
36889
+ result.push(point5);
36884
36890
  } else {
36885
36891
  uniquePoints.set(key, result.length);
36886
36892
  result.push(point5);
@@ -36890,19 +36896,32 @@ function reducePoints(points) {
36890
36896
  }
36891
36897
  function getLines(pathPoints) {
36892
36898
  const lines = [];
36899
+ if (pathPoints.length < 2) {
36900
+ return [];
36901
+ }
36893
36902
  const reducedPoints = reducePoints(pathPoints);
36903
+ if (reducedPoints.length < 2) {
36904
+ return [];
36905
+ }
36894
36906
  let startPoint = reducedPoints[0];
36895
- for (let i = 1;i < reducedPoints.length - 1; i += 1) {
36907
+ for (let i = 1;i < reducedPoints.length; i += 1) {
36896
36908
  const prevPoint = reducedPoints[i - 1];
36897
36909
  const currPoint = reducedPoints[i];
36898
- const nextPoint = reducedPoints[i + 1];
36899
- if (prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
36910
+ const nextPoint = i + 1 < reducedPoints.length ? reducedPoints[i + 1] : null;
36911
+ if (!nextPoint || prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
36900
36912
  lines.push(new Line(startPoint, currPoint));
36901
36913
  startPoint = currPoint;
36902
36914
  }
36903
36915
  }
36904
- if (lines.length > 0) {
36905
- lines.push(new Line(startPoint, pathPoints[pathPoints.length - 1]));
36916
+ if (lines.length === 0 && reducedPoints.length > 1) {
36917
+ lines.push(new Line(reducedPoints[0], reducedPoints[reducedPoints.length - 1]));
36918
+ } else if (lines.length > 0) {
36919
+ const lastLine = lines[lines.length - 1];
36920
+ const lastPointInLines = lastLine.getEndPoint();
36921
+ const lastPointInReduced = reducedPoints[reducedPoints.length - 1];
36922
+ if (!lastPointInLines.barelyEqual(lastPointInReduced)) {
36923
+ lines.push(new Line(lastPointInLines, lastPointInReduced));
36924
+ }
36906
36925
  }
36907
36926
  return lines;
36908
36927
  }
@@ -36916,16 +36935,10 @@ function reconstructPath(node2) {
36916
36935
  return path2.reverse();
36917
36936
  }
36918
36937
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
36919
- const { grid, newStart, newEnd, middlePoint } = createGrid(start, end, toVisitPoints);
36920
- const startPoint = newStart ? newStart : start;
36921
- const endPoint = newEnd ? newEnd : end;
36922
- const centerLine = findCenterLine(grid, startPoint, endPoint, middlePoint);
36923
- const adjustedCenterLine = centerLine.length > 0 ? startPoint.getDistance(centerLine[0]) < startPoint.getDistance(centerLine[centerLine.length - 1]) ? centerLine : centerLine.reverse() : centerLine;
36924
- const points = [
36925
- startPoint,
36926
- ...toVisitPoints.length > 0 ? toVisitPoints : adjustedCenterLine,
36927
- endPoint
36928
- ];
36938
+ const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
36939
+ const startPoint = newStart || start;
36940
+ const endPoint = newEnd || end;
36941
+ const points = [startPoint, ...toVisitPoints, endPoint];
36929
36942
  const pathPoints = findPathPoints(points, grid, obstacles, newStart, newEnd);
36930
36943
  return {
36931
36944
  lines: getLines(pathPoints),
package/dist/cjs/index.js CHANGED
@@ -36710,7 +36710,7 @@ function getNeighbors(node2, grid, obstacles) {
36710
36710
  { x: node2.xGrid, y: node2.yGrid + 1 }
36711
36711
  ];
36712
36712
  for (const pos of potentialNeighbors) {
36713
- if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0) {
36713
+ if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0 && grid[pos.x] && grid[pos.x][pos.y]) {
36714
36714
  const newPoint = grid[pos.x][pos.y];
36715
36715
  if (newPoint && !obstacles.some((obstacle) => obstacle.isAlmostInside(newPoint, conf.CONNECTOR_ITEM_OFFSET - 1))) {
36716
36716
  neighbors.push({
@@ -36727,34 +36727,6 @@ function getNeighbors(node2, grid, obstacles) {
36727
36727
  }
36728
36728
  return neighbors;
36729
36729
  }
36730
- function findCenterLine(grid, start, end, middle) {
36731
- const centerLine = [];
36732
- const middlePoint = middle ? middle : new Point((start.x + end.x) / 2, (start.y + end.y) / 2);
36733
- const min2 = new Point(Math.min(start.x, end.x), Math.min(start.y, end.y));
36734
- const max2 = new Point(Math.max(start.x, end.x), Math.max(start.y, end.y));
36735
- const width = max2.x - min2.x;
36736
- const height = max2.y - min2.y;
36737
- if (start.pointType !== "Board" && end.pointType !== "Board") {
36738
- const isInGrid = grid.some((row2) => row2.some((point5) => point5.barelyEqual(middlePoint)));
36739
- return isInGrid ? [middlePoint] : [];
36740
- }
36741
- if (width > height) {
36742
- const centerIdx = grid.findIndex((row2) => row2[0].x === middlePoint.x || Math.abs(row2[0].x - middlePoint.x) < 0.01);
36743
- for (let y = 0;y < grid[0].length && centerIdx !== -1; y++) {
36744
- if (grid[centerIdx][y] && grid[centerIdx][y].x >= min2.x - 0.01 && grid[centerIdx][y].x <= max2.x + 0.01 && grid[centerIdx][y].y >= min2.y - 0.01 && grid[centerIdx][y].y <= max2.y + 0.01) {
36745
- centerLine.push(grid[centerIdx][y]);
36746
- }
36747
- }
36748
- } else {
36749
- const centerIdx = grid[0].findIndex((point5) => point5.y === middlePoint.y || Math.abs(point5.y - middlePoint.y) < 0.01);
36750
- for (let x = 0;x < grid.length && centerIdx !== -1; x++) {
36751
- if (grid[x][centerIdx].x >= min2.x - 0.01 && grid[x][centerIdx].x <= max2.x + 0.01 && grid[x][centerIdx].y >= min2.y - 0.01 && grid[x][centerIdx].y <= max2.y + 0.01) {
36752
- centerLine.push(grid[x][centerIdx]);
36753
- }
36754
- }
36755
- }
36756
- return centerLine;
36757
- }
36758
36730
  function createGrid(start, end, toVisitPoints = []) {
36759
36731
  const startDir = getPointerDirection(start);
36760
36732
  const endDir = getPointerDirection(end);
@@ -36805,18 +36777,27 @@ function createGrid(start, end, toVisitPoints = []) {
36805
36777
  middlePoint: middle
36806
36778
  };
36807
36779
  }
36808
- function findPath(start, end, grid, obstacles, newStart, newEnd) {
36809
- const startIdx = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
36810
- const endIdx = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
36811
- if (startIdx === -1 || endIdx === -1) {
36812
- throw new Error("Start or end point not found in the grid");
36780
+ function findPath(start, end, grid, obstacles, existingPath, newStart, newEnd) {
36781
+ const startRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
36782
+ if (startRowIndex === -1) {
36783
+ throw new Error("Start point not found in the grid row");
36784
+ }
36785
+ const startPointIndex = grid[startRowIndex].findIndex((point5) => point5.barelyEqual(start));
36786
+ if (startPointIndex === -1) {
36787
+ throw new Error("Start point not found in the grid column");
36788
+ }
36789
+ const endRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
36790
+ if (endRowIndex === -1) {
36791
+ throw new Error("End point not found in the grid row");
36792
+ }
36793
+ const endPointIndex = grid[endRowIndex].findIndex((point5) => point5.barelyEqual(end));
36794
+ if (endPointIndex === -1) {
36795
+ throw new Error("End point not found in the grid column");
36813
36796
  }
36814
- const startPointIdx = grid[startIdx].findIndex((point5) => point5.barelyEqual(start));
36815
- const endPointIdx = grid[endIdx].findIndex((point5) => point5.barelyEqual(end));
36816
36797
  const endNode = {
36817
36798
  point: end,
36818
- xGrid: endIdx,
36819
- yGrid: endPointIdx,
36799
+ xGrid: endRowIndex,
36800
+ yGrid: endPointIndex,
36820
36801
  costSoFar: 0,
36821
36802
  heuristic: 0,
36822
36803
  toFinish: 0
@@ -36824,53 +36805,72 @@ function findPath(start, end, grid, obstacles, newStart, newEnd) {
36824
36805
  const startNode = {
36825
36806
  point: start,
36826
36807
  costSoFar: 0,
36827
- heuristic: heuristic({ point: start, xGrid: startIdx, yGrid: startPointIdx }, endNode),
36828
- toFinish: heuristic({ point: start, xGrid: startIdx, yGrid: startPointIdx }, endNode),
36829
- xGrid: startIdx,
36830
- yGrid: startPointIdx
36808
+ heuristic: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
36809
+ toFinish: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
36810
+ xGrid: startRowIndex,
36811
+ yGrid: startPointIndex
36831
36812
  };
36832
36813
  const openSet = [startNode];
36833
36814
  const closedSet = new Set;
36834
36815
  while (openSet.length > 0) {
36835
36816
  openSet.sort((aa, bb) => aa.toFinish - bb.toFinish);
36836
36817
  const current = openSet.shift();
36818
+ const currentKey = `${current.point.x},${current.point.y}`;
36837
36819
  if (current.point.barelyEqual(end)) {
36838
36820
  const path2 = reconstructPath(current);
36839
36821
  return path2;
36840
36822
  }
36841
- closedSet.add(current.point);
36823
+ closedSet.add(currentKey);
36842
36824
  const neighbors = getNeighbors(current, grid, obstacles);
36843
36825
  for (const neighbor of neighbors) {
36844
- if (closedSet.has(neighbor.point)) {
36826
+ const neighborKey = `${neighbor.point.x},${neighbor.point.y}`;
36827
+ if (closedSet.has(neighborKey) || existingPath.has(neighborKey) && !neighbor.point.barelyEqual(end)) {
36845
36828
  continue;
36846
36829
  }
36847
36830
  const extraCost = isChangingDirection(current, neighbor, newStart, newEnd);
36848
- const tentativeCost = current.costSoFar + 1;
36849
- if (!openSet.some((node2) => node2.point.barelyEqual(neighbor.point) && node2.costSoFar <= tentativeCost)) {
36850
- neighbor.costSoFar = tentativeCost + extraCost;
36851
- neighbor.heuristic = heuristic(neighbor, endNode);
36852
- neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
36853
- openSet.push(neighbor);
36831
+ const pathOverlapCost = existingPath.has(neighborKey) ? 1000 : 0;
36832
+ const tentativeCost = current.costSoFar + 1 + pathOverlapCost;
36833
+ let existingNodeInOpenSet = openSet.find((node2) => node2.point.barelyEqual(neighbor.point));
36834
+ if (!existingNodeInOpenSet || tentativeCost < existingNodeInOpenSet.costSoFar) {
36835
+ if (existingNodeInOpenSet) {
36836
+ existingNodeInOpenSet.costSoFar = tentativeCost + extraCost;
36837
+ existingNodeInOpenSet.heuristic = heuristic(neighbor, endNode);
36838
+ existingNodeInOpenSet.toFinish = existingNodeInOpenSet.costSoFar + existingNodeInOpenSet.heuristic;
36839
+ existingNodeInOpenSet.parent = current;
36840
+ } else {
36841
+ neighbor.costSoFar = tentativeCost + extraCost;
36842
+ neighbor.heuristic = heuristic(neighbor, endNode);
36843
+ neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
36844
+ openSet.push(neighbor);
36845
+ }
36854
36846
  }
36855
36847
  }
36856
36848
  }
36857
36849
  return;
36858
36850
  }
36859
36851
  function findPathPoints(points, grid, obstacles, newStart, newEnd) {
36860
- const pathPoints = [];
36852
+ const finalPath = [];
36853
+ const existingPathSegments = new Set;
36854
+ if (points.length > 0) {
36855
+ finalPath.push(points[0]);
36856
+ const startKey = `${points[0].x},${points[0].y}`;
36857
+ existingPathSegments.add(startKey);
36858
+ }
36861
36859
  for (let i = 0;i < points.length - 1; i += 1) {
36862
- const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, newStart, newEnd);
36863
- if (segmentPath) {
36864
- pathPoints.push(...segmentPath.slice(0, -1));
36860
+ const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, existingPathSegments, newStart, newEnd);
36861
+ if (segmentPath && segmentPath.length > 0) {
36862
+ for (let j = 1;j < segmentPath.length; j++) {
36863
+ const point5 = segmentPath[j];
36864
+ const key = `${point5.x},${point5.y}`;
36865
+ finalPath.push(point5);
36866
+ existingPathSegments.add(key);
36867
+ }
36865
36868
  } else {
36866
36869
  points.splice(i + 1, 1);
36867
36870
  i--;
36868
36871
  }
36869
36872
  }
36870
- if (pathPoints.length !== 0) {
36871
- pathPoints.push(points[points.length - 1]);
36872
- }
36873
- return pathPoints;
36873
+ return finalPath;
36874
36874
  }
36875
36875
  function reducePoints(points) {
36876
36876
  const uniquePoints = new Map;
@@ -36881,6 +36881,12 @@ function reducePoints(points) {
36881
36881
  if (uniquePoints.has(key)) {
36882
36882
  const loopStartIndex = uniquePoints.get(key);
36883
36883
  result.splice(loopStartIndex + 1);
36884
+ const removedPoints = points.slice(loopStartIndex + 1, i + 1);
36885
+ removedPoints.forEach((p3) => {
36886
+ uniquePoints.delete(`${p3.x},${p3.y}`);
36887
+ });
36888
+ uniquePoints.set(key, result.length);
36889
+ result.push(point5);
36884
36890
  } else {
36885
36891
  uniquePoints.set(key, result.length);
36886
36892
  result.push(point5);
@@ -36890,19 +36896,32 @@ function reducePoints(points) {
36890
36896
  }
36891
36897
  function getLines(pathPoints) {
36892
36898
  const lines = [];
36899
+ if (pathPoints.length < 2) {
36900
+ return [];
36901
+ }
36893
36902
  const reducedPoints = reducePoints(pathPoints);
36903
+ if (reducedPoints.length < 2) {
36904
+ return [];
36905
+ }
36894
36906
  let startPoint = reducedPoints[0];
36895
- for (let i = 1;i < reducedPoints.length - 1; i += 1) {
36907
+ for (let i = 1;i < reducedPoints.length; i += 1) {
36896
36908
  const prevPoint = reducedPoints[i - 1];
36897
36909
  const currPoint = reducedPoints[i];
36898
- const nextPoint = reducedPoints[i + 1];
36899
- if (prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
36910
+ const nextPoint = i + 1 < reducedPoints.length ? reducedPoints[i + 1] : null;
36911
+ if (!nextPoint || prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
36900
36912
  lines.push(new Line(startPoint, currPoint));
36901
36913
  startPoint = currPoint;
36902
36914
  }
36903
36915
  }
36904
- if (lines.length > 0) {
36905
- lines.push(new Line(startPoint, pathPoints[pathPoints.length - 1]));
36916
+ if (lines.length === 0 && reducedPoints.length > 1) {
36917
+ lines.push(new Line(reducedPoints[0], reducedPoints[reducedPoints.length - 1]));
36918
+ } else if (lines.length > 0) {
36919
+ const lastLine = lines[lines.length - 1];
36920
+ const lastPointInLines = lastLine.getEndPoint();
36921
+ const lastPointInReduced = reducedPoints[reducedPoints.length - 1];
36922
+ if (!lastPointInLines.barelyEqual(lastPointInReduced)) {
36923
+ lines.push(new Line(lastPointInLines, lastPointInReduced));
36924
+ }
36906
36925
  }
36907
36926
  return lines;
36908
36927
  }
@@ -36916,16 +36935,10 @@ function reconstructPath(node2) {
36916
36935
  return path2.reverse();
36917
36936
  }
36918
36937
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
36919
- const { grid, newStart, newEnd, middlePoint } = createGrid(start, end, toVisitPoints);
36920
- const startPoint = newStart ? newStart : start;
36921
- const endPoint = newEnd ? newEnd : end;
36922
- const centerLine = findCenterLine(grid, startPoint, endPoint, middlePoint);
36923
- const adjustedCenterLine = centerLine.length > 0 ? startPoint.getDistance(centerLine[0]) < startPoint.getDistance(centerLine[centerLine.length - 1]) ? centerLine : centerLine.reverse() : centerLine;
36924
- const points = [
36925
- startPoint,
36926
- ...toVisitPoints.length > 0 ? toVisitPoints : adjustedCenterLine,
36927
- endPoint
36928
- ];
36938
+ const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
36939
+ const startPoint = newStart || start;
36940
+ const endPoint = newEnd || end;
36941
+ const points = [startPoint, ...toVisitPoints, endPoint];
36929
36942
  const pathPoints = findPathPoints(points, grid, obstacles, newStart, newEnd);
36930
36943
  return {
36931
36944
  lines: getLines(pathPoints),