@tscircuit/math-utils 0.0.3 → 0.0.5
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.
- package/dist/chunk-GIGMPRPV.js +30 -0
- package/dist/chunk-GIGMPRPV.js.map +1 -0
- package/dist/chunk-MHHTZHOJ.js +62 -0
- package/dist/chunk-MHHTZHOJ.js.map +1 -0
- package/dist/get-unit-vector.d.ts +6 -0
- package/dist/get-unit-vector.js +9 -0
- package/dist/get-unit-vector.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -1
- package/dist/nearest-box.js +1 -1
- package/package.json +2 -1
- package/dist/chunk-EJDR5MHT.js +0 -111
- package/dist/chunk-EJDR5MHT.js.map +0 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/get-unit-vector.ts
|
|
2
|
+
var getUnitVectorFromPointAToB = (a, b) => {
|
|
3
|
+
const delta = {
|
|
4
|
+
x: b.x - a.x,
|
|
5
|
+
y: b.y - a.y
|
|
6
|
+
};
|
|
7
|
+
const magnitude = Math.sqrt(delta.x ** 2 + delta.y ** 2);
|
|
8
|
+
return {
|
|
9
|
+
x: delta.x / magnitude,
|
|
10
|
+
y: delta.y / magnitude
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
var getUnitVectorFromDirection = (direction) => {
|
|
14
|
+
switch (direction) {
|
|
15
|
+
case "up":
|
|
16
|
+
return { x: 0, y: 1 };
|
|
17
|
+
case "down":
|
|
18
|
+
return { x: 0, y: -1 };
|
|
19
|
+
case "left":
|
|
20
|
+
return { x: -1, y: 0 };
|
|
21
|
+
case "right":
|
|
22
|
+
return { x: 1, y: 0 };
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
getUnitVectorFromPointAToB,
|
|
28
|
+
getUnitVectorFromDirection
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=chunk-GIGMPRPV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/get-unit-vector.ts"],"sourcesContent":["import type { Point } from \"./common\"\n\nexport const getUnitVectorFromPointAToB = (a: Point, b: Point): Point => {\n const delta = {\n x: b.x - a.x,\n y: b.y - a.y,\n }\n\n const magnitude = Math.sqrt(delta.x ** 2 + delta.y ** 2)\n\n return {\n x: delta.x / magnitude,\n y: delta.y / magnitude,\n }\n}\n\nexport const getUnitVectorFromDirection = (\n direction: \"up\" | \"down\" | \"left\" | \"right\",\n): Point => {\n switch (direction) {\n case \"up\":\n return { x: 0, y: 1 }\n case \"down\":\n return { x: 0, y: -1 }\n case \"left\":\n return { x: -1, y: 0 }\n case \"right\":\n return { x: 1, y: 0 }\n }\n}\n"],"mappings":";AAEO,IAAM,6BAA6B,CAAC,GAAU,MAAoB;AACvE,QAAM,QAAQ;AAAA,IACZ,GAAG,EAAE,IAAI,EAAE;AAAA,IACX,GAAG,EAAE,IAAI,EAAE;AAAA,EACb;AAEA,QAAM,YAAY,KAAK,KAAK,MAAM,KAAK,IAAI,MAAM,KAAK,CAAC;AAEvD,SAAO;AAAA,IACL,GAAG,MAAM,IAAI;AAAA,IACb,GAAG,MAAM,IAAI;AAAA,EACf;AACF;AAEO,IAAM,6BAA6B,CACxC,cACU;AACV,UAAQ,WAAW;AAAA,IACjB,KAAK;AACH,aAAO,EAAE,GAAG,GAAG,GAAG,EAAE;AAAA,IACtB,KAAK;AACH,aAAO,EAAE,GAAG,GAAG,GAAG,GAAG;AAAA,IACvB,KAAK;AACH,aAAO,EAAE,GAAG,IAAI,GAAG,EAAE;AAAA,IACvB,KAAK;AACH,aAAO,EAAE,GAAG,GAAG,GAAG,EAAE;AAAA,EACxB;AACF;","names":[]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/nearest-box.ts
|
|
2
|
+
function getBoundingBox(box) {
|
|
3
|
+
const halfWidth = box.width / 2;
|
|
4
|
+
const halfHeight = box.height / 2;
|
|
5
|
+
return {
|
|
6
|
+
minX: box.center.x - halfWidth,
|
|
7
|
+
maxX: box.center.x + halfWidth,
|
|
8
|
+
minY: box.center.y - halfHeight,
|
|
9
|
+
maxY: box.center.y + halfHeight
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function computeDistanceBetweenBoxes(boxA, boxB) {
|
|
13
|
+
const a = getBoundingBox(boxA);
|
|
14
|
+
const b = getBoundingBox(boxB);
|
|
15
|
+
const dx = Math.max(a.minX - b.maxX, b.minX - a.maxX, 0);
|
|
16
|
+
const dy = Math.max(a.minY - b.maxY, b.minY - a.maxY, 0);
|
|
17
|
+
const pointA = { x: 0, y: 0 };
|
|
18
|
+
const pointB = { x: 0, y: 0 };
|
|
19
|
+
if (dx === 0 && dy === 0) {
|
|
20
|
+
return { distance: 0, pointA: boxA.center, pointB: boxB.center };
|
|
21
|
+
}
|
|
22
|
+
pointA.x = clamp(boxA.center.x, b.minX, b.maxX);
|
|
23
|
+
pointA.y = clamp(boxA.center.y, b.minY, b.maxY);
|
|
24
|
+
pointB.x = clamp(boxB.center.x, a.minX, a.maxX);
|
|
25
|
+
pointB.y = clamp(boxB.center.y, a.minY, a.maxY);
|
|
26
|
+
const distance = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);
|
|
27
|
+
return { distance, pointA, pointB };
|
|
28
|
+
}
|
|
29
|
+
function clamp(value, min, max) {
|
|
30
|
+
return Math.max(min, Math.min(max, value));
|
|
31
|
+
}
|
|
32
|
+
function findNearestPointsBetweenBoxSets(boxSetA, boxSetB) {
|
|
33
|
+
let minDistance = Number.POSITIVE_INFINITY;
|
|
34
|
+
let nearestPointA = { x: 0, y: 0 };
|
|
35
|
+
let nearestPointB = { x: 0, y: 0 };
|
|
36
|
+
for (const boxA of boxSetA) {
|
|
37
|
+
for (const boxB of boxSetB) {
|
|
38
|
+
const { distance, pointA, pointB } = computeDistanceBetweenBoxes(
|
|
39
|
+
boxA,
|
|
40
|
+
boxB
|
|
41
|
+
);
|
|
42
|
+
if (distance < minDistance) {
|
|
43
|
+
minDistance = distance;
|
|
44
|
+
nearestPointA = pointA;
|
|
45
|
+
nearestPointB = pointB;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
pointA: nearestPointA,
|
|
51
|
+
pointB: nearestPointB,
|
|
52
|
+
distance: minDistance
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
getBoundingBox,
|
|
58
|
+
computeDistanceBetweenBoxes,
|
|
59
|
+
clamp,
|
|
60
|
+
findNearestPointsBetweenBoxSets
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=chunk-MHHTZHOJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/nearest-box.ts"],"sourcesContent":["import type { Point } from \"./common\"\n\nexport type Box = { center: Point; width: number; height: number }\nexport type BoxSet = Box[]\n\nexport type GridCell = { boxes: Box[] }\n\nexport function getBoundingBox(box: Box) {\n const halfWidth = box.width / 2\n const halfHeight = box.height / 2\n return {\n minX: box.center.x - halfWidth,\n maxX: box.center.x + halfWidth,\n minY: box.center.y - halfHeight,\n maxY: box.center.y + halfHeight,\n }\n}\n\nexport function computeDistanceBetweenBoxes(\n boxA: Box,\n boxB: Box,\n): { distance: number; pointA: Point; pointB: Point } {\n const a = getBoundingBox(boxA)\n const b = getBoundingBox(boxB)\n\n const dx = Math.max(a.minX - b.maxX, b.minX - a.maxX, 0)\n const dy = Math.max(a.minY - b.maxY, b.minY - a.maxY, 0)\n\n const pointA: Point = { x: 0, y: 0 }\n const pointB: Point = { x: 0, y: 0 }\n\n if (dx === 0 && dy === 0) {\n // Boxes overlap\n return { distance: 0, pointA: boxA.center, pointB: boxB.center }\n }\n\n // Compute the closest points on the edges\n pointA.x = clamp(boxA.center.x, b.minX, b.maxX)\n pointA.y = clamp(boxA.center.y, b.minY, b.maxY)\n\n pointB.x = clamp(boxB.center.x, a.minX, a.maxX)\n pointB.y = clamp(boxB.center.y, a.minY, a.maxY)\n\n const distance = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y)\n return { distance, pointA, pointB }\n}\n\nexport function clamp(value: number, min: number, max: number): number {\n return Math.max(min, Math.min(max, value))\n}\n\nexport function findNearestPointsBetweenBoxSets(\n boxSetA: BoxSet,\n boxSetB: BoxSet,\n): { pointA: Point; pointB: Point; distance: number } {\n let minDistance = Number.POSITIVE_INFINITY\n let nearestPointA: Point = { x: 0, y: 0 }\n let nearestPointB: Point = { x: 0, y: 0 }\n\n for (const boxA of boxSetA) {\n for (const boxB of boxSetB) {\n const { distance, pointA, pointB } = computeDistanceBetweenBoxes(\n boxA,\n boxB,\n )\n if (distance < minDistance) {\n minDistance = distance\n nearestPointA = pointA\n nearestPointB = pointB\n }\n }\n }\n\n return {\n pointA: nearestPointA,\n pointB: nearestPointB,\n distance: minDistance,\n }\n}\n"],"mappings":";AAOO,SAAS,eAAe,KAAU;AACvC,QAAM,YAAY,IAAI,QAAQ;AAC9B,QAAM,aAAa,IAAI,SAAS;AAChC,SAAO;AAAA,IACL,MAAM,IAAI,OAAO,IAAI;AAAA,IACrB,MAAM,IAAI,OAAO,IAAI;AAAA,IACrB,MAAM,IAAI,OAAO,IAAI;AAAA,IACrB,MAAM,IAAI,OAAO,IAAI;AAAA,EACvB;AACF;AAEO,SAAS,4BACd,MACA,MACoD;AACpD,QAAM,IAAI,eAAe,IAAI;AAC7B,QAAM,IAAI,eAAe,IAAI;AAE7B,QAAM,KAAK,KAAK,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC;AACvD,QAAM,KAAK,KAAK,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC;AAEvD,QAAM,SAAgB,EAAE,GAAG,GAAG,GAAG,EAAE;AACnC,QAAM,SAAgB,EAAE,GAAG,GAAG,GAAG,EAAE;AAEnC,MAAI,OAAO,KAAK,OAAO,GAAG;AAExB,WAAO,EAAE,UAAU,GAAG,QAAQ,KAAK,QAAQ,QAAQ,KAAK,OAAO;AAAA,EACjE;AAGA,SAAO,IAAI,MAAM,KAAK,OAAO,GAAG,EAAE,MAAM,EAAE,IAAI;AAC9C,SAAO,IAAI,MAAM,KAAK,OAAO,GAAG,EAAE,MAAM,EAAE,IAAI;AAE9C,SAAO,IAAI,MAAM,KAAK,OAAO,GAAG,EAAE,MAAM,EAAE,IAAI;AAC9C,SAAO,IAAI,MAAM,KAAK,OAAO,GAAG,EAAE,MAAM,EAAE,IAAI;AAE9C,QAAM,WAAW,KAAK,MAAM,OAAO,IAAI,OAAO,GAAG,OAAO,IAAI,OAAO,CAAC;AACpE,SAAO,EAAE,UAAU,QAAQ,OAAO;AACpC;AAEO,SAAS,MAAM,OAAe,KAAa,KAAqB;AACrE,SAAO,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC;AAC3C;AAEO,SAAS,gCACd,SACA,SACoD;AACpD,MAAI,cAAc,OAAO;AACzB,MAAI,gBAAuB,EAAE,GAAG,GAAG,GAAG,EAAE;AACxC,MAAI,gBAAuB,EAAE,GAAG,GAAG,GAAG,EAAE;AAExC,aAAW,QAAQ,SAAS;AAC1B,eAAW,QAAQ,SAAS;AAC1B,YAAM,EAAE,UAAU,QAAQ,OAAO,IAAI;AAAA,QACnC;AAAA,QACA;AAAA,MACF;AACA,UAAI,WAAW,aAAa;AAC1B,sBAAc;AACd,wBAAgB;AAChB,wBAAgB;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,UAAU;AAAA,EACZ;AACF;","names":[]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Point } from './common.js';
|
|
2
|
+
|
|
3
|
+
declare const getUnitVectorFromPointAToB: (a: Point, b: Point) => Point;
|
|
4
|
+
declare const getUnitVectorFromDirection: (direction: "up" | "down" | "left" | "right") => Point;
|
|
5
|
+
|
|
6
|
+
export { getUnitVectorFromDirection, getUnitVectorFromPointAToB };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { distance, doSegmentsIntersect, doesLineIntersectLine, onSegment, orientation, pointToSegmentDistance } from './line-intersections.js';
|
|
2
2
|
export { Box, BoxSet, GridCell, clamp, computeDistanceBetweenBoxes, findNearestPointsBetweenBoxSets, getBoundingBox } from './nearest-box.js';
|
|
3
3
|
export { Point } from './common.js';
|
|
4
|
+
export { getUnitVectorFromDirection, getUnitVectorFromPointAToB } from './get-unit-vector.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import "./chunk-GYQ2KZV6.js";
|
|
2
|
+
import {
|
|
3
|
+
getUnitVectorFromDirection,
|
|
4
|
+
getUnitVectorFromPointAToB
|
|
5
|
+
} from "./chunk-GIGMPRPV.js";
|
|
2
6
|
import {
|
|
3
7
|
distance,
|
|
4
8
|
doSegmentsIntersect,
|
|
@@ -12,7 +16,7 @@ import {
|
|
|
12
16
|
computeDistanceBetweenBoxes,
|
|
13
17
|
findNearestPointsBetweenBoxSets,
|
|
14
18
|
getBoundingBox
|
|
15
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-MHHTZHOJ.js";
|
|
16
20
|
export {
|
|
17
21
|
clamp,
|
|
18
22
|
computeDistanceBetweenBoxes,
|
|
@@ -21,6 +25,8 @@ export {
|
|
|
21
25
|
doesLineIntersectLine,
|
|
22
26
|
findNearestPointsBetweenBoxSets,
|
|
23
27
|
getBoundingBox,
|
|
28
|
+
getUnitVectorFromDirection,
|
|
29
|
+
getUnitVectorFromPointAToB,
|
|
24
30
|
onSegment,
|
|
25
31
|
orientation,
|
|
26
32
|
pointToSegmentDistance
|
package/dist/nearest-box.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscircuit/math-utils",
|
|
3
3
|
"main": "dist/index.js",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"build": "tsup-node src --format esm --dts --sourcemap"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@biomejs/biome": "^1.9.1",
|
|
29
30
|
"@types/bun": "latest",
|
|
30
31
|
"tsup": "^8.2.4"
|
|
31
32
|
},
|
package/dist/chunk-EJDR5MHT.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
// src/nearest-box.ts
|
|
2
|
-
function getBoundingBox(box) {
|
|
3
|
-
const halfWidth = box.width / 2;
|
|
4
|
-
const halfHeight = box.height / 2;
|
|
5
|
-
return {
|
|
6
|
-
minX: box.center.x - halfWidth,
|
|
7
|
-
maxX: box.center.x + halfWidth,
|
|
8
|
-
minY: box.center.y - halfHeight,
|
|
9
|
-
maxY: box.center.y + halfHeight
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function computeDistanceBetweenBoxes(boxA, boxB) {
|
|
13
|
-
const a = getBoundingBox(boxA);
|
|
14
|
-
const b = getBoundingBox(boxB);
|
|
15
|
-
const dx = Math.max(a.minX - b.maxX, b.minX - a.maxX, 0);
|
|
16
|
-
const dy = Math.max(a.minY - b.maxY, b.minY - a.maxY, 0);
|
|
17
|
-
const pointA = { x: 0, y: 0 };
|
|
18
|
-
const pointB = { x: 0, y: 0 };
|
|
19
|
-
if (dx === 0 && dy === 0) {
|
|
20
|
-
return { distance: 0, pointA: boxA.center, pointB: boxB.center };
|
|
21
|
-
}
|
|
22
|
-
pointA.x = clamp(boxA.center.x, b.minX, b.maxX);
|
|
23
|
-
pointA.y = clamp(boxA.center.y, b.minY, b.maxY);
|
|
24
|
-
pointB.x = clamp(boxB.center.x, a.minX, a.maxX);
|
|
25
|
-
pointB.y = clamp(boxB.center.y, a.minY, a.maxY);
|
|
26
|
-
const distance = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);
|
|
27
|
-
return { distance, pointA, pointB };
|
|
28
|
-
}
|
|
29
|
-
function clamp(value, min, max) {
|
|
30
|
-
return Math.max(min, Math.min(max, value));
|
|
31
|
-
}
|
|
32
|
-
function findNearestPointsBetweenBoxSets(boxSetA, boxSetB) {
|
|
33
|
-
const allBoxes = [...boxSetA, ...boxSetB];
|
|
34
|
-
let minX = Number.POSITIVE_INFINITY;
|
|
35
|
-
let minY = Number.POSITIVE_INFINITY;
|
|
36
|
-
let maxX = Number.NEGATIVE_INFINITY;
|
|
37
|
-
let maxY = Number.NEGATIVE_INFINITY;
|
|
38
|
-
let avgWidth = 0;
|
|
39
|
-
let avgHeight = 0;
|
|
40
|
-
for (const box of allBoxes) {
|
|
41
|
-
const bbox = getBoundingBox(box);
|
|
42
|
-
minX = Math.min(minX, bbox.minX);
|
|
43
|
-
minY = Math.min(minY, bbox.minY);
|
|
44
|
-
maxX = Math.max(maxX, bbox.maxX);
|
|
45
|
-
maxY = Math.max(maxY, bbox.maxY);
|
|
46
|
-
avgWidth += box.width;
|
|
47
|
-
avgHeight += box.height;
|
|
48
|
-
}
|
|
49
|
-
avgWidth /= allBoxes.length;
|
|
50
|
-
avgHeight /= allBoxes.length;
|
|
51
|
-
const cellSize = Math.max(avgWidth, avgHeight);
|
|
52
|
-
const gridWidth = Math.ceil((maxX - minX) / cellSize);
|
|
53
|
-
const gridHeight = Math.ceil((maxY - minY) / cellSize);
|
|
54
|
-
const grid = Array.from(
|
|
55
|
-
{ length: gridWidth },
|
|
56
|
-
() => Array.from({ length: gridHeight }, () => ({ boxes: [] }))
|
|
57
|
-
);
|
|
58
|
-
for (const box of boxSetB) {
|
|
59
|
-
const bbox = getBoundingBox(box);
|
|
60
|
-
const startX = Math.floor((bbox.minX - minX) / cellSize);
|
|
61
|
-
const endX = Math.floor((bbox.maxX - minX) / cellSize);
|
|
62
|
-
const startY = Math.floor((bbox.minY - minY) / cellSize);
|
|
63
|
-
const endY = Math.floor((bbox.maxY - minY) / cellSize);
|
|
64
|
-
for (let x = startX; x <= endX; x++) {
|
|
65
|
-
for (let y = startY; y <= endY; y++) {
|
|
66
|
-
grid[x][y].boxes.push(box);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
let minDistance = Number.POSITIVE_INFINITY;
|
|
71
|
-
let nearestPointA = { x: 0, y: 0 };
|
|
72
|
-
let nearestPointB = { x: 0, y: 0 };
|
|
73
|
-
for (const boxA of boxSetA) {
|
|
74
|
-
const bboxA = getBoundingBox(boxA);
|
|
75
|
-
const startX = Math.floor((bboxA.minX - minX) / cellSize);
|
|
76
|
-
const endX = Math.floor((bboxA.maxX - minX) / cellSize);
|
|
77
|
-
const startY = Math.floor((bboxA.minY - minY) / cellSize);
|
|
78
|
-
const endY = Math.floor((bboxA.maxY - minY) / cellSize);
|
|
79
|
-
for (let x = startX - 1; x <= endX + 1; x++) {
|
|
80
|
-
if (x < 0 || x >= gridWidth) continue;
|
|
81
|
-
for (let y = startY - 1; y <= endY + 1; y++) {
|
|
82
|
-
if (y < 0 || y >= gridHeight) continue;
|
|
83
|
-
const cell = grid[x][y];
|
|
84
|
-
for (const boxB of cell.boxes) {
|
|
85
|
-
const { distance, pointA, pointB } = computeDistanceBetweenBoxes(
|
|
86
|
-
boxA,
|
|
87
|
-
boxB
|
|
88
|
-
);
|
|
89
|
-
if (distance < minDistance) {
|
|
90
|
-
minDistance = distance;
|
|
91
|
-
nearestPointA = pointA;
|
|
92
|
-
nearestPointB = pointB;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return {
|
|
99
|
-
pointA: nearestPointA,
|
|
100
|
-
pointB: nearestPointB,
|
|
101
|
-
distance: minDistance
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export {
|
|
106
|
-
getBoundingBox,
|
|
107
|
-
computeDistanceBetweenBoxes,
|
|
108
|
-
clamp,
|
|
109
|
-
findNearestPointsBetweenBoxSets
|
|
110
|
-
};
|
|
111
|
-
//# sourceMappingURL=chunk-EJDR5MHT.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/nearest-box.ts"],"sourcesContent":["import type { Point } from \"./common\";\n\nexport type Box = { center: Point; width: number; height: number };\nexport type BoxSet = Box[];\n\nexport type GridCell = { boxes: Box[] };\n\nexport function getBoundingBox(box: Box) {\n\tconst halfWidth = box.width / 2;\n\tconst halfHeight = box.height / 2;\n\treturn {\n\t\tminX: box.center.x - halfWidth,\n\t\tmaxX: box.center.x + halfWidth,\n\t\tminY: box.center.y - halfHeight,\n\t\tmaxY: box.center.y + halfHeight,\n\t};\n}\n\nexport function computeDistanceBetweenBoxes(\n\tboxA: Box,\n\tboxB: Box,\n): { distance: number; pointA: Point; pointB: Point } {\n\tconst a = getBoundingBox(boxA);\n\tconst b = getBoundingBox(boxB);\n\n\tconst dx = Math.max(a.minX - b.maxX, b.minX - a.maxX, 0);\n\tconst dy = Math.max(a.minY - b.maxY, b.minY - a.maxY, 0);\n\n\tconst pointA: Point = { x: 0, y: 0 };\n\tconst pointB: Point = { x: 0, y: 0 };\n\n\tif (dx === 0 && dy === 0) {\n\t\t// Boxes overlap\n\t\treturn { distance: 0, pointA: boxA.center, pointB: boxB.center };\n\t}\n\n\t// Compute the closest points on the edges\n\tpointA.x = clamp(boxA.center.x, b.minX, b.maxX);\n\tpointA.y = clamp(boxA.center.y, b.minY, b.maxY);\n\n\tpointB.x = clamp(boxB.center.x, a.minX, a.maxX);\n\tpointB.y = clamp(boxB.center.y, a.minY, a.maxY);\n\n\tconst distance = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);\n\treturn { distance, pointA, pointB };\n}\n\nexport function clamp(value: number, min: number, max: number): number {\n\treturn Math.max(min, Math.min(max, value));\n}\n\nexport function findNearestPointsBetweenBoxSets(\n\tboxSetA: BoxSet,\n\tboxSetB: BoxSet,\n): { pointA: Point; pointB: Point; distance: number } {\n\t// Determine grid parameters\n\tconst allBoxes = [...boxSetA, ...boxSetB];\n\tlet minX = Number.POSITIVE_INFINITY;\n\tlet minY = Number.POSITIVE_INFINITY;\n\tlet maxX = Number.NEGATIVE_INFINITY;\n\tlet maxY = Number.NEGATIVE_INFINITY;\n\tlet avgWidth = 0;\n\tlet avgHeight = 0;\n\n\tfor (const box of allBoxes) {\n\t\tconst bbox = getBoundingBox(box);\n\t\tminX = Math.min(minX, bbox.minX);\n\t\tminY = Math.min(minY, bbox.minY);\n\t\tmaxX = Math.max(maxX, bbox.maxX);\n\t\tmaxY = Math.max(maxY, bbox.maxY);\n\t\tavgWidth += box.width;\n\t\tavgHeight += box.height;\n\t}\n\n\tavgWidth /= allBoxes.length;\n\tavgHeight /= allBoxes.length;\n\n\t// Define grid cell size\n\tconst cellSize = Math.max(avgWidth, avgHeight);\n\n\tconst gridWidth = Math.ceil((maxX - minX) / cellSize);\n\tconst gridHeight = Math.ceil((maxY - minY) / cellSize);\n\n\t// Initialize grid\n\tconst grid: GridCell[][] = Array.from({ length: gridWidth }, () =>\n\t\tArray.from({ length: gridHeight }, () => ({ boxes: [] })),\n\t);\n\t// Assign boxes from Set B to grid cells\n\tfor (const box of boxSetB) {\n\t\tconst bbox = getBoundingBox(box);\n\t\tconst startX = Math.floor((bbox.minX - minX) / cellSize);\n\t\tconst endX = Math.floor((bbox.maxX - minX) / cellSize);\n\t\tconst startY = Math.floor((bbox.minY - minY) / cellSize);\n\t\tconst endY = Math.floor((bbox.maxY - minY) / cellSize);\n\n\t\tfor (let x = startX; x <= endX; x++) {\n\t\t\tfor (let y = startY; y <= endY; y++) {\n\t\t\t\tgrid[x][y].boxes.push(box);\n\t\t\t}\n\t\t}\n\t}\n\n\tlet minDistance = Number.POSITIVE_INFINITY;\n\tlet nearestPointA: Point = { x: 0, y: 0 };\n\tlet nearestPointB: Point = { x: 0, y: 0 };\n\n\t// Process boxes in Set A\n\tfor (const boxA of boxSetA) {\n\t\tconst bboxA = getBoundingBox(boxA);\n\t\tconst startX = Math.floor((bboxA.minX - minX) / cellSize);\n\t\tconst endX = Math.floor((bboxA.maxX - minX) / cellSize);\n\t\tconst startY = Math.floor((bboxA.minY - minY) / cellSize);\n\t\tconst endY = Math.floor((bboxA.maxY - minY) / cellSize);\n\n\t\t// Consider neighboring cells as well\n\t\tfor (let x = startX - 1; x <= endX + 1; x++) {\n\t\t\tif (x < 0 || x >= gridWidth) continue;\n\t\t\tfor (let y = startY - 1; y <= endY + 1; y++) {\n\t\t\t\tif (y < 0 || y >= gridHeight) continue;\n\n\t\t\t\tconst cell = grid[x][y];\n\t\t\t\tfor (const boxB of cell.boxes) {\n\t\t\t\t\tconst { distance, pointA, pointB } = computeDistanceBetweenBoxes(\n\t\t\t\t\t\tboxA,\n\t\t\t\t\t\tboxB,\n\t\t\t\t\t);\n\t\t\t\t\tif (distance < minDistance) {\n\t\t\t\t\t\tminDistance = distance;\n\t\t\t\t\t\tnearestPointA = pointA;\n\t\t\t\t\t\tnearestPointB = pointB;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\tpointA: nearestPointA,\n\t\tpointB: nearestPointB,\n\t\tdistance: minDistance,\n\t};\n}\n"],"mappings":";AAOO,SAAS,eAAe,KAAU;AACxC,QAAM,YAAY,IAAI,QAAQ;AAC9B,QAAM,aAAa,IAAI,SAAS;AAChC,SAAO;AAAA,IACN,MAAM,IAAI,OAAO,IAAI;AAAA,IACrB,MAAM,IAAI,OAAO,IAAI;AAAA,IACrB,MAAM,IAAI,OAAO,IAAI;AAAA,IACrB,MAAM,IAAI,OAAO,IAAI;AAAA,EACtB;AACD;AAEO,SAAS,4BACf,MACA,MACqD;AACrD,QAA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