Tackle unstable cubic root calc

feat/remove-ga
Mark Tolmacs 3 weeks ago
parent a938bb123d
commit dafccacbd3
No known key found for this signature in database

@ -44,22 +44,16 @@ describe("Math curve", () => {
const l = line(pointFrom(0, 112.5), pointFrom(90, 0));
expect(curveIntersectLine(c, l)).toCloselyEqualPoints([
[49.99999999999996, 49.99999999999997],
[70.47732960327718, 24.403337995903534],
[10.970762294018797, 98.78654713247653],
[49.99999999999996, 50],
]);
});
it("regression 1", () => {
it("can be detected where the determinant is overly precise", () => {
const c = curve(
// pointFrom(41.028864759926016, 12.226249068355052),
// pointFrom(41.028864759926016, 33.55958240168839),
// pointFrom(30.362198093259348, 44.22624906835505),
// pointFrom(9.028864759926016, 44.22624906835505),
pointFrom(41.02886475, 12.22624906),
pointFrom(41.02886475, 33.5595824),
pointFrom(30.362198093, 44.226249),
pointFrom(9.02886475, 44.226249),
pointFrom(41.028864759926016, 12.226249068355052),
pointFrom(41.028864759926016, 33.55958240168839),
pointFrom(30.362198093259348, 44.22624906835505),
pointFrom(9.028864759926016, 44.22624906835505),
);
const l = line(
pointFrom(-82.30963544324186, -41.19949363038283),
@ -67,7 +61,9 @@ describe("Math curve", () => {
pointFrom(188.2149592542487, 134.75505940984908),
);
expect(curveIntersectLine(c, l)).toCloselyEqualPoints([[8.5, 87.5]]);
expect(curveIntersectLine(c, l)).toCloselyEqualPoints([
[34.4078, 34.7168],
]);
});
});

@ -1,5 +1,3 @@
import { P } from "vitest/dist/chunks/environment.0M5R1SX_";
import { add, complex, conjugate, mul, pow, sqrt, sub } from "./complex";
import { isPoint, pointDistance, pointFrom } from "./point";
import type { Curve, GlobalPoint, Line, LocalPoint } from "./types";
@ -35,13 +33,37 @@ export function curveIntersectLine<Point extends GlobalPoint | LocalPoint>(
c: Curve<Point>,
l: Line<Point>,
): Point[] {
const C1 = pointFrom<Point>(
Math.round(c[0][0] * 1e6) / 1e6,
Math.round(c[0][1] * 1e6) / 1e6,
);
const C2 = pointFrom<Point>(
Math.round(c[1][0] * 1e6) / 1e6,
Math.round(c[1][1] * 1e6) / 1e6,
);
const C3 = pointFrom<Point>(
Math.round(c[2][0] * 1e6) / 1e6,
Math.round(c[2][1] * 1e6) / 1e6,
);
const C4 = pointFrom<Point>(
Math.round(c[3][0] * 1e6) / 1e6,
Math.round(c[3][1] * 1e6) / 1e6,
);
const L1 = pointFrom<Point>(
Math.round(l[0][0] * 1e6) / 1e6,
Math.round(l[0][1] * 1e6) / 1e6,
);
const L2 = pointFrom<Point>(
Math.round(l[1][0] * 1e6) / 1e6,
Math.round(l[1][1] * 1e6) / 1e6,
);
const [px, py] = [
[c[0][0], c[1][0], c[2][0], c[3][0]],
[c[0][1], c[1][1], c[2][1], c[3][1]],
[C1[0], C2[0], C3[0], C4[0]],
[C1[1], C2[1], C3[1], C4[1]],
];
const [lx, ly] = [
[l[0][0], l[1][0]],
[l[0][1], l[1][1]],
[L1[0], L2[0]],
[L1[1], L2[1]],
];
const X = [];
@ -78,12 +100,17 @@ export function curveIntersectLine<Point extends GlobalPoint | LocalPoint>(
// s = (X[1] - ly[0]) / (ly[1] - ly[0]);
// }
// /*in bounds?*/
// if (t < 0 || t > 1.0 || s < 0 || s > 1.0) {
// X[0] = -100; /*move off screen*/
// X[1] = -100;
// }
console.log(X[0] + " " + X[1]);
/*in bounds?*/
if (
t < 0 ||
t > 1.0 //||
// s < 0 ||
// s > 1.0
) {
X[0] = -100; /*move off screen*/
X[1] = -100;
}
if (!isNaN(X[0]) && !isNaN(X[1])) {
intersections.push(pointFrom(X[0], X[1]));
}
@ -120,7 +147,6 @@ function cubicRoots([a, b, c, d]: number[]): number[] {
/*discard complex roots*/
if (Im !== 0) {
console.log(t);
t[1] = -1;
t[2] = -1;
}
@ -141,9 +167,6 @@ function cubicRoots([a, b, c, d]: number[]): number[] {
}
}
/*sort but place -1 at the end*/
console.log(t[0] + " " + t[1] + " " + t[2]);
return t.filter((t) => t !== -1);
}

Loading…
Cancel
Save