-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHermiteSegment.js
81 lines (76 loc) · 1.94 KB
/
HermiteSegment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class HermiteSegment {
/**
* @param {PointWithTangent} p1
* @param {PointWithTangent} p2
* @param {Number} step
* @param {CanvasRenderingContext2D} ctx
* @param {CanvasRenderingContext2D} highlightCtx
*/
constructor(p1, p2, step, ctx, highlightCtx) {
this.p1 = p1;
this.p2 = p2;
this.#setHermitePolys();
this.step = step;
this.ctx = ctx;
this.highlightCtx = highlightCtx;
}
draw() {
if (this.p1.modified || this.p2.modified) {
this.#setHermitePolys();
}
this.ctx.beginPath();
this.ctx.moveTo(...this.#segmentPoint(0));
for (let i = this.step; i < 1; i += this.step) {
this.ctx.lineTo(...this.#segmentPoint(i));
}
this.ctx.lineTo(...this.#segmentPoint(1));
this.ctx.stroke();
}
highlight() {
// if (this.p1.modified || this.p2.modified) {
// this.#setHermitePolys();
// }
this.highlightCtx.beginPath();
this.highlightCtx.moveTo(...this.#segmentPoint(0));
for (let i = this.step; i < 1; i += this.step) {
this.highlightCtx.lineTo(...this.#segmentPoint(i));
}
this.highlightCtx.lineTo(...this.#segmentPoint(1));
this.highlightCtx.stroke();
}
/**
* @param {Number} val
* @returns {Array<Number>}
*/
#segmentPoint(val) {
return [this.polyX.eval(val), this.polyY.eval(val)];
}
#setHermitePolys() {
this.polyX = this.#getHermitePoly(
this.p1.point.x,
this.p1.tangent.x,
this.p2.point.x,
this.p2.tangent.x,
);
this.polyY = this.#getHermitePoly(
this.p1.point.y,
this.p1.tangent.y,
this.p2.point.y,
this.p2.tangent.y,
);
}
/**
* @param {Number} n1
* @param {Number} n1Tangent
* @param {Number} n2
* @param {Number} n2Tangent
*/
#getHermitePoly(n1, n1Tangent, n2, n2Tangent) {
return new Polynomial3(
n1,
n1Tangent,
-3 * n1 + 3 * n2 - 2 * n1Tangent - n2Tangent,
2 * n1 - 2 * n2 + n1Tangent + n2Tangent,
);
}
}