@tscircuit/math-utils 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 tscircuit Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # @tscircuit/math-utils
2
+
3
+ This repository contains a collection of TypeScript utility functions for geometric calculations, primarily focused on line intersection and distance calculations.
4
+
5
+ ## Features
6
+
7
+ - Line intersection detection
8
+ - Segment intersection detection
9
+ - Point-to-segment distance calculation
10
+ - Orientation of points
11
+ - Distance between points
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ bun add @tscircuit/math-utils
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Import the functions you need in your TypeScript project:
22
+
23
+ ```typescript
24
+ import {
25
+ doesLineIntersectLine,
26
+ doSegmentsIntersect,
27
+ pointToSegmentDistance,
28
+ } from "./src/index"
29
+
30
+ // Example usage
31
+ const point1 = { x: 0, y: 0 }
32
+ const point2 = { x: 5, y: 5 }
33
+ const point3 = { x: 0, y: 5 }
34
+ const point4 = { x: 5, y: 0 }
35
+
36
+ const intersects = doesLineIntersectLine([point1, point2], [point3, point4])
37
+ console.log("Lines intersect:", intersects)
38
+ ```
39
+
40
+ ## API Reference
41
+
42
+ ### `doesLineIntersectLine(line1: [Point, Point], line2: [Point, Point], options?: { lineThickness?: number }): boolean`
43
+
44
+ Determines if two lines intersect, optionally considering line thickness.
45
+
46
+ ### `doSegmentsIntersect(p1: Point, q1: Point, p2: Point, q2: Point): boolean`
47
+
48
+ Checks if two line segments intersect.
49
+
50
+ ### `orientation(p: Point, q: Point, r: Point): number`
51
+
52
+ Calculates the orientation of three points.
53
+
54
+ ### `onSegment(p: Point, q: Point, r: Point): boolean`
55
+
56
+ Checks if point q lies on the segment p-r.
57
+
58
+ ### `pointToSegmentDistance(p: Point, v: Point, w: Point): number`
59
+
60
+ Calculates the minimum distance between a point and a line segment.
61
+
62
+ ### `distance(p1: Point, p2: Point): number`
63
+
64
+ Calculates the Euclidean distance between two points.
65
+
66
+ ## Contributing
67
+
68
+ Contributions are welcome! Please feel free to submit a Pull Request.
69
+
70
+ ## License
71
+
72
+ This project is open source and available under the [MIT License](LICENSE).
@@ -0,0 +1,32 @@
1
+ type Point = {
2
+ x: number;
3
+ y: number;
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 };
package/dist/index.js ADDED
@@ -0,0 +1,75 @@
1
+ // src/index.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
+ export {
68
+ distance,
69
+ doSegmentsIntersect,
70
+ doesLineIntersectLine,
71
+ onSegment,
72
+ orientation,
73
+ pointToSegmentDistance
74
+ };
75
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type Point = {\n\tx: number;\n\ty: number;\n};\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":";AAQO,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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AAEA,SAAO,KAAK,IAAI,GAAG,SAAS;AAC7B;AAKO,SAAS,uBAAuB,GAAU,GAAU,GAAkB;AAC5E,QAAM,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,EAAE,IAAI,EAAE,MAAM;AAC7C,MAAI,OAAO,EAAG,QAAO,SAAS,GAAG,CAAC;AAElC,MAAI,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,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":[]}
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@tscircuit/math-utils",
3
+ "main": "dist/index.js",
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "scripts": {
10
+ "build": "tsup-node src/index.ts --format esm --dts --sourcemap"
11
+ },
12
+ "devDependencies": {
13
+ "@types/bun": "latest",
14
+ "tsup": "^8.2.4"
15
+ },
16
+ "peerDependencies": {
17
+ "typescript": "^5.0.0"
18
+ }
19
+ }