Added evenly spaced points to paths

This commit is contained in:
Emma
2023-02-08 01:14:13 -07:00
parent ae0575875f
commit 9b03e6c2cb
5 changed files with 164 additions and 54 deletions

View File

@@ -95,8 +95,11 @@ export class Track extends PathSegment {
export class Spline<T extends PathSegment = PathSegment> {
segments: T[] = [];
ctx?: CanvasRenderingContext2D;
evenPoints: Vector[];
constructor(segs: T[]) {
this.segments = segs;
this.evenPoints = this.calculateEvenlySpacedPoints(3);
}
setContext(ctx: CanvasRenderingContext2D) {
@@ -111,6 +114,52 @@ export class Spline<T extends PathSegment = PathSegment> {
segment.draw();
}
}
calculateEvenlySpacedPoints(spacing: number, resolution = 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
// }
// }
// return points;
}
followEvenPoints(t: number) {
const i = Math.floor(t);
const a = this.evenPoints[i]
const b = this.evenPoints[(i + 1) % this.evenPoints.length]
try {
return Vector.lerp(a, b, t % 1);
} catch {
console.log(t, i, a, b);
}
}
}
export const generateSquareTrack = () => {