Added evenly spaced points to paths
This commit is contained in:
49
track.ts
49
track.ts
@@ -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 = () => {
|
||||
|
Reference in New Issue
Block a user