microboard-temp 0.5.96 → 0.5.98

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.
@@ -36682,6 +36682,7 @@ function getDirection(from, to) {
36682
36682
  return null;
36683
36683
  }
36684
36684
  function isChangingDirection(current, neighbor, newStart, newEnd) {
36685
+ const TURN_PENALTY = 50;
36685
36686
  const dirMap = {
36686
36687
  top: "vertical",
36687
36688
  bottom: "vertical",
@@ -36693,10 +36694,13 @@ function isChangingDirection(current, neighbor, newStart, newEnd) {
36693
36694
  if (newEnd && neighbor.point.barelyEqual(newEnd)) {
36694
36695
  const endDir = dirMap[getPointerDirection(newEnd)];
36695
36696
  if (goingDirection && endDir !== goingDirection) {
36696
- return 1 + isChangingDirection(current, neighbor, newStart);
36697
+ return TURN_PENALTY;
36697
36698
  }
36698
36699
  }
36699
- return comingDirection && goingDirection && comingDirection !== goingDirection ? 1 : 0;
36700
+ if (comingDirection && goingDirection && comingDirection !== goingDirection) {
36701
+ return TURN_PENALTY;
36702
+ }
36703
+ return 0;
36700
36704
  }
36701
36705
  function heuristic(start, end) {
36702
36706
  return Math.abs(start.xGrid - end.xGrid) + Math.abs(start.yGrid - end.yGrid);
@@ -36900,6 +36904,37 @@ function reconstructPath(node2) {
36900
36904
  }
36901
36905
  return path2.reverse();
36902
36906
  }
36907
+ function createHookWaypoints(startPoint, endPoint, startDir, endDir) {
36908
+ if (startDir === "right" && endDir === "left" && startPoint.x > endPoint.x) {
36909
+ const midY = (startPoint.y + endPoint.y) / 2;
36910
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36911
+ }
36912
+ if (startDir === "left" && endDir === "right" && startPoint.x < endPoint.x) {
36913
+ const midY = (startPoint.y + endPoint.y) / 2;
36914
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36915
+ }
36916
+ if (startDir === "bottom" && endDir === "top" && startPoint.y > endPoint.y) {
36917
+ const midX = (startPoint.x + endPoint.x) / 2;
36918
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36919
+ }
36920
+ if (startDir === "top" && endDir === "bottom" && startPoint.y < endPoint.y) {
36921
+ const midX = (startPoint.x + endPoint.x) / 2;
36922
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36923
+ }
36924
+ const dx = endPoint.x - startPoint.x;
36925
+ const dy = endPoint.y - startPoint.y;
36926
+ const startConflictX = startDir === "right" && dx < 0 || startDir === "left" && dx > 0;
36927
+ const startConflictY = startDir === "bottom" && dy < 0 || startDir === "top" && dy > 0;
36928
+ const endConflictX = endDir === "right" && dx > 0 || endDir === "left" && dx < 0;
36929
+ const endConflictY = endDir === "bottom" && dy < 0 || endDir === "top" && dy > 0;
36930
+ if (startConflictX || endConflictY) {
36931
+ return [new Point(startPoint.x, endPoint.y)];
36932
+ }
36933
+ if (startConflictY || endConflictX) {
36934
+ return [new Point(endPoint.x, startPoint.y)];
36935
+ }
36936
+ return [];
36937
+ }
36903
36938
  function findClosestPointInGrid(point5, grid) {
36904
36939
  let closestPoint = grid[0][0];
36905
36940
  let minDistance = Infinity;
@@ -36915,33 +36950,22 @@ function findClosestPointInGrid(point5, grid) {
36915
36950
  return closestPoint;
36916
36951
  }
36917
36952
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
36918
- const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
36953
+ const tempGridInfo = createGrid(start, end);
36954
+ const startPoint = tempGridInfo.newStart || start;
36955
+ const endPoint = tempGridInfo.newEnd || end;
36956
+ const startDir = getPointerDirection(start);
36957
+ const endDir = getPointerDirection(end);
36958
+ const hookWaypoints = createHookWaypoints(startPoint, endPoint, startDir, endDir);
36959
+ const allGuidingPoints = [...hookWaypoints, ...toVisitPoints];
36960
+ const { grid, newStart, newEnd } = createGrid(start, end, allGuidingPoints);
36919
36961
  if (grid.length === 0 || grid[0].length === 0) {
36920
36962
  return { lines: [], newStart, newEnd };
36921
36963
  }
36922
36964
  const searchStart = findClosestPointInGrid(newStart || start, grid);
36923
36965
  const searchEnd = findClosestPointInGrid(newEnd || end, grid);
36924
- const snappedWaypoints = toVisitPoints.map((p3) => findClosestPointInGrid(p3, grid));
36925
- const pointsToConnect = [searchStart, ...snappedWaypoints, searchEnd];
36926
- const finalPath = [pointsToConnect[0]];
36927
- const existingPathSegments = new Set([`${pointsToConnect[0].x},${pointsToConnect[0].y}`]);
36928
- for (let i = 0;i < pointsToConnect.length - 1; i++) {
36929
- const segmentStart = pointsToConnect[i];
36930
- const segmentEnd = pointsToConnect[i + 1];
36931
- const segmentPath = findPath(segmentStart, segmentEnd, grid, obstacles, existingPathSegments, newStart, newEnd);
36932
- if (segmentPath && segmentPath.length > 1) {
36933
- for (let j = 1;j < segmentPath.length; j++) {
36934
- const point5 = segmentPath[j];
36935
- finalPath.push(point5);
36936
- existingPathSegments.add(`${point5.x},${point5.y}`);
36937
- }
36938
- } else {
36939
- console.error(`Could not route segment from ${segmentStart.x},${segmentStart.y} to ${segmentEnd.x},${segmentEnd.y}`);
36940
- return { lines: [], newStart, newEnd };
36941
- }
36942
- }
36966
+ const pathPoints = findPath(searchStart, searchEnd, grid, obstacles, new Set, newStart, newEnd);
36943
36967
  return {
36944
- lines: getLines(finalPath),
36968
+ lines: pathPoints ? getLines(pathPoints) : [],
36945
36969
  newStart,
36946
36970
  newEnd
36947
36971
  };
package/dist/cjs/index.js CHANGED
@@ -36682,6 +36682,7 @@ function getDirection(from, to) {
36682
36682
  return null;
36683
36683
  }
36684
36684
  function isChangingDirection(current, neighbor, newStart, newEnd) {
36685
+ const TURN_PENALTY = 50;
36685
36686
  const dirMap = {
36686
36687
  top: "vertical",
36687
36688
  bottom: "vertical",
@@ -36693,10 +36694,13 @@ function isChangingDirection(current, neighbor, newStart, newEnd) {
36693
36694
  if (newEnd && neighbor.point.barelyEqual(newEnd)) {
36694
36695
  const endDir = dirMap[getPointerDirection(newEnd)];
36695
36696
  if (goingDirection && endDir !== goingDirection) {
36696
- return 1 + isChangingDirection(current, neighbor, newStart);
36697
+ return TURN_PENALTY;
36697
36698
  }
36698
36699
  }
36699
- return comingDirection && goingDirection && comingDirection !== goingDirection ? 1 : 0;
36700
+ if (comingDirection && goingDirection && comingDirection !== goingDirection) {
36701
+ return TURN_PENALTY;
36702
+ }
36703
+ return 0;
36700
36704
  }
36701
36705
  function heuristic(start, end) {
36702
36706
  return Math.abs(start.xGrid - end.xGrid) + Math.abs(start.yGrid - end.yGrid);
@@ -36900,6 +36904,37 @@ function reconstructPath(node2) {
36900
36904
  }
36901
36905
  return path2.reverse();
36902
36906
  }
36907
+ function createHookWaypoints(startPoint, endPoint, startDir, endDir) {
36908
+ if (startDir === "right" && endDir === "left" && startPoint.x > endPoint.x) {
36909
+ const midY = (startPoint.y + endPoint.y) / 2;
36910
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36911
+ }
36912
+ if (startDir === "left" && endDir === "right" && startPoint.x < endPoint.x) {
36913
+ const midY = (startPoint.y + endPoint.y) / 2;
36914
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36915
+ }
36916
+ if (startDir === "bottom" && endDir === "top" && startPoint.y > endPoint.y) {
36917
+ const midX = (startPoint.x + endPoint.x) / 2;
36918
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36919
+ }
36920
+ if (startDir === "top" && endDir === "bottom" && startPoint.y < endPoint.y) {
36921
+ const midX = (startPoint.x + endPoint.x) / 2;
36922
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36923
+ }
36924
+ const dx = endPoint.x - startPoint.x;
36925
+ const dy = endPoint.y - startPoint.y;
36926
+ const startConflictX = startDir === "right" && dx < 0 || startDir === "left" && dx > 0;
36927
+ const startConflictY = startDir === "bottom" && dy < 0 || startDir === "top" && dy > 0;
36928
+ const endConflictX = endDir === "right" && dx > 0 || endDir === "left" && dx < 0;
36929
+ const endConflictY = endDir === "bottom" && dy < 0 || endDir === "top" && dy > 0;
36930
+ if (startConflictX || endConflictY) {
36931
+ return [new Point(startPoint.x, endPoint.y)];
36932
+ }
36933
+ if (startConflictY || endConflictX) {
36934
+ return [new Point(endPoint.x, startPoint.y)];
36935
+ }
36936
+ return [];
36937
+ }
36903
36938
  function findClosestPointInGrid(point5, grid) {
36904
36939
  let closestPoint = grid[0][0];
36905
36940
  let minDistance = Infinity;
@@ -36915,33 +36950,22 @@ function findClosestPointInGrid(point5, grid) {
36915
36950
  return closestPoint;
36916
36951
  }
36917
36952
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
36918
- const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
36953
+ const tempGridInfo = createGrid(start, end);
36954
+ const startPoint = tempGridInfo.newStart || start;
36955
+ const endPoint = tempGridInfo.newEnd || end;
36956
+ const startDir = getPointerDirection(start);
36957
+ const endDir = getPointerDirection(end);
36958
+ const hookWaypoints = createHookWaypoints(startPoint, endPoint, startDir, endDir);
36959
+ const allGuidingPoints = [...hookWaypoints, ...toVisitPoints];
36960
+ const { grid, newStart, newEnd } = createGrid(start, end, allGuidingPoints);
36919
36961
  if (grid.length === 0 || grid[0].length === 0) {
36920
36962
  return { lines: [], newStart, newEnd };
36921
36963
  }
36922
36964
  const searchStart = findClosestPointInGrid(newStart || start, grid);
36923
36965
  const searchEnd = findClosestPointInGrid(newEnd || end, grid);
36924
- const snappedWaypoints = toVisitPoints.map((p3) => findClosestPointInGrid(p3, grid));
36925
- const pointsToConnect = [searchStart, ...snappedWaypoints, searchEnd];
36926
- const finalPath = [pointsToConnect[0]];
36927
- const existingPathSegments = new Set([`${pointsToConnect[0].x},${pointsToConnect[0].y}`]);
36928
- for (let i = 0;i < pointsToConnect.length - 1; i++) {
36929
- const segmentStart = pointsToConnect[i];
36930
- const segmentEnd = pointsToConnect[i + 1];
36931
- const segmentPath = findPath(segmentStart, segmentEnd, grid, obstacles, existingPathSegments, newStart, newEnd);
36932
- if (segmentPath && segmentPath.length > 1) {
36933
- for (let j = 1;j < segmentPath.length; j++) {
36934
- const point5 = segmentPath[j];
36935
- finalPath.push(point5);
36936
- existingPathSegments.add(`${point5.x},${point5.y}`);
36937
- }
36938
- } else {
36939
- console.error(`Could not route segment from ${segmentStart.x},${segmentStart.y} to ${segmentEnd.x},${segmentEnd.y}`);
36940
- return { lines: [], newStart, newEnd };
36941
- }
36942
- }
36966
+ const pathPoints = findPath(searchStart, searchEnd, grid, obstacles, new Set, newStart, newEnd);
36943
36967
  return {
36944
- lines: getLines(finalPath),
36968
+ lines: pathPoints ? getLines(pathPoints) : [],
36945
36969
  newStart,
36946
36970
  newEnd
36947
36971
  };
package/dist/cjs/node.js CHANGED
@@ -39155,6 +39155,7 @@ function getDirection(from, to) {
39155
39155
  return null;
39156
39156
  }
39157
39157
  function isChangingDirection(current, neighbor, newStart, newEnd) {
39158
+ const TURN_PENALTY = 50;
39158
39159
  const dirMap = {
39159
39160
  top: "vertical",
39160
39161
  bottom: "vertical",
@@ -39166,10 +39167,13 @@ function isChangingDirection(current, neighbor, newStart, newEnd) {
39166
39167
  if (newEnd && neighbor.point.barelyEqual(newEnd)) {
39167
39168
  const endDir = dirMap[getPointerDirection(newEnd)];
39168
39169
  if (goingDirection && endDir !== goingDirection) {
39169
- return 1 + isChangingDirection(current, neighbor, newStart);
39170
+ return TURN_PENALTY;
39170
39171
  }
39171
39172
  }
39172
- return comingDirection && goingDirection && comingDirection !== goingDirection ? 1 : 0;
39173
+ if (comingDirection && goingDirection && comingDirection !== goingDirection) {
39174
+ return TURN_PENALTY;
39175
+ }
39176
+ return 0;
39173
39177
  }
39174
39178
  function heuristic(start, end) {
39175
39179
  return Math.abs(start.xGrid - end.xGrid) + Math.abs(start.yGrid - end.yGrid);
@@ -39373,6 +39377,37 @@ function reconstructPath(node2) {
39373
39377
  }
39374
39378
  return path2.reverse();
39375
39379
  }
39380
+ function createHookWaypoints(startPoint, endPoint, startDir, endDir) {
39381
+ if (startDir === "right" && endDir === "left" && startPoint.x > endPoint.x) {
39382
+ const midY = (startPoint.y + endPoint.y) / 2;
39383
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
39384
+ }
39385
+ if (startDir === "left" && endDir === "right" && startPoint.x < endPoint.x) {
39386
+ const midY = (startPoint.y + endPoint.y) / 2;
39387
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
39388
+ }
39389
+ if (startDir === "bottom" && endDir === "top" && startPoint.y > endPoint.y) {
39390
+ const midX = (startPoint.x + endPoint.x) / 2;
39391
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
39392
+ }
39393
+ if (startDir === "top" && endDir === "bottom" && startPoint.y < endPoint.y) {
39394
+ const midX = (startPoint.x + endPoint.x) / 2;
39395
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
39396
+ }
39397
+ const dx = endPoint.x - startPoint.x;
39398
+ const dy = endPoint.y - startPoint.y;
39399
+ const startConflictX = startDir === "right" && dx < 0 || startDir === "left" && dx > 0;
39400
+ const startConflictY = startDir === "bottom" && dy < 0 || startDir === "top" && dy > 0;
39401
+ const endConflictX = endDir === "right" && dx > 0 || endDir === "left" && dx < 0;
39402
+ const endConflictY = endDir === "bottom" && dy < 0 || endDir === "top" && dy > 0;
39403
+ if (startConflictX || endConflictY) {
39404
+ return [new Point(startPoint.x, endPoint.y)];
39405
+ }
39406
+ if (startConflictY || endConflictX) {
39407
+ return [new Point(endPoint.x, startPoint.y)];
39408
+ }
39409
+ return [];
39410
+ }
39376
39411
  function findClosestPointInGrid(point5, grid) {
39377
39412
  let closestPoint = grid[0][0];
39378
39413
  let minDistance = Infinity;
@@ -39388,33 +39423,22 @@ function findClosestPointInGrid(point5, grid) {
39388
39423
  return closestPoint;
39389
39424
  }
39390
39425
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
39391
- const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
39426
+ const tempGridInfo = createGrid(start, end);
39427
+ const startPoint = tempGridInfo.newStart || start;
39428
+ const endPoint = tempGridInfo.newEnd || end;
39429
+ const startDir = getPointerDirection(start);
39430
+ const endDir = getPointerDirection(end);
39431
+ const hookWaypoints = createHookWaypoints(startPoint, endPoint, startDir, endDir);
39432
+ const allGuidingPoints = [...hookWaypoints, ...toVisitPoints];
39433
+ const { grid, newStart, newEnd } = createGrid(start, end, allGuidingPoints);
39392
39434
  if (grid.length === 0 || grid[0].length === 0) {
39393
39435
  return { lines: [], newStart, newEnd };
39394
39436
  }
39395
39437
  const searchStart = findClosestPointInGrid(newStart || start, grid);
39396
39438
  const searchEnd = findClosestPointInGrid(newEnd || end, grid);
39397
- const snappedWaypoints = toVisitPoints.map((p3) => findClosestPointInGrid(p3, grid));
39398
- const pointsToConnect = [searchStart, ...snappedWaypoints, searchEnd];
39399
- const finalPath = [pointsToConnect[0]];
39400
- const existingPathSegments = new Set([`${pointsToConnect[0].x},${pointsToConnect[0].y}`]);
39401
- for (let i = 0;i < pointsToConnect.length - 1; i++) {
39402
- const segmentStart = pointsToConnect[i];
39403
- const segmentEnd = pointsToConnect[i + 1];
39404
- const segmentPath = findPath(segmentStart, segmentEnd, grid, obstacles, existingPathSegments, newStart, newEnd);
39405
- if (segmentPath && segmentPath.length > 1) {
39406
- for (let j = 1;j < segmentPath.length; j++) {
39407
- const point5 = segmentPath[j];
39408
- finalPath.push(point5);
39409
- existingPathSegments.add(`${point5.x},${point5.y}`);
39410
- }
39411
- } else {
39412
- console.error(`Could not route segment from ${segmentStart.x},${segmentStart.y} to ${segmentEnd.x},${segmentEnd.y}`);
39413
- return { lines: [], newStart, newEnd };
39414
- }
39415
- }
39439
+ const pathPoints = findPath(searchStart, searchEnd, grid, obstacles, new Set, newStart, newEnd);
39416
39440
  return {
39417
- lines: getLines(finalPath),
39441
+ lines: pathPoints ? getLines(pathPoints) : [],
39418
39442
  newStart,
39419
39443
  newEnd
39420
39444
  };
@@ -36527,6 +36527,7 @@ function getDirection(from, to) {
36527
36527
  return null;
36528
36528
  }
36529
36529
  function isChangingDirection(current, neighbor, newStart, newEnd) {
36530
+ const TURN_PENALTY = 50;
36530
36531
  const dirMap = {
36531
36532
  top: "vertical",
36532
36533
  bottom: "vertical",
@@ -36538,10 +36539,13 @@ function isChangingDirection(current, neighbor, newStart, newEnd) {
36538
36539
  if (newEnd && neighbor.point.barelyEqual(newEnd)) {
36539
36540
  const endDir = dirMap[getPointerDirection(newEnd)];
36540
36541
  if (goingDirection && endDir !== goingDirection) {
36541
- return 1 + isChangingDirection(current, neighbor, newStart);
36542
+ return TURN_PENALTY;
36542
36543
  }
36543
36544
  }
36544
- return comingDirection && goingDirection && comingDirection !== goingDirection ? 1 : 0;
36545
+ if (comingDirection && goingDirection && comingDirection !== goingDirection) {
36546
+ return TURN_PENALTY;
36547
+ }
36548
+ return 0;
36545
36549
  }
36546
36550
  function heuristic(start, end) {
36547
36551
  return Math.abs(start.xGrid - end.xGrid) + Math.abs(start.yGrid - end.yGrid);
@@ -36745,6 +36749,37 @@ function reconstructPath(node2) {
36745
36749
  }
36746
36750
  return path2.reverse();
36747
36751
  }
36752
+ function createHookWaypoints(startPoint, endPoint, startDir, endDir) {
36753
+ if (startDir === "right" && endDir === "left" && startPoint.x > endPoint.x) {
36754
+ const midY = (startPoint.y + endPoint.y) / 2;
36755
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36756
+ }
36757
+ if (startDir === "left" && endDir === "right" && startPoint.x < endPoint.x) {
36758
+ const midY = (startPoint.y + endPoint.y) / 2;
36759
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36760
+ }
36761
+ if (startDir === "bottom" && endDir === "top" && startPoint.y > endPoint.y) {
36762
+ const midX = (startPoint.x + endPoint.x) / 2;
36763
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36764
+ }
36765
+ if (startDir === "top" && endDir === "bottom" && startPoint.y < endPoint.y) {
36766
+ const midX = (startPoint.x + endPoint.x) / 2;
36767
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36768
+ }
36769
+ const dx = endPoint.x - startPoint.x;
36770
+ const dy = endPoint.y - startPoint.y;
36771
+ const startConflictX = startDir === "right" && dx < 0 || startDir === "left" && dx > 0;
36772
+ const startConflictY = startDir === "bottom" && dy < 0 || startDir === "top" && dy > 0;
36773
+ const endConflictX = endDir === "right" && dx > 0 || endDir === "left" && dx < 0;
36774
+ const endConflictY = endDir === "bottom" && dy < 0 || endDir === "top" && dy > 0;
36775
+ if (startConflictX || endConflictY) {
36776
+ return [new Point(startPoint.x, endPoint.y)];
36777
+ }
36778
+ if (startConflictY || endConflictX) {
36779
+ return [new Point(endPoint.x, startPoint.y)];
36780
+ }
36781
+ return [];
36782
+ }
36748
36783
  function findClosestPointInGrid(point5, grid) {
36749
36784
  let closestPoint = grid[0][0];
36750
36785
  let minDistance = Infinity;
@@ -36760,33 +36795,22 @@ function findClosestPointInGrid(point5, grid) {
36760
36795
  return closestPoint;
36761
36796
  }
36762
36797
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
36763
- const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
36798
+ const tempGridInfo = createGrid(start, end);
36799
+ const startPoint = tempGridInfo.newStart || start;
36800
+ const endPoint = tempGridInfo.newEnd || end;
36801
+ const startDir = getPointerDirection(start);
36802
+ const endDir = getPointerDirection(end);
36803
+ const hookWaypoints = createHookWaypoints(startPoint, endPoint, startDir, endDir);
36804
+ const allGuidingPoints = [...hookWaypoints, ...toVisitPoints];
36805
+ const { grid, newStart, newEnd } = createGrid(start, end, allGuidingPoints);
36764
36806
  if (grid.length === 0 || grid[0].length === 0) {
36765
36807
  return { lines: [], newStart, newEnd };
36766
36808
  }
36767
36809
  const searchStart = findClosestPointInGrid(newStart || start, grid);
36768
36810
  const searchEnd = findClosestPointInGrid(newEnd || end, grid);
36769
- const snappedWaypoints = toVisitPoints.map((p3) => findClosestPointInGrid(p3, grid));
36770
- const pointsToConnect = [searchStart, ...snappedWaypoints, searchEnd];
36771
- const finalPath = [pointsToConnect[0]];
36772
- const existingPathSegments = new Set([`${pointsToConnect[0].x},${pointsToConnect[0].y}`]);
36773
- for (let i = 0;i < pointsToConnect.length - 1; i++) {
36774
- const segmentStart = pointsToConnect[i];
36775
- const segmentEnd = pointsToConnect[i + 1];
36776
- const segmentPath = findPath(segmentStart, segmentEnd, grid, obstacles, existingPathSegments, newStart, newEnd);
36777
- if (segmentPath && segmentPath.length > 1) {
36778
- for (let j = 1;j < segmentPath.length; j++) {
36779
- const point5 = segmentPath[j];
36780
- finalPath.push(point5);
36781
- existingPathSegments.add(`${point5.x},${point5.y}`);
36782
- }
36783
- } else {
36784
- console.error(`Could not route segment from ${segmentStart.x},${segmentStart.y} to ${segmentEnd.x},${segmentEnd.y}`);
36785
- return { lines: [], newStart, newEnd };
36786
- }
36787
- }
36811
+ const pathPoints = findPath(searchStart, searchEnd, grid, obstacles, new Set, newStart, newEnd);
36788
36812
  return {
36789
- lines: getLines(finalPath),
36813
+ lines: pathPoints ? getLines(pathPoints) : [],
36790
36814
  newStart,
36791
36815
  newEnd
36792
36816
  };
package/dist/esm/index.js CHANGED
@@ -36520,6 +36520,7 @@ function getDirection(from, to) {
36520
36520
  return null;
36521
36521
  }
36522
36522
  function isChangingDirection(current, neighbor, newStart, newEnd) {
36523
+ const TURN_PENALTY = 50;
36523
36524
  const dirMap = {
36524
36525
  top: "vertical",
36525
36526
  bottom: "vertical",
@@ -36531,10 +36532,13 @@ function isChangingDirection(current, neighbor, newStart, newEnd) {
36531
36532
  if (newEnd && neighbor.point.barelyEqual(newEnd)) {
36532
36533
  const endDir = dirMap[getPointerDirection(newEnd)];
36533
36534
  if (goingDirection && endDir !== goingDirection) {
36534
- return 1 + isChangingDirection(current, neighbor, newStart);
36535
+ return TURN_PENALTY;
36535
36536
  }
36536
36537
  }
36537
- return comingDirection && goingDirection && comingDirection !== goingDirection ? 1 : 0;
36538
+ if (comingDirection && goingDirection && comingDirection !== goingDirection) {
36539
+ return TURN_PENALTY;
36540
+ }
36541
+ return 0;
36538
36542
  }
36539
36543
  function heuristic(start, end) {
36540
36544
  return Math.abs(start.xGrid - end.xGrid) + Math.abs(start.yGrid - end.yGrid);
@@ -36738,6 +36742,37 @@ function reconstructPath(node2) {
36738
36742
  }
36739
36743
  return path2.reverse();
36740
36744
  }
36745
+ function createHookWaypoints(startPoint, endPoint, startDir, endDir) {
36746
+ if (startDir === "right" && endDir === "left" && startPoint.x > endPoint.x) {
36747
+ const midY = (startPoint.y + endPoint.y) / 2;
36748
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36749
+ }
36750
+ if (startDir === "left" && endDir === "right" && startPoint.x < endPoint.x) {
36751
+ const midY = (startPoint.y + endPoint.y) / 2;
36752
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
36753
+ }
36754
+ if (startDir === "bottom" && endDir === "top" && startPoint.y > endPoint.y) {
36755
+ const midX = (startPoint.x + endPoint.x) / 2;
36756
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36757
+ }
36758
+ if (startDir === "top" && endDir === "bottom" && startPoint.y < endPoint.y) {
36759
+ const midX = (startPoint.x + endPoint.x) / 2;
36760
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
36761
+ }
36762
+ const dx = endPoint.x - startPoint.x;
36763
+ const dy = endPoint.y - startPoint.y;
36764
+ const startConflictX = startDir === "right" && dx < 0 || startDir === "left" && dx > 0;
36765
+ const startConflictY = startDir === "bottom" && dy < 0 || startDir === "top" && dy > 0;
36766
+ const endConflictX = endDir === "right" && dx > 0 || endDir === "left" && dx < 0;
36767
+ const endConflictY = endDir === "bottom" && dy < 0 || endDir === "top" && dy > 0;
36768
+ if (startConflictX || endConflictY) {
36769
+ return [new Point(startPoint.x, endPoint.y)];
36770
+ }
36771
+ if (startConflictY || endConflictX) {
36772
+ return [new Point(endPoint.x, startPoint.y)];
36773
+ }
36774
+ return [];
36775
+ }
36741
36776
  function findClosestPointInGrid(point5, grid) {
36742
36777
  let closestPoint = grid[0][0];
36743
36778
  let minDistance = Infinity;
@@ -36753,33 +36788,22 @@ function findClosestPointInGrid(point5, grid) {
36753
36788
  return closestPoint;
36754
36789
  }
36755
36790
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
36756
- const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
36791
+ const tempGridInfo = createGrid(start, end);
36792
+ const startPoint = tempGridInfo.newStart || start;
36793
+ const endPoint = tempGridInfo.newEnd || end;
36794
+ const startDir = getPointerDirection(start);
36795
+ const endDir = getPointerDirection(end);
36796
+ const hookWaypoints = createHookWaypoints(startPoint, endPoint, startDir, endDir);
36797
+ const allGuidingPoints = [...hookWaypoints, ...toVisitPoints];
36798
+ const { grid, newStart, newEnd } = createGrid(start, end, allGuidingPoints);
36757
36799
  if (grid.length === 0 || grid[0].length === 0) {
36758
36800
  return { lines: [], newStart, newEnd };
36759
36801
  }
36760
36802
  const searchStart = findClosestPointInGrid(newStart || start, grid);
36761
36803
  const searchEnd = findClosestPointInGrid(newEnd || end, grid);
36762
- const snappedWaypoints = toVisitPoints.map((p3) => findClosestPointInGrid(p3, grid));
36763
- const pointsToConnect = [searchStart, ...snappedWaypoints, searchEnd];
36764
- const finalPath = [pointsToConnect[0]];
36765
- const existingPathSegments = new Set([`${pointsToConnect[0].x},${pointsToConnect[0].y}`]);
36766
- for (let i = 0;i < pointsToConnect.length - 1; i++) {
36767
- const segmentStart = pointsToConnect[i];
36768
- const segmentEnd = pointsToConnect[i + 1];
36769
- const segmentPath = findPath(segmentStart, segmentEnd, grid, obstacles, existingPathSegments, newStart, newEnd);
36770
- if (segmentPath && segmentPath.length > 1) {
36771
- for (let j = 1;j < segmentPath.length; j++) {
36772
- const point5 = segmentPath[j];
36773
- finalPath.push(point5);
36774
- existingPathSegments.add(`${point5.x},${point5.y}`);
36775
- }
36776
- } else {
36777
- console.error(`Could not route segment from ${segmentStart.x},${segmentStart.y} to ${segmentEnd.x},${segmentEnd.y}`);
36778
- return { lines: [], newStart, newEnd };
36779
- }
36780
- }
36804
+ const pathPoints = findPath(searchStart, searchEnd, grid, obstacles, new Set, newStart, newEnd);
36781
36805
  return {
36782
- lines: getLines(finalPath),
36806
+ lines: pathPoints ? getLines(pathPoints) : [],
36783
36807
  newStart,
36784
36808
  newEnd
36785
36809
  };
package/dist/esm/node.js CHANGED
@@ -38988,6 +38988,7 @@ function getDirection(from, to) {
38988
38988
  return null;
38989
38989
  }
38990
38990
  function isChangingDirection(current, neighbor, newStart, newEnd) {
38991
+ const TURN_PENALTY = 50;
38991
38992
  const dirMap = {
38992
38993
  top: "vertical",
38993
38994
  bottom: "vertical",
@@ -38999,10 +39000,13 @@ function isChangingDirection(current, neighbor, newStart, newEnd) {
38999
39000
  if (newEnd && neighbor.point.barelyEqual(newEnd)) {
39000
39001
  const endDir = dirMap[getPointerDirection(newEnd)];
39001
39002
  if (goingDirection && endDir !== goingDirection) {
39002
- return 1 + isChangingDirection(current, neighbor, newStart);
39003
+ return TURN_PENALTY;
39003
39004
  }
39004
39005
  }
39005
- return comingDirection && goingDirection && comingDirection !== goingDirection ? 1 : 0;
39006
+ if (comingDirection && goingDirection && comingDirection !== goingDirection) {
39007
+ return TURN_PENALTY;
39008
+ }
39009
+ return 0;
39006
39010
  }
39007
39011
  function heuristic(start, end) {
39008
39012
  return Math.abs(start.xGrid - end.xGrid) + Math.abs(start.yGrid - end.yGrid);
@@ -39206,6 +39210,37 @@ function reconstructPath(node2) {
39206
39210
  }
39207
39211
  return path2.reverse();
39208
39212
  }
39213
+ function createHookWaypoints(startPoint, endPoint, startDir, endDir) {
39214
+ if (startDir === "right" && endDir === "left" && startPoint.x > endPoint.x) {
39215
+ const midY = (startPoint.y + endPoint.y) / 2;
39216
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
39217
+ }
39218
+ if (startDir === "left" && endDir === "right" && startPoint.x < endPoint.x) {
39219
+ const midY = (startPoint.y + endPoint.y) / 2;
39220
+ return [new Point(startPoint.x, midY), new Point(endPoint.x, midY)];
39221
+ }
39222
+ if (startDir === "bottom" && endDir === "top" && startPoint.y > endPoint.y) {
39223
+ const midX = (startPoint.x + endPoint.x) / 2;
39224
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
39225
+ }
39226
+ if (startDir === "top" && endDir === "bottom" && startPoint.y < endPoint.y) {
39227
+ const midX = (startPoint.x + endPoint.x) / 2;
39228
+ return [new Point(midX, startPoint.y), new Point(midX, endPoint.y)];
39229
+ }
39230
+ const dx = endPoint.x - startPoint.x;
39231
+ const dy = endPoint.y - startPoint.y;
39232
+ const startConflictX = startDir === "right" && dx < 0 || startDir === "left" && dx > 0;
39233
+ const startConflictY = startDir === "bottom" && dy < 0 || startDir === "top" && dy > 0;
39234
+ const endConflictX = endDir === "right" && dx > 0 || endDir === "left" && dx < 0;
39235
+ const endConflictY = endDir === "bottom" && dy < 0 || endDir === "top" && dy > 0;
39236
+ if (startConflictX || endConflictY) {
39237
+ return [new Point(startPoint.x, endPoint.y)];
39238
+ }
39239
+ if (startConflictY || endConflictX) {
39240
+ return [new Point(endPoint.x, startPoint.y)];
39241
+ }
39242
+ return [];
39243
+ }
39209
39244
  function findClosestPointInGrid(point5, grid) {
39210
39245
  let closestPoint = grid[0][0];
39211
39246
  let minDistance = Infinity;
@@ -39221,33 +39256,22 @@ function findClosestPointInGrid(point5, grid) {
39221
39256
  return closestPoint;
39222
39257
  }
39223
39258
  function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
39224
- const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
39259
+ const tempGridInfo = createGrid(start, end);
39260
+ const startPoint = tempGridInfo.newStart || start;
39261
+ const endPoint = tempGridInfo.newEnd || end;
39262
+ const startDir = getPointerDirection(start);
39263
+ const endDir = getPointerDirection(end);
39264
+ const hookWaypoints = createHookWaypoints(startPoint, endPoint, startDir, endDir);
39265
+ const allGuidingPoints = [...hookWaypoints, ...toVisitPoints];
39266
+ const { grid, newStart, newEnd } = createGrid(start, end, allGuidingPoints);
39225
39267
  if (grid.length === 0 || grid[0].length === 0) {
39226
39268
  return { lines: [], newStart, newEnd };
39227
39269
  }
39228
39270
  const searchStart = findClosestPointInGrid(newStart || start, grid);
39229
39271
  const searchEnd = findClosestPointInGrid(newEnd || end, grid);
39230
- const snappedWaypoints = toVisitPoints.map((p3) => findClosestPointInGrid(p3, grid));
39231
- const pointsToConnect = [searchStart, ...snappedWaypoints, searchEnd];
39232
- const finalPath = [pointsToConnect[0]];
39233
- const existingPathSegments = new Set([`${pointsToConnect[0].x},${pointsToConnect[0].y}`]);
39234
- for (let i = 0;i < pointsToConnect.length - 1; i++) {
39235
- const segmentStart = pointsToConnect[i];
39236
- const segmentEnd = pointsToConnect[i + 1];
39237
- const segmentPath = findPath(segmentStart, segmentEnd, grid, obstacles, existingPathSegments, newStart, newEnd);
39238
- if (segmentPath && segmentPath.length > 1) {
39239
- for (let j = 1;j < segmentPath.length; j++) {
39240
- const point5 = segmentPath[j];
39241
- finalPath.push(point5);
39242
- existingPathSegments.add(`${point5.x},${point5.y}`);
39243
- }
39244
- } else {
39245
- console.error(`Could not route segment from ${segmentStart.x},${segmentStart.y} to ${segmentEnd.x},${segmentEnd.y}`);
39246
- return { lines: [], newStart, newEnd };
39247
- }
39248
- }
39272
+ const pathPoints = findPath(searchStart, searchEnd, grid, obstacles, new Set, newStart, newEnd);
39249
39273
  return {
39250
- lines: getLines(finalPath),
39274
+ lines: pathPoints ? getLines(pathPoints) : [],
39251
39275
  newStart,
39252
39276
  newEnd
39253
39277
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "microboard-temp",
3
- "version": "0.5.96",
3
+ "version": "0.5.98",
4
4
  "description": "A flexible interactive whiteboard library",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",