1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-09-29 17:49:08 +02:00

projection, half moulding

This commit is contained in:
Pomax
2020-08-30 17:08:43 -07:00
parent 10148f46b1
commit e6608d65af
72 changed files with 448 additions and 251 deletions

View File

@@ -436,12 +436,13 @@ class GraphicsAPI extends BaseAPI {
/**
* Reset the canvas bitmap to a uniform color.
*/
clear(color = `white`) {
clear(color = `white`, preserveTransforms = false) {
this.ctx.cacheStyle();
this.resetTransform();
this.ctx.fillStyle = color;
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.restoreStyle();
if (!preserveTransforms) this.resetTransform();
}
/**

View File

@@ -34,8 +34,11 @@ class Bezier extends Original {
this.ctx = apiInstance.ctx;
}
getPointNear(point, d = 5) {
const { x, y } = point;
project(x, y) {
return super.project({ x, y });
}
getPointNear(x, y, d = 5) {
const p = this.points;
for (let i = 0, e = p.length; i < e; i++) {
let dx = Math.abs(p[i].x - x);
@@ -46,46 +49,6 @@ class Bezier extends Original {
}
}
getProjectionPoint(point) {
const { x, y } = point;
// project this point onto the curve and return _that_ point
const n = this.lut.length - 1,
p = this.points;
let d,
closest,
smallestDistance = Number.MAX_SAFE_INTEGER;
// coarse check
this.lut.forEach((p, i) => {
d = p.dist(x, y);
if (d < smallestDistance) {
smallestDistance = d;
p.t = i / n;
closest = p;
}
});
// fine check
for (let o = -0.1, t, np, st = closest.t; o <= 0.1; o += 0.005) {
t = st + o;
if (t < 0) continue;
if (t > 1) continue;
np = new Point(
compute(t, p[0].x, p[1].x, p[2].x, p[3].x),
compute(t, p[0].y, p[1].y, p[2].y, p[3].y)
);
d = np.dist(x, y);
if (d < smallestDistance) {
smallestDistance = d;
closest = np;
closest.t = t;
}
}
return closest;
}
drawCurve(color = `#333`) {
const ctx = this.ctx;
ctx.cacheStyle();