one step forward, one step back

This commit is contained in:
2025-02-09 05:23:30 -07:00
parent 3d4596f8fb
commit 68eec35ea2
11 changed files with 797 additions and 49 deletions

View File

@@ -2,10 +2,12 @@ import { Vector } from "@bearmetal/doodler";
export class ComplexPath {
points: Vector[] = [];
segments: PathSegment[] = [];
radius = 50;
ctx?: CanvasRenderingContext2D;
evenPoints: Vector[] = [];
constructor(points?: Vector[]) {
points && (this.points = points);
@@ -35,6 +37,52 @@ export class ComplexPath {
}
ctx.restore();
}
followEvenPoints(t: number) {
if (t < 0) t += this.evenPoints.length;
const i = Math.floor(t);
const a = this.evenPoints[i];
const b = this.evenPoints[(i + 1) % this.evenPoints.length];
return Vector.lerp(a, b, t % 1);
}
calculateEvenlySpacedPoints(spacing: number, resolution = 1) {
// this.pointSpacing = 1;
// return this.segments.flatMap(s => s.calculateEvenlySpacedPoints(spacing, resolution));
const points: Vector[] = [];
points.push(this.segments[0].points[0]);
let prev = points[0];
let distSinceLastEvenPoint = 0;
for (const seg of this.segments) {
let t = 0;
const div = Math.ceil(seg.length * resolution * 10);
while (t < 1) {
t += 1 / div;
const point = seg.getPointAtT(t);
distSinceLastEvenPoint += prev.dist(point);
if (distSinceLastEvenPoint >= spacing) {
const overshoot = distSinceLastEvenPoint - spacing;
const evenPoint = Vector.add(
point,
Vector.sub(point, prev).normalize().mult(overshoot),
);
distSinceLastEvenPoint = overshoot;
points.push(evenPoint);
prev = evenPoint;
}
prev = point;
}
}
this.evenPoints = points;
return points;
}
}
export class PathSegment {
@@ -43,6 +91,9 @@ export class PathSegment {
length: number;
startingLength: number;
next?: PathSegment;
prev?: PathSegment;
constructor(points: [Vector, Vector, Vector, Vector]) {
this.points = points;
this.length = this.calculateApproxLength(100);
@@ -254,4 +305,6 @@ export class PathSegment {
this.points[3].set(points[curveLength]);
}
}
draw(): void {}
}