1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-08-22 00:07:10 +02:00

intersections

This commit is contained in:
Pomax
2020-08-29 08:40:36 -07:00
parent 4e34774afb
commit bdc7c4228d
13 changed files with 201 additions and 89 deletions

View File

@@ -0,0 +1,53 @@
const utils = Bezier.getUtils();
let Point = Vector, curve, line;
setup() {
const type = this.parameters.type ?? `quadratic`;
if (type === `quadratic`) {
curve = Bezier.defaultQuadratic(this);
line = [new Point(15,250), new Point(220,20)];
} else {
curve = Bezier.defaultCubic(this);
line = [new Point(25,260), new Point(240,55)];
}
setMovable(curve.points, line);
}
draw() {
clear();
curve.drawSkeleton();
curve.drawCurve();
curve.drawPoints();
this.drawLine(...line);
const [p1, p2] = line;
const aligned = utils.align(curve.points, {p1,p2});
const nB = new Bezier(this, aligned);
const roots = utils.roots(nB.points);
const coords = roots.map(t => curve.get(t));
if (roots.length) {
roots.forEach((t,i) => {
var p = coords[i];
setColor(`magenta`);
circle(p.x, p.y, 3);
setColor(`black`);
text(`t = ${t.toFixed(2)}`, p.x + 5, p.y + 10);
});
setFill(`black`);
const cval = coords.map(p => `(${p.x|0},${p.y|0})`).join(`, `);
text(`Intersection${roots.length>=1?`s`:``} at ${cval}`, this.width/2, 15, CENTER);
}
}
drawLine(p1, p2) {
setStroke(`black`);
line(p1.x, p1.y, p2.x, p2.y);
setStroke( randomColor() );
circle(p1.x, p1.y, 3);
circle(p2.x, p2.y, 3);
}