aboutsummaryrefslogtreecommitdiffstats
path: root/packages/math/line.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/math/line.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/math/line.ts')
-rw-r--r--packages/math/line.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/packages/math/line.ts b/packages/math/line.ts
new file mode 100644
index 0000000..bcb4f6d
--- /dev/null
+++ b/packages/math/line.ts
@@ -0,0 +1,38 @@
+import { pointFrom } from "./point";
+import type { GlobalPoint, Line, LocalPoint } from "./types";
+
+/**
+ * Create a line from two points.
+ *
+ * @param points The two points lying on the line
+ * @returns The line on which the points lie
+ */
+export function line<P extends GlobalPoint | LocalPoint>(a: P, b: P): Line<P> {
+ return [a, b] as Line<P>;
+}
+
+/**
+ * Determines the intersection point (unless the lines are parallel) of two
+ * lines
+ *
+ * @param a
+ * @param b
+ * @returns
+ */
+export function linesIntersectAt<Point extends GlobalPoint | LocalPoint>(
+ a: Line<Point>,
+ b: Line<Point>,
+): Point | null {
+ const A1 = a[1][1] - a[0][1];
+ const B1 = a[0][0] - a[1][0];
+ const A2 = b[1][1] - b[0][1];
+ const B2 = b[0][0] - b[1][0];
+ const D = A1 * B2 - A2 * B1;
+ if (D !== 0) {
+ const C1 = A1 * a[0][0] + B1 * a[0][1];
+ const C2 = A2 * b[0][0] + B2 * b[0][1];
+ return pointFrom<Point>((C1 * B2 - C2 * B1) / D, (A1 * C2 - A2 * C1) / D);
+ }
+
+ return null;
+}