@tscircuit/math-utils 0.0.2 → 0.0.4
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-GYQ2KZV6.js +1 -0
- package/dist/chunk-GYQ2KZV6.js.map +1 -0
- package/dist/chunk-KHV6IODO.js +76 -0
- package/dist/chunk-KHV6IODO.js.map +1 -0
- package/dist/chunk-MHHTZHOJ.js +62 -0
- package/dist/chunk-MHHTZHOJ.js.map +1 -0
- package/dist/common.d.ts +6 -0
- package/dist/common.js +2 -0
- package/dist/common.js.map +1 -0
- package/dist/index.d.ts +3 -32
- package/dist/index.js +19 -66
- package/dist/index.js.map +1 -1
- package/dist/line-intersections.d.ts +30 -0
- package/dist/line-intersections.js +17 -0
- package/dist/line-intersections.js.map +1 -0
- package/dist/nearest-box.d.ts +30 -0
- package/dist/nearest-box.js +13 -0
- package/dist/nearest-box.js.map +1 -0
- package/package.json +19 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-GYQ2KZV6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// src/line-intersections.ts
|
|
2
|
+
function doesLineIntersectLine([a1, a2], [b1, b2], {
|
|
3
|
+
lineThickness = 0
|
|
4
|
+
} = {}) {
|
|
5
|
+
if (lineThickness === 0) {
|
|
6
|
+
return doSegmentsIntersect(a1, a2, b1, b2);
|
|
7
|
+
}
|
|
8
|
+
const minDist = segmentsDistance(a1, a2, b1, b2);
|
|
9
|
+
return minDist <= lineThickness;
|
|
10
|
+
}
|
|
11
|
+
function doSegmentsIntersect(p1, q1, p2, q2) {
|
|
12
|
+
const o1 = orientation(p1, q1, p2);
|
|
13
|
+
const o2 = orientation(p1, q1, q2);
|
|
14
|
+
const o3 = orientation(p2, q2, p1);
|
|
15
|
+
const o4 = orientation(p2, q2, q1);
|
|
16
|
+
if (o1 !== o2 && o3 !== o4) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if (o1 === 0 && onSegment(p1, p2, q1)) return true;
|
|
20
|
+
if (o2 === 0 && onSegment(p1, q2, q1)) return true;
|
|
21
|
+
if (o3 === 0 && onSegment(p2, p1, q2)) return true;
|
|
22
|
+
if (o4 === 0 && onSegment(p2, q1, q2)) return true;
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
function orientation(p, q, r) {
|
|
26
|
+
const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
|
|
27
|
+
if (val === 0) return 0;
|
|
28
|
+
return val > 0 ? 1 : 2;
|
|
29
|
+
}
|
|
30
|
+
function onSegment(p, q, r) {
|
|
31
|
+
return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
|
|
32
|
+
}
|
|
33
|
+
function segmentsDistance(a1, a2, b1, b2) {
|
|
34
|
+
if (a1.x === a2.x && a1.y === a2.y) {
|
|
35
|
+
return pointToSegmentDistance(a1, b1, b2);
|
|
36
|
+
}
|
|
37
|
+
if (b1.x === b2.x && b1.y === b2.y) {
|
|
38
|
+
return pointToSegmentDistance(b1, a1, a2);
|
|
39
|
+
}
|
|
40
|
+
if (doSegmentsIntersect(a1, a2, b1, b2)) {
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
const distances = [
|
|
44
|
+
pointToSegmentDistance(a1, b1, b2),
|
|
45
|
+
pointToSegmentDistance(a2, b1, b2),
|
|
46
|
+
pointToSegmentDistance(b1, a1, a2),
|
|
47
|
+
pointToSegmentDistance(b2, a1, a2)
|
|
48
|
+
];
|
|
49
|
+
return Math.min(...distances);
|
|
50
|
+
}
|
|
51
|
+
function pointToSegmentDistance(p, v, w) {
|
|
52
|
+
const l2 = (w.x - v.x) ** 2 + (w.y - v.y) ** 2;
|
|
53
|
+
if (l2 === 0) return distance(p, v);
|
|
54
|
+
let t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
|
|
55
|
+
t = Math.max(0, Math.min(1, t));
|
|
56
|
+
const projection = {
|
|
57
|
+
x: v.x + t * (w.x - v.x),
|
|
58
|
+
y: v.y + t * (w.y - v.y)
|
|
59
|
+
};
|
|
60
|
+
return distance(p, projection);
|
|
61
|
+
}
|
|
62
|
+
function distance(p1, p2) {
|
|
63
|
+
const dx = p1.x - p2.x;
|
|
64
|
+
const dy = p1.y - p2.y;
|
|
65
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export {
|
|
69
|
+
doesLineIntersectLine,
|
|
70
|
+
doSegmentsIntersect,
|
|
71
|
+
orientation,
|
|
72
|
+
onSegment,
|
|
73
|
+
pointToSegmentDistance,
|
|
74
|
+
distance
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=chunk-KHV6IODO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/line-intersections.ts"],"sourcesContent":["import type { Point } from \"./common\";\n\n/**\n * Returns true if the two lines intersect.\n */\nexport function doesLineIntersectLine(\n\t[a1, a2]: [Point, Point],\n\t[b1, b2]: [Point, Point],\n\t{\n\t\tlineThickness = 0,\n\t}: {\n\t\tlineThickness?: number;\n\t} = {},\n): boolean {\n\tif (lineThickness === 0) {\n\t\treturn doSegmentsIntersect(a1, a2, b1, b2);\n\t}\n\tconst minDist = segmentsDistance(a1, a2, b1, b2);\n\treturn minDist <= lineThickness;\n}\n\n/**\n * Returns true if the two segments intersect.\n */\nexport function doSegmentsIntersect(\n\tp1: Point,\n\tq1: Point,\n\tp2: Point,\n\tq2: Point,\n): boolean {\n\tconst o1 = orientation(p1, q1, p2);\n\tconst o2 = orientation(p1, q1, q2);\n\tconst o3 = orientation(p2, q2, p1);\n\tconst o4 = orientation(p2, q2, q1);\n\n\t// General case\n\tif (o1 !== o2 && o3 !== o4) {\n\t\treturn true;\n\t}\n\n\t// Special Cases\n\tif (o1 === 0 && onSegment(p1, p2, q1)) return true;\n\tif (o2 === 0 && onSegment(p1, q2, q1)) return true;\n\tif (o3 === 0 && onSegment(p2, p1, q2)) return true;\n\tif (o4 === 0 && onSegment(p2, q1, q2)) return true;\n\n\treturn false;\n}\n\n/**\n * Returns 0 if the points are colinear, 1 if they are clockwise, and 2 if they are counterclockwise.\n */\nexport function orientation(p: Point, q: Point, r: Point): number {\n\tconst val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);\n\tif (val === 0) return 0; // colinear\n\treturn val > 0 ? 1 : 2; // clock or counterclock wise\n}\n\n/**\n * Returns true if q is on the segment p->r.\n */\nexport function onSegment(p: Point, q: Point, r: Point): boolean {\n\treturn (\n\t\tq.x <= Math.max(p.x, r.x) &&\n\t\tq.x >= Math.min(p.x, r.x) &&\n\t\tq.y <= Math.max(p.y, r.y) &&\n\t\tq.y >= Math.min(p.y, r.y)\n\t);\n}\n\n/**\n * Returns the minimum distance between two segments.\n */\nfunction segmentsDistance(a1: Point, a2: Point, b1: Point, b2: Point): number {\n\t// Handle degenerate cases: segments of zero length\n\tif (a1.x === a2.x && a1.y === a2.y) {\n\t\treturn pointToSegmentDistance(a1, b1, b2);\n\t}\n\tif (b1.x === b2.x && b1.y === b2.y) {\n\t\treturn pointToSegmentDistance(b1, a1, a2);\n\t}\n\n\t// Check if segments intersect\n\tif (doSegmentsIntersect(a1, a2, b1, b2)) {\n\t\treturn 0;\n\t}\n\n\t// Compute the minimum distance between the segments\n\tconst distances = [\n\t\tpointToSegmentDistance(a1, b1, b2),\n\t\tpointToSegmentDistance(a2, b1, b2),\n\t\tpointToSegmentDistance(b1, a1, a2),\n\t\tpointToSegmentDistance(b2, a1, a2),\n\t];\n\n\treturn Math.min(...distances);\n}\n\n/**\n * Returns the minimum distance between a point and a segment.\n */\nexport function pointToSegmentDistance(p: Point, v: Point, w: Point): number {\n\tconst l2 = (w.x - v.x) ** 2 + (w.y - v.y) ** 2;\n\tif (l2 === 0) return distance(p, v);\n\n\tlet t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;\n\tt = Math.max(0, Math.min(1, t));\n\n\tconst projection = {\n\t\tx: v.x + t * (w.x - v.x),\n\t\ty: v.y + t * (w.y - v.y),\n\t};\n\n\treturn distance(p, projection);\n}\n\n/**\n * Returns the distance between two points.\n */\nexport function distance(p1: Point, p2: Point): number {\n\tconst dx = p1.x - p2.x;\n\tconst dy = p1.y - p2.y;\n\treturn Math.sqrt(dx * dx + dy * dy);\n}\n"],"mappings":";AAKO,SAAS,sBACf,CAAC,IAAI,EAAE,GACP,CAAC,IAAI,EAAE,GACP;AAAA,EACC,gBAAgB;AACjB,IAEI,CAAC,GACK;AACV,MAAI,kBAAkB,GAAG;AACxB,WAAO,oBAAoB,IAAI,IAAI,IAAI,EAAE;AAAA,EAC1C;AACA,QAAM,UAAU,iBAAiB,IAAI,IAAI,IAAI,EAAE;AAC/C,SAAO,WAAW;AACnB;AAKO,SAAS,oBACf,IACA,IACA,IACA,IACU;AACV,QAAM,KAAK,YAAY,IAAI,IAAI,EAAE;AACjC,QAAM,KAAK,YAAY,IAAI,IAAI,EAAE;AACjC,QAAM,KAAK,YAAY,IAAI,IAAI,EAAE;AACjC,QAAM,KAAK,YAAY,IAAI,IAAI,EAAE;AAGjC,MAAI,OAAO,MAAM,OAAO,IAAI;AAC3B,WAAO;AAAA,EACR;AAGA,MAAI,OAAO,KAAK,UAAU,IAAI,IAAI,EAAE,EAAG,QAAO;AAC9C,MAAI,OAAO,KAAK,UAAU,IAAI,IAAI,EAAE,EAAG,QAAO;AAC9C,MAAI,OAAO,KAAK,UAAU,IAAI,IAAI,EAAE,EAAG,QAAO;AAC9C,MAAI,OAAO,KAAK,UAAU,IAAI,IAAI,EAAE,EAAG,QAAO;AAE9C,SAAO;AACR;AAKO,SAAS,YAAY,GAAU,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E,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;AAClE,MAAI,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,CAAC,CAAC;AAE9B,QAAM,aAAa;AAAA,IAClB,GAAG,EAAE,IAAI,KAAK,EAAE,IAAI,EAAE;AAAA,IACtB,GAAG,EAAE,IAAI,KAAK,EAAE,IAAI,EAAE;AAAA,EACvB;AAEA,SAAO,SAAS,GAAG,UAAU;AAC9B;AAKO,SAAS,SAAS,IAAW,IAAmB;AACtD,QAAM,KAAK,GAAG,IAAI,GAAG;AACrB,QAAM,KAAK,GAAG,IAAI,GAAG;AACrB,SAAO,KAAK,KAAK,KAAK,KAAK,KAAK,EAAE;AACnC;","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,KAA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A,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":[]}
|
package/dist/common.d.ts
ADDED
package/dist/common.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
/**
|
|
6
|
-
* Returns true if the two lines intersect.
|
|
7
|
-
*/
|
|
8
|
-
declare function doesLineIntersectLine([a1, a2]: [Point, Point], [b1, b2]: [Point, Point], { lineThickness, }?: {
|
|
9
|
-
lineThickness?: number;
|
|
10
|
-
}): boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Returns true if the two segments intersect.
|
|
13
|
-
*/
|
|
14
|
-
declare function doSegmentsIntersect(p1: Point, q1: Point, p2: Point, q2: Point): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Returns 0 if the points are colinear, 1 if they are clockwise, and 2 if they are counterclockwise.
|
|
17
|
-
*/
|
|
18
|
-
declare function orientation(p: Point, q: Point, r: Point): number;
|
|
19
|
-
/**
|
|
20
|
-
* Returns true if q is on the segment p->r.
|
|
21
|
-
*/
|
|
22
|
-
declare function onSegment(p: Point, q: Point, r: Point): boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Returns the minimum distance between a point and a segment.
|
|
25
|
-
*/
|
|
26
|
-
declare function pointToSegmentDistance(p: Point, v: Point, w: Point): number;
|
|
27
|
-
/**
|
|
28
|
-
* Returns the distance between two points.
|
|
29
|
-
*/
|
|
30
|
-
declare function distance(p1: Point, p2: Point): number;
|
|
31
|
-
|
|
32
|
-
export { type Point, distance, doSegmentsIntersect, doesLineIntersectLine, onSegment, orientation, pointToSegmentDistance };
|
|
1
|
+
export { distance, doSegmentsIntersect, doesLineIntersectLine, onSegment, orientation, pointToSegmentDistance } from './line-intersections.js';
|
|
2
|
+
export { Box, BoxSet, GridCell, clamp, computeDistanceBetweenBoxes, findNearestPointsBetweenBoxSets, getBoundingBox } from './nearest-box.js';
|
|
3
|
+
export { Point } from './common.js';
|
package/dist/index.js
CHANGED
|
@@ -1,73 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (o1 !== o2 && o3 !== o4) {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
if (o1 === 0 && onSegment(p1, p2, q1)) return true;
|
|
20
|
-
if (o2 === 0 && onSegment(p1, q2, q1)) return true;
|
|
21
|
-
if (o3 === 0 && onSegment(p2, p1, q2)) return true;
|
|
22
|
-
if (o4 === 0 && onSegment(p2, q1, q2)) return true;
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
function orientation(p, q, r) {
|
|
26
|
-
const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
|
|
27
|
-
if (val === 0) return 0;
|
|
28
|
-
return val > 0 ? 1 : 2;
|
|
29
|
-
}
|
|
30
|
-
function onSegment(p, q, r) {
|
|
31
|
-
return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
|
|
32
|
-
}
|
|
33
|
-
function segmentsDistance(a1, a2, b1, b2) {
|
|
34
|
-
if (a1.x === a2.x && a1.y === a2.y) {
|
|
35
|
-
return pointToSegmentDistance(a1, b1, b2);
|
|
36
|
-
}
|
|
37
|
-
if (b1.x === b2.x && b1.y === b2.y) {
|
|
38
|
-
return pointToSegmentDistance(b1, a1, a2);
|
|
39
|
-
}
|
|
40
|
-
if (doSegmentsIntersect(a1, a2, b1, b2)) {
|
|
41
|
-
return 0;
|
|
42
|
-
}
|
|
43
|
-
const distances = [
|
|
44
|
-
pointToSegmentDistance(a1, b1, b2),
|
|
45
|
-
pointToSegmentDistance(a2, b1, b2),
|
|
46
|
-
pointToSegmentDistance(b1, a1, a2),
|
|
47
|
-
pointToSegmentDistance(b2, a1, a2)
|
|
48
|
-
];
|
|
49
|
-
return Math.min(...distances);
|
|
50
|
-
}
|
|
51
|
-
function pointToSegmentDistance(p, v, w) {
|
|
52
|
-
const l2 = (w.x - v.x) ** 2 + (w.y - v.y) ** 2;
|
|
53
|
-
if (l2 === 0) return distance(p, v);
|
|
54
|
-
let t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
|
|
55
|
-
t = Math.max(0, Math.min(1, t));
|
|
56
|
-
const projection = {
|
|
57
|
-
x: v.x + t * (w.x - v.x),
|
|
58
|
-
y: v.y + t * (w.y - v.y)
|
|
59
|
-
};
|
|
60
|
-
return distance(p, projection);
|
|
61
|
-
}
|
|
62
|
-
function distance(p1, p2) {
|
|
63
|
-
const dx = p1.x - p2.x;
|
|
64
|
-
const dy = p1.y - p2.y;
|
|
65
|
-
return Math.sqrt(dx * dx + dy * dy);
|
|
66
|
-
}
|
|
1
|
+
import "./chunk-GYQ2KZV6.js";
|
|
2
|
+
import {
|
|
3
|
+
distance,
|
|
4
|
+
doSegmentsIntersect,
|
|
5
|
+
doesLineIntersectLine,
|
|
6
|
+
onSegment,
|
|
7
|
+
orientation,
|
|
8
|
+
pointToSegmentDistance
|
|
9
|
+
} from "./chunk-KHV6IODO.js";
|
|
10
|
+
import {
|
|
11
|
+
clamp,
|
|
12
|
+
computeDistanceBetweenBoxes,
|
|
13
|
+
findNearestPointsBetweenBoxSets,
|
|
14
|
+
getBoundingBox
|
|
15
|
+
} from "./chunk-MHHTZHOJ.js";
|
|
67
16
|
export {
|
|
17
|
+
clamp,
|
|
18
|
+
computeDistanceBetweenBoxes,
|
|
68
19
|
distance,
|
|
69
20
|
doSegmentsIntersect,
|
|
70
21
|
doesLineIntersectLine,
|
|
22
|
+
findNearestPointsBetweenBoxSets,
|
|
23
|
+
getBoundingBox,
|
|
71
24
|
onSegment,
|
|
72
25
|
orientation,
|
|
73
26
|
pointToSegmentDistance
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Point } from './common.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns true if the two lines intersect.
|
|
5
|
+
*/
|
|
6
|
+
declare function doesLineIntersectLine([a1, a2]: [Point, Point], [b1, b2]: [Point, Point], { lineThickness, }?: {
|
|
7
|
+
lineThickness?: number;
|
|
8
|
+
}): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Returns true if the two segments intersect.
|
|
11
|
+
*/
|
|
12
|
+
declare function doSegmentsIntersect(p1: Point, q1: Point, p2: Point, q2: Point): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Returns 0 if the points are colinear, 1 if they are clockwise, and 2 if they are counterclockwise.
|
|
15
|
+
*/
|
|
16
|
+
declare function orientation(p: Point, q: Point, r: Point): number;
|
|
17
|
+
/**
|
|
18
|
+
* Returns true if q is on the segment p->r.
|
|
19
|
+
*/
|
|
20
|
+
declare function onSegment(p: Point, q: Point, r: Point): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the minimum distance between a point and a segment.
|
|
23
|
+
*/
|
|
24
|
+
declare function pointToSegmentDistance(p: Point, v: Point, w: Point): number;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the distance between two points.
|
|
27
|
+
*/
|
|
28
|
+
declare function distance(p1: Point, p2: Point): number;
|
|
29
|
+
|
|
30
|
+
export { distance, doSegmentsIntersect, doesLineIntersectLine, onSegment, orientation, pointToSegmentDistance };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
distance,
|
|
3
|
+
doSegmentsIntersect,
|
|
4
|
+
doesLineIntersectLine,
|
|
5
|
+
onSegment,
|
|
6
|
+
orientation,
|
|
7
|
+
pointToSegmentDistance
|
|
8
|
+
} from "./chunk-KHV6IODO.js";
|
|
9
|
+
export {
|
|
10
|
+
distance,
|
|
11
|
+
doSegmentsIntersect,
|
|
12
|
+
doesLineIntersectLine,
|
|
13
|
+
onSegment,
|
|
14
|
+
orientation,
|
|
15
|
+
pointToSegmentDistance
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=line-intersections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Point } from './common.js';
|
|
2
|
+
|
|
3
|
+
type Box = {
|
|
4
|
+
center: Point;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
};
|
|
8
|
+
type BoxSet = Box[];
|
|
9
|
+
type GridCell = {
|
|
10
|
+
boxes: Box[];
|
|
11
|
+
};
|
|
12
|
+
declare function getBoundingBox(box: Box): {
|
|
13
|
+
minX: number;
|
|
14
|
+
maxX: number;
|
|
15
|
+
minY: number;
|
|
16
|
+
maxY: number;
|
|
17
|
+
};
|
|
18
|
+
declare function computeDistanceBetweenBoxes(boxA: Box, boxB: Box): {
|
|
19
|
+
distance: number;
|
|
20
|
+
pointA: Point;
|
|
21
|
+
pointB: Point;
|
|
22
|
+
};
|
|
23
|
+
declare function clamp(value: number, min: number, max: number): number;
|
|
24
|
+
declare function findNearestPointsBetweenBoxSets(boxSetA: BoxSet, boxSetB: BoxSet): {
|
|
25
|
+
pointA: Point;
|
|
26
|
+
pointB: Point;
|
|
27
|
+
distance: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { type Box, type BoxSet, type GridCell, clamp, computeDistanceBetweenBoxes, findNearestPointsBetweenBoxSets, getBoundingBox };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clamp,
|
|
3
|
+
computeDistanceBetweenBoxes,
|
|
4
|
+
findNearestPointsBetweenBoxSets,
|
|
5
|
+
getBoundingBox
|
|
6
|
+
} from "./chunk-MHHTZHOJ.js";
|
|
7
|
+
export {
|
|
8
|
+
clamp,
|
|
9
|
+
computeDistanceBetweenBoxes,
|
|
10
|
+
findNearestPointsBetweenBoxSets,
|
|
11
|
+
getBoundingBox
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=nearest-box.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscircuit/math-utils",
|
|
3
3
|
"main": "dist/index.js",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./line-intersections": {
|
|
14
|
+
"import": "./dist/line-intersections.js",
|
|
15
|
+
"types": "./dist/line-intersections.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./nearest-box": {
|
|
18
|
+
"import": "./dist/nearest-box.js",
|
|
19
|
+
"types": "./dist/nearest-box.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
6
22
|
"files": [
|
|
7
23
|
"dist"
|
|
8
24
|
],
|
|
9
25
|
"scripts": {
|
|
10
|
-
"build": "tsup-node src
|
|
26
|
+
"build": "tsup-node src --format esm --dts --sourcemap"
|
|
11
27
|
},
|
|
12
28
|
"devDependencies": {
|
|
29
|
+
"@biomejs/biome": "^1.9.1",
|
|
13
30
|
"@types/bun": "latest",
|
|
14
31
|
"tsup": "^8.2.4"
|
|
15
32
|
},
|