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

@@ -54,8 +54,8 @@ export class PathSegment {
}
draw() {
const [a,b,c,d] = this.points;
doodler.drawBezier(a,b,c,d, {
const [a, b, c, d] = this.points;
doodler.drawBezier(a, b, c, d, {
strokeColor: '#ffffff50'
})
// if (!this.ctx) return;
@@ -120,7 +120,7 @@ export class PathSegment {
const point = this.getPointAtT(i * resolution);
const distance = v.dist(point);
if (distance < r) {
points.push([i * resolution,this]);
points.push([i * resolution, this]);
}
}
return points
@@ -178,18 +178,48 @@ export class PathSegment {
}
calculateApproxLength(resolution = 25) {
const stepSize = 1/resolution;
const stepSize = 1 / resolution;
const points: Vector[] = []
for (let i = 0; i <= resolution; i++) {
const current = stepSize*i;
const current = stepSize * i;
points.push(this.getPointAtT(current))
}
return points.reduce((acc:{prev?: Vector, length: number}, cur) => {
return points.reduce((acc: { prev?: Vector, length: number }, cur) => {
const prev = acc.prev;
acc.prev = cur;
if (!prev) return acc;
acc.length += cur.dist(prev);
return acc;
}, {prev: undefined, length: 0}).length
}, { prev: undefined, length: 0 }).length
}
calculateEvenlySpacedPoints(spacing: number, resolution = 1) {
const points: Vector[] = []
points.push(this.points[0]);
let prev = points[0];
let distSinceLastEvenPoint = 0
let t = 0;
const div = Math.ceil(this.length * resolution * 10);
while (t < 1) {
t += 1 / div;
const point = this.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;
}
}