1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-08-22 00:07:10 +02:00
This commit is contained in:
Pomax
2020-08-29 15:29:49 -07:00
parent 2e66f90a69
commit 10148f46b1
18 changed files with 229 additions and 336 deletions

71
docs/chapters/abc/abc.js Normal file
View File

@@ -0,0 +1,71 @@
let curve, utils = Bezier.getUtils();
setup() {
const type = this.parameters.type ?? `quadratic`;
curve = (type === `quadratic`) ? Bezier.defaultQuadratic(this) : Bezier.defaultCubic(this);
curve.points.forEach(p => p.y -= 20);
setMovable(curve.points);
setSlider(`.slide-control`, `position`, 0.5);
}
/**
* The master draw function for the `projection` sketches
*/
draw() {
clear();
curve.drawSkeleton();
curve.drawCurve();
curve.drawPoints();
const t = this.position;
const p = curve.get(t);
setStroke(`black`);
circle(p.x, p.y, 2);
// find the A/B/C values as described in the section text
const hull = curve.drawStruts(t, `lightblue`);
let A, B, C;
setStroke(`lightgrey`);
if(hull.length === 6) {
A = curve.points[1];
B = hull[5];
let p1 = curve.points[0];
let p2 = curve.points[2];
C = utils.lli4(A, B, p1, p2);
line(p1.x, p1.y, p2.x, p2.y);
} else if(hull.length === 10) {
A = hull[5];
B = hull[9];
let p1 = curve.points[0];
let p2 = curve.points[3];
C = utils.lli4(A, B, p1, p2);
line(p1.x, p1.y, p2.x, p2.y);
}
this.drawABCdata(t, A, B, C);
}
drawABCdata(t, A, B, C) {
// show the lines between the A/B/C values
setStroke(`#00FF00`);
line(A.x, A.y, B.x, B.y);
setStroke(`red`);
line(B.x, B.y, C.x, C.y);
setStroke(`black`);
circle(C.x, C.y, 3);
// with their associated labels
setFill(`black`);
text(`A`, 10 + A.x, A.y);
text(`B (t = ${t.toFixed(2)})`, 10 + B.x, B.y);
text(`C`, 10 + C.x, C.y);
// and show the distance ratio, which we see does not change irrespective of whether A/B/C change.
const d1 = dist(A.x, A.y, B.x, B.y);
const d2 = dist(B.x, B.y, C.x, C.y);
const ratio = d1/d2;
text(`d1 = A-B: ${d1.toFixed(2)}, d2 = B-C: ${d2.toFixed(2)}, d1/d2: ${ratio.toFixed(4)}`, 10, this.height-7);
}

View File

@@ -6,78 +6,69 @@ How does that work? Succinctly: we run de Casteljau's algorithm in reverse!
In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated *t* value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious. In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated *t* value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.
So let's use graphics instead of text to see where this "A" is, because text only gets us so far: in the following graphic, click anywhere on the curves to see the identity information that we'll be using to run de Casteljau in reverse (you can manipulate the curve even after picking a point. Note the "ratio" value when you do so: does it change?): So let's use graphics instead of text to see where this "A" is, because text only gets us so far: move the sliders for the following graphics to see what, given specific `t` value, our `A` coordinate is. As well as some other coordinates, which taken together let us derive a value that the graphics call "ratio": if you move the curve's points around, A, B, and C will move, what happens to that value?
<div class="figure"> <div class="figure">
<Graphic inline={true} title="Projections in a quadratic Bézier curve" setup={this.setupQuadratic} draw={this.draw} onClick={this.onClick} />
<Graphic inline={true} title="Projections in a cubic Bézier curve" setup={this.setupCubic} draw={this.draw} onClick={this.onClick} /> <graphics-element inline={true} title="Projections in a quadratic Bézier curve" src="./abc.js" data-type="quadratic">
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
<graphics-element inline={true} title="Projections in a cubic Bézier curve" src="./abc.js" data-type="cubic">
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
</div> </div>
Clicking anywhere on the curves shows us three things: So these graphics show us several things:
1. our on-curve point; let's call that <b>B</b>, 1. a point at the tip of the curve construction's "hat": let's call that `A`, as well as
2. a point at the tip of B's "hat", on de Casteljau step up; let's call that <b>A</b>, and 2. our on-curve point give our chosen `t` value: let's call that `B`, and finally,
3. a point that we get by projecting B onto the start--end baseline; let's call that <b>C</b>. 3. a point that we get by projecting A, through B, onto the line between the curve's start and end points: let's call that `C`.
These three values A, B, and C hide an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some *t* value, the ratio distance of C along the baseline is fixed: if some *t* value sets up a C that is 20% away from the start and 80% away from the end, then it doesn't matter where the start, end, or control points are; for that *t* value, C will *always* lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change. The following function stays true: These three values A, B, and C allow us to derive an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some `t` value, the ratio of distances from A to B and B to C is fixed: if some `t` value sets up a C that is 20% away from the start and 80% away from the end, then _it doesn't matter where the start, end, or control points are_; for that `t` value, `C` will *always* lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change.
So, how can we compute `C`? We start with our observation that `C` always lies somewhere between the start and ends points, so logically `C` will have a function that interpolates between those two coordinates:
\[ \[
C = u \cdot P_{start} + (1-u) \cdot P_{end} C = u(t) \cdot P_{start} + (1-u(t)) \cdot P_{end}
\] \]
So that just leaves finding A. If we can figure out what the function `u(t)` looks like, we'll be done. Although we do need to remember that this `u(t)` will have a different for depending on whether we're working with quadratic or cubic curves. [Running through the maths](http://mathoverflow.net/questions/122257/finding-the-formula-for-Bézier-curve-ratios-hull-point-point-baseline) (with thanks to Boris Zbarsky) shows us the following two formulae:
<div class="note">
While that relation is fixed, the function *u(t)* differs depending on whether we're working
with quadratic or cubic curves:
\[ \[
\begin{aligned} u(t)_{quadratic} = \frac{(1-t)^2}{t^2 + (1-t)^2}
& u(t)_{quadratic} &= \frac{(1-t)^2}{t^2 + (1-t)^2} \\
& u(t)_{cubic} &= \frac{(1-t)^3}{t^3 + (1-t)^3}
\end{aligned}
\] \]
So, if we know the start and end coordinates, and we know the *t* value, we know C: And
<div class="figure"> \[
<Graphic inline={true} title="Quadratic value of C for t" draw={this.drawQCT} onMouseMove={this.setCT}/> u(t)_{cubic} = \frac{(1-t)^3}{t^3 + (1-t)^3}
<Graphic inline={true} title="Cubic value of C for t" draw={this.drawCCT} onMouseMove={this.setCT}/> \]
</div>
Mouse-over the graphs to see the expression for C, given the *t* value at the mouse pointer. So, if we know the start and end coordinates, and we know the *t* value, we know C, without having to calculate the `A` or even `B` coordinates. In fact, we can do the same for the ratio function: as another function of `t`, we technically don't need to know what `A` or `B` or `C` are, we can express it was a pure function of `t`, too.
</div> We start by observing that, given `A`, `B`, and `C`, the following always holds:
There's also another important bit of information that is inherent to the ABC values: while the distances between A and B, and B and C, are dynamic (based on where we put B), the *ratio* between the two distances is stable. Given some *t* value, the following always holds:
\[ \[
ratio(t) = \frac{distance(B,C)}{distance(A,B)} = Constant ratio(t) = \frac{distance(B,C)}{distance(A,B)} = Constant
\] \]
This leads to a pretty powerful bit of knowledge: merely by knowing the *t* value of some on curve point, we know where C has to be (as per the above note), and because we know B and C, and thus have the distance between them, we know where A has to be: Working out the maths for this, we see the following two formulae for quadratic and cubic curves:
\[
A = B - \frac{C - B}{ratio(t)} = B + \frac{B - C}{ratio(t)}
\]
And that's it, all values found.
<div class="note">
Much like the *u(t)* function in the above note, the *ratio(t)* function depends on whether we're looking at quadratic or cubic curves. Their form is intrinsically related to the *u(t)* function in that they both come rolling out of the same function evaluation, explained over on [MathOverflow](http://mathoverflow.net/questions/122257/finding-the-formula-for-Bézier-curve-ratios-hull-point-point-baseline) by Boris Zbarsky and myself. The ratio functions are the "s(t)" functions from the answers there, while the "u(t)" functions have the same name both here and on MathOverflow.
\[ \[
ratio(t)_{quadratic} = \left | \frac{t^2 + (1-t)^2 - 1}{t^2 + (1-t)^2} \right | ratio(t)_{quadratic} = \left | \frac{t^2 + (1-t)^2 - 1}{t^2 + (1-t)^2} \right |
\] \]
And
\[ \[
ratio(t)_{cubic} = \left | \frac{t^3 + (1-t)^3 - 1}{t^3 + (1-t)^3} \right | ratio(t)_{cubic} = \left | \frac{t^3 + (1-t)^3 - 1}{t^3 + (1-t)^3} \right |
\] \]
Unfortunately, this trick only works for quadratic and cubic curves. Once we hit higher order curves, things become a lot less predictable; the "fixed point *C*" is no longer fixed, moving around as we move the control points, and projections of *B* onto the line between start and end may actually lie on that line before the start, or after the end, and there are no simple ratios that we can exploit. Which now leaves us with some powerful tools: given thee points (start, end, and "some point on the curve"), as well as a `t` value, we can _contruct_ curves: we can compute `C` using the start and end points, and our `u(t)` function, and once we have `C`, we can use our on-curve point (`B`) and the `ratio(t)` function to find `A`:
</div> \[
A = B - \frac{C - B}{ratio(t)} = B + \frac{B - C}{ratio(t)}
\]
So: if we know B and its corresponding *t* value, then we know all the ABC values, which —together with a start and end coordinate— gives us the necessary information to reconstruct a curve's "de Casteljau skeleton", which means that two points and a value between 0 and 1, we can come up with a curve. And that opens up possibilities: curve manipulation by dragging an on-curve point, as well as curve fitting of "a bunch of coordinates". These are useful things, and we'll look at both in the next sections. So: if we have a curve's start and end point, then for any `t` value, we implicitly know all the ABC values, which gives us the necessary information to reconstruct a curve's "de Casteljau skeleton". Which means that we can now do several things: we can "fit" curves using only three points, which means we can also "mould" curves by moving an on-curve point but leaving its start and end point, and then reconstructing the curve based on where we moved the on-curve point to. These are very useful things, and we'll look at both in the next sections.

View File

@@ -1,173 +0,0 @@
module.exports = {
// ============== first sketch set =====================
/**
* The entry point for the quadratic curve example
*/
setupQuadratic: function(api) {
var curve = api.getDefaultQuadratic();
curve.points[0].y -= 10;
api.setCurve(curve);
},
/**
* The entry point for the cubic curve example
*/
setupCubic: function(api) {
var curve = api.getDefaultCubic();
curve.points[2].y -= 20;
api.setCurve(curve);
api.lut = curve.getLUT(100);
},
/**
* When someone clicks a graphic, find the associated
* on-curve t value and redraw with that new knowledge.
*/
onClick: function(evt, api) {
api.t = api.curve.on({x: evt.offsetX, y: evt.offsetY},7);
if (api.t < 0.05 || api.t > 0.95) api.t = false;
api.redraw();
},
/**
* The master draw function for the "projection" sketches
*/
draw: function(api, curve) {
// draw the basic curve and curve control points
api.reset();
api.drawSkeleton(curve);
api.drawCurve(curve);
api.setColor("black");
if (!api.t) return;
// draw the user-clicked on-curve point
api.drawCircle(api.curve.get(api.t),3);
api.setColor("lightgrey");
var utils = api.utils;
// find the A/B/C values as described in the section text
var hull = api.drawHull(curve, api.t);
var A, B, C;
if(hull.length === 6) {
A = curve.points[1];
B = hull[5];
C = utils.lli4(A, B, curve.points[0], curve.points[2]);
api.setColor("lightgrey");
api.drawLine(curve.points[0], curve.points[2]);
} else if(hull.length === 10) {
A = hull[5];
B = hull[9];
C = utils.lli4(A, B, curve.points[0], curve.points[3]);
api.setColor("lightgrey");
api.drawLine(curve.points[0], curve.points[3]);
}
// show the lines between the A/B/C values
api.setColor("#00FF00");
api.drawLine(A,B);
api.setColor("red");
api.drawLine(B,C);
api.setColor("black");
api.drawCircle(C,3);
// with their associated labels
api.setFill("black");
api.text("A", {x:10 + A.x, y: A.y});
api.text("B (t = " + api.utils.round(api.t,2) + ")", {x:10 + B.x, y: B.y});
api.text("C", {x:10 + C.x, y: C.y});
// and show the distance ratio, which we see does not change irrespective of whether A/B/C change.
var d1 = utils.dist(A, B);
var d2 = utils.dist(B, C);
var ratio = d1/d2;
var h = api.getPanelHeight();
api.text("d1 (A-B): " + utils.round(d1,2) + ", d2 (B-C): "+ utils.round(d2,2) + ", ratio (d1/d2): " + utils.round(ratio,4), {x:10, y:h-7});
},
// ============== second sketch set =====================
/**
* on mouse move, fix the t value for drawing based on the
* cursor position over the sketch. All the way on the left
* is t=0, all the way on the right is t=1, with a linear
* interpolation for anything in between.
*/
setCT: function(evt,api) {
api.t = evt.offsetX / api.getPanelWidth();
},
/**
* Draw the quadratic C(t) values
*/
drawQCT: function(api) {
api.u = api.u || function(t) {
var top = (t-1) * (t-1),
bottom = 2*t*t - 2*t + 1;
return top/bottom;
};
this.drawCTgraph(api);
},
/**
* Draw the cubic C(t) values
*/
drawCCT: function(api) {
api.u = api.u || function(t) {
var top = (1-t) * (1-t) * (1-t),
bottom = t*t*t + top;
return top/bottom;
};
this.drawCTgraph(api);
},
/**
* Draw a C(t) curve
*/
drawCTgraph: function(api) {
api.reset();
var w = api.getPanelWidth();
var pad = 20;
var fwh = w - 2*pad;
// draw some axes
api.setColor("black");
api.drawAxes(pad, "t",0,1, "u",0,1);
// draw the C(t) function using an
// indirection function that takes a
// t value and spits out the C(t) value
// as a point coordinate.
api.setColor("blue");
var uPoint = function(t) {
var value = api.u(t),
res = { x: pad + t*fwh, y: pad + value*fwh };
return res;
};
api.drawFunction(uPoint);
// if the cursor is (or was ever) over this
// graphic, draw the "crosshair" that pinpoints
// where in the function the associated t/C(t)
// coordinate is.
if (api.t) {
var v = api.u(api.t),
v1 = api.utils.round(v,3),
v2 = api.utils.round(1-v,3),
up = uPoint(api.t);
api.drawLine({x:up.x,y:pad}, up);
api.drawLine({x:pad,y:up.y}, up);
api.drawCircle(up,3);
// with some handy text that shows the actual computed values
api.setFill("blue");
api.text(" t = " + api.utils.round(api.t,3), {x:up.x+10, y:up.y-7});
api.text("u(t) = " + api.utils.round(v,3), {x:up.x+10, y:up.y+7});
api.setFill("black");
api.text("C = "+v1+" * start + "+v2+" * end", {x:w/2 - pad, y:pad+fwh});
}
}
};

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 8.5 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 11 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 10 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -1556,49 +1556,49 @@ lli = function(line1, line2):
<p>De Casteljau's algorithm is the pivotal algorithm when it comes to Bézier curves. You can use it not just to split curves, but also to draw them efficiently (especially for high-order Bézier curves), as well as to come up with curves based on three points and a tangent. Particularly this last thing is really useful because it lets us "mould" a curve, by picking it up at some point, and dragging that point around to change the curve's shape.</p> <p>De Casteljau's algorithm is the pivotal algorithm when it comes to Bézier curves. You can use it not just to split curves, but also to draw them efficiently (especially for high-order Bézier curves), as well as to come up with curves based on three points and a tangent. Particularly this last thing is really useful because it lets us "mould" a curve, by picking it up at some point, and dragging that point around to change the curve's shape.</p>
<p>How does that work? Succinctly: we run de Casteljau's algorithm in reverse!</p> <p>How does that work? Succinctly: we run de Casteljau's algorithm in reverse!</p>
<p>In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated <em>t</em> value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.</p> <p>In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated <em>t</em> value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.</p>
<p>So let's use graphics instead of text to see where this "A" is, because text only gets us so far: in the following graphic, click anywhere on the curves to see the identity information that we'll be using to run de Casteljau in reverse (you can manipulate the curve even after picking a point. Note the "ratio" value when you do so: does it change?):</p> <p>So let's use graphics instead of text to see where this "A" is, because text only gets us so far: move the sliders for the following graphics to see what, given specific <code>t</code> value, our <code>A</code> coordinate is. As well as some other coordinates, which taken together let us derive a value that the graphics call "ratio": if you move the curve's points around, A, B, and C will move, what happens to that value?</p>
<div class="figure"> <div class="figure">
<Graphic inline={true} title="Projections in a quadratic Bézier curve" setup={this.setupQuadratic} draw={this.draw} onClick={this.onClick} />
<Graphic inline={true} title="Projections in a cubic Bézier curve" setup={this.setupCubic} draw={this.draw} onClick={this.onClick} /> <graphics-element inline={true} title="Projections in a quadratic Bézier curve" width="275" height="275" src="./chapters/abc/abc.js" data-type="quadratic">
<fallback-image>
<img width="275px" height="275px" src="images\chapters\abc\6e40975c21e70b73954a4dce02b9ba75.png" loading="lazy">
Scripts are disabled. Showing fallback image.
</fallback-image>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
<graphics-element inline={true} title="Projections in a cubic Bézier curve" width="275" height="275" src="./chapters/abc/abc.js" data-type="cubic">
<fallback-image>
<img width="275px" height="275px" src="images\chapters\abc\d744a4955a3ff4e2d85760887ea923d4.png" loading="lazy">
Scripts are disabled. Showing fallback image.
</fallback-image>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
</div> </div>
<p>Clicking anywhere on the curves shows us three things:</p> <p>So these graphics show us several things:</p>
<ol> <ol>
<li>our on-curve point; let's call that <b>B</b>,</li> <li>a point at the tip of the curve construction's "hat": let's call that <code>A</code>, as well as</li>
<li>a point at the tip of B's "hat", on de Casteljau step up; let's call that <b>A</b>, and</li> <li>our on-curve point give our chosen <code>t</code> value: let's call that <code>B</code>, and finally,</li>
<li>a point that we get by projecting B onto the start--end baseline; let's call that <b>C</b>.</li> <li>a point that we get by projecting A, through B, onto the line between the curve's start and end points: let's call that <code>C</code>.</li>
</ol> </ol>
<p>These three values A, B, and C hide an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some <em>t</em> value, the ratio distance of C along the baseline is fixed: if some <em>t</em> value sets up a C that is 20% away from the start and 80% away from the end, then it doesn't matter where the start, end, or control points are; for that <em>t</em> value, C will <em>always</em> lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change. The following function stays true:</p> <p>These three values A, B, and C allow us to derive an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some <code>t</code> value, the ratio of distances from A to B and B to C is fixed: if some <code>t</code> value sets up a C that is 20% away from the start and 80% away from the end, then <em>it doesn't matter where the start, end, or control points are</em>; for that <code>t</code> value, <code>C</code> will <em>always</em> lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change.</p>
<img class="LaTeX SVG" src="./images/chapters/abc/34fe255294faf45ab02128f7997b92ce.svg" width="197px" height="16px" loading="lazy"> <p>So, how can we compute <code>C</code>? We start with our observation that <code>C</code> always lies somewhere between the start and ends points, so logically <code>C</code> will have a function that interpolates between those two coordinates:</p>
<p>So that just leaves finding A.</p> <img class="LaTeX SVG" src="./images/chapters/abc/cd2e47cdc2e23ec86cd1ca1cb4286645.svg" width="241px" height="17px" loading="lazy">
<div class="note"> <p>If we can figure out what the function <code>u(t)</code> looks like, we'll be done. Although we do need to remember that this <code>u(t)</code> will have a different for depending on whether we're working with quadratic or cubic curves. <a href="http://mathoverflow.net/questions/122257/finding-the-formula-for-B%C3%A9zier-curve-ratios-hull-point-point-baseline">Running through the maths</a> (with thanks to Boris Zbarsky) shows us the following two formulae:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/5484dc53e408a4259891a65212ef8636.svg" width="192px" height="40px" loading="lazy">
<p>While that relation is fixed, the function <em>u(t)</em> differs depending on whether we're working <p>And</p>
with quadratic or cubic curves:</p> <img class="LaTeX SVG" src="./images/chapters/abc/63fbe4e666a7dad985ec4110e17c249f.svg" width="167px" height="40px" loading="lazy">
<img class="LaTeX SVG" src="./images/chapters/abc/62f2f984e43a22a6b4bda4d399dedfc6.svg" width="197px" height="87px" loading="lazy"> <p>So, if we know the start and end coordinates, and we know the <em>t</em> value, we know C, without having to calculate the <code>A</code> or even <code>B</code> coordinates. In fact, we can do the same for the ratio function: as another function of <code>t</code>, we technically don't need to know what <code>A</code> or <code>B</code> or <code>C</code> are, we can express it was a pure function of <code>t</code>, too.</p>
<p>So, if we know the start and end coordinates, and we know the <em>t</em> value, we know C:</p> <p>We start by observing that, given <code>A</code>, <code>B</code>, and <code>C</code>, the following always holds:</p>
<div class="figure"> <img class="LaTeX SVG" src="./images/chapters/abc/385d1fd4aecbd2066e6e284a84408be6.svg" width="272px" height="39px" loading="lazy">
<Graphic inline={true} title="Quadratic value of C for t" draw={this.drawQCT} onMouseMove={this.setCT}/> <p>Working out the maths for this, we see the following two formulae for quadratic and cubic curves:</p>
<Graphic inline={true} title="Cubic value of C for t" draw={this.drawCCT} onMouseMove={this.setCT}/> <img class="LaTeX SVG" src="./images/chapters/abc/059000c5c8a37dcc8d7fa04154a05df3.svg" width="255px" height="41px" loading="lazy">
</div> <p>And</p>
<img class="LaTeX SVG" src="./images/chapters/abc/b4987e9b77b0df604238b88596c5f7c3.svg" width="228px" height="41px" loading="lazy">
<p>Mouse-over the graphs to see the expression for C, given the <em>t</em> value at the mouse pointer.</p> <p>Which now leaves us with some powerful tools: given thee points (start, end, and "some point on the curve"), as well as a <code>t</code> value, we can <em>contruct</em> curves: we can compute <code>C</code> using the start and end points, and our <code>u(t)</code> function, and once we have <code>C</code>, we can use our on-curve point (<code>B</code>) and the <code>ratio(t)</code> function to find <code>A</code>:</p>
</div> <img class="LaTeX SVG" src="./images/chapters/abc/12aaf0d7fd20b3c551a0ec76b18bd7d2.svg" width="231px" height="39px" loading="lazy">
<p>So: if we have a curve's start and end point, then for any <code>t</code> value, we implicitly know all the ABC values, which gives us the necessary information to reconstruct a curve's "de Casteljau skeleton". Which means that we can now do several things: we can "fit" curves using only three points, which means we can also "mould" curves by moving an on-curve point but leaving its start and end point, and then reconstructing the curve based on where we moved the on-curve point to. These are very useful things, and we'll look at both in the next sections.</p>
<p>There's also another important bit of information that is inherent to the ABC values: while the distances between A and B, and B and C, are dynamic (based on where we put B), the <em>ratio</em> between the two distances is stable. Given some <em>t</em> value, the following always holds:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/385d1fd4aecbd2066e6e284a84408be6.svg" width="251px" height="39px" loading="lazy">
<p>This leads to a pretty powerful bit of knowledge: merely by knowing the <em>t</em> value of some on curve point, we know where C has to be (as per the above note), and because we know B and C, and thus have the distance between them, we know where A has to be:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/12aaf0d7fd20b3c551a0ec76b18bd7d2.svg" width="217px" height="37px" loading="lazy">
<p>And that's it, all values found.</p>
<div class="note">
<p>Much like the <em>u(t)</em> function in the above note, the <em>ratio(t)</em> function depends on whether we're looking at quadratic or cubic curves. Their form is intrinsically related to the <em>u(t)</em> function in that they both come rolling out of the same function evaluation, explained over on <a href="http://mathoverflow.net/questions/122257/finding-the-formula-for-B%C3%A9zier-curve-ratios-hull-point-point-baseline">MathOverflow</a> by Boris Zbarsky and myself. The ratio functions are the "s(t)" functions from the answers there, while the "u(t)" functions have the same name both here and on MathOverflow.</p>
<img class="LaTeX SVG" src="./images/chapters/abc/059000c5c8a37dcc8d7fa04154a05df3.svg" width="245px" height="41px" loading="lazy">
<img class="LaTeX SVG" src="./images/chapters/abc/b4987e9b77b0df604238b88596c5f7c3.svg" width="223px" height="41px" loading="lazy">
<p>Unfortunately, this trick only works for quadratic and cubic curves. Once we hit higher order curves, things become a lot less predictable; the "fixed point <em>C</em>" is no longer fixed, moving around as we move the control points, and projections of <em>B</em> onto the line between start and end may actually lie on that line before the start, or after the end, and there are no simple ratios that we can exploit.</p>
</div>
<p>So: if we know B and its corresponding <em>t</em> value, then we know all the ABC values, which —together with a start and end coordinate— gives us the necessary information to reconstruct a curve's "de Casteljau skeleton", which means that two points and a value between 0 and 1, we can come up with a curve. And that opens up possibilities: curve manipulation by dragging an on-curve point, as well as curve fitting of "a bunch of coordinates". These are useful things, and we'll look at both in the next sections.</p>
</section> </section>
<section id="moulding"> <section id="moulding">

View File

@@ -1553,49 +1553,49 @@ lli = function(line1, line2):
<p>De Casteljau's algorithm is the pivotal algorithm when it comes to Bézier curves. You can use it not just to split curves, but also to draw them efficiently (especially for high-order Bézier curves), as well as to come up with curves based on three points and a tangent. Particularly this last thing is really useful because it lets us "mould" a curve, by picking it up at some point, and dragging that point around to change the curve's shape.</p> <p>De Casteljau's algorithm is the pivotal algorithm when it comes to Bézier curves. You can use it not just to split curves, but also to draw them efficiently (especially for high-order Bézier curves), as well as to come up with curves based on three points and a tangent. Particularly this last thing is really useful because it lets us "mould" a curve, by picking it up at some point, and dragging that point around to change the curve's shape.</p>
<p>How does that work? Succinctly: we run de Casteljau's algorithm in reverse!</p> <p>How does that work? Succinctly: we run de Casteljau's algorithm in reverse!</p>
<p>In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated <em>t</em> value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.</p> <p>In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated <em>t</em> value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.</p>
<p>So let's use graphics instead of text to see where this "A" is, because text only gets us so far: in the following graphic, click anywhere on the curves to see the identity information that we'll be using to run de Casteljau in reverse (you can manipulate the curve even after picking a point. Note the "ratio" value when you do so: does it change?):</p> <p>So let's use graphics instead of text to see where this "A" is, because text only gets us so far: move the sliders for the following graphics to see what, given specific <code>t</code> value, our <code>A</code> coordinate is. As well as some other coordinates, which taken together let us derive a value that the graphics call "ratio": if you move the curve's points around, A, B, and C will move, what happens to that value?</p>
<div class="figure"> <div class="figure">
<Graphic inline={true} title="Projections in a quadratic Bézier curve" setup={this.setupQuadratic} draw={this.draw} onClick={this.onClick} />
<Graphic inline={true} title="Projections in a cubic Bézier curve" setup={this.setupCubic} draw={this.draw} onClick={this.onClick} /> <graphics-element inline={true} title="Projections in a quadratic Bézier curve" width="275" height="275" src="./chapters/abc/abc.js" data-type="quadratic">
<fallback-image>
<img width="275px" height="275px" src="images\chapters\abc\6e40975c21e70b73954a4dce02b9ba75.png" loading="lazy">
Scripts are disabled. Showing fallback image.
</fallback-image>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
<graphics-element inline={true} title="Projections in a cubic Bézier curve" width="275" height="275" src="./chapters/abc/abc.js" data-type="cubic">
<fallback-image>
<img width="275px" height="275px" src="images\chapters\abc\d744a4955a3ff4e2d85760887ea923d4.png" loading="lazy">
Scripts are disabled. Showing fallback image.
</fallback-image>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
</div> </div>
<p>Clicking anywhere on the curves shows us three things:</p> <p>So these graphics show us several things:</p>
<ol> <ol>
<li>our on-curve point; let's call that <b>B</b>,</li> <li>a point at the tip of the curve construction's "hat": let's call that <code>A</code>, as well as</li>
<li>a point at the tip of B's "hat", on de Casteljau step up; let's call that <b>A</b>, and</li> <li>our on-curve point give our chosen <code>t</code> value: let's call that <code>B</code>, and finally,</li>
<li>a point that we get by projecting B onto the start--end baseline; let's call that <b>C</b>.</li> <li>a point that we get by projecting A, through B, onto the line between the curve's start and end points: let's call that <code>C</code>.</li>
</ol> </ol>
<p>These three values A, B, and C hide an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some <em>t</em> value, the ratio distance of C along the baseline is fixed: if some <em>t</em> value sets up a C that is 20% away from the start and 80% away from the end, then it doesn't matter where the start, end, or control points are; for that <em>t</em> value, C will <em>always</em> lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change. The following function stays true:</p> <p>These three values A, B, and C allow us to derive an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some <code>t</code> value, the ratio of distances from A to B and B to C is fixed: if some <code>t</code> value sets up a C that is 20% away from the start and 80% away from the end, then <em>it doesn't matter where the start, end, or control points are</em>; for that <code>t</code> value, <code>C</code> will <em>always</em> lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change.</p>
<img class="LaTeX SVG" src="./images/chapters/abc/34fe255294faf45ab02128f7997b92ce.svg" width="197px" height="16px" loading="lazy"> <p>So, how can we compute <code>C</code>? We start with our observation that <code>C</code> always lies somewhere between the start and ends points, so logically <code>C</code> will have a function that interpolates between those two coordinates:</p>
<p>So that just leaves finding A.</p> <img class="LaTeX SVG" src="./images/chapters/abc/cd2e47cdc2e23ec86cd1ca1cb4286645.svg" width="241px" height="17px" loading="lazy">
<div class="note"> <p>If we can figure out what the function <code>u(t)</code> looks like, we'll be done. Although we do need to remember that this <code>u(t)</code> will have a different for depending on whether we're working with quadratic or cubic curves. <a href="http://mathoverflow.net/questions/122257/finding-the-formula-for-B%C3%A9zier-curve-ratios-hull-point-point-baseline">Running through the maths</a> (with thanks to Boris Zbarsky) shows us the following two formulae:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/5484dc53e408a4259891a65212ef8636.svg" width="192px" height="40px" loading="lazy">
<p>While that relation is fixed, the function <em>u(t)</em> differs depending on whether we're working <p>And</p>
with quadratic or cubic curves:</p> <img class="LaTeX SVG" src="./images/chapters/abc/63fbe4e666a7dad985ec4110e17c249f.svg" width="167px" height="40px" loading="lazy">
<img class="LaTeX SVG" src="./images/chapters/abc/62f2f984e43a22a6b4bda4d399dedfc6.svg" width="197px" height="87px" loading="lazy"> <p>So, if we know the start and end coordinates, and we know the <em>t</em> value, we know C, without having to calculate the <code>A</code> or even <code>B</code> coordinates. In fact, we can do the same for the ratio function: as another function of <code>t</code>, we technically don't need to know what <code>A</code> or <code>B</code> or <code>C</code> are, we can express it was a pure function of <code>t</code>, too.</p>
<p>So, if we know the start and end coordinates, and we know the <em>t</em> value, we know C:</p> <p>We start by observing that, given <code>A</code>, <code>B</code>, and <code>C</code>, the following always holds:</p>
<div class="figure"> <img class="LaTeX SVG" src="./images/chapters/abc/385d1fd4aecbd2066e6e284a84408be6.svg" width="272px" height="39px" loading="lazy">
<Graphic inline={true} title="Quadratic value of C for t" draw={this.drawQCT} onMouseMove={this.setCT}/> <p>Working out the maths for this, we see the following two formulae for quadratic and cubic curves:</p>
<Graphic inline={true} title="Cubic value of C for t" draw={this.drawCCT} onMouseMove={this.setCT}/> <img class="LaTeX SVG" src="./images/chapters/abc/059000c5c8a37dcc8d7fa04154a05df3.svg" width="255px" height="41px" loading="lazy">
</div> <p>And</p>
<img class="LaTeX SVG" src="./images/chapters/abc/b4987e9b77b0df604238b88596c5f7c3.svg" width="228px" height="41px" loading="lazy">
<p>Mouse-over the graphs to see the expression for C, given the <em>t</em> value at the mouse pointer.</p> <p>Which now leaves us with some powerful tools: given thee points (start, end, and "some point on the curve"), as well as a <code>t</code> value, we can <em>contruct</em> curves: we can compute <code>C</code> using the start and end points, and our <code>u(t)</code> function, and once we have <code>C</code>, we can use our on-curve point (<code>B</code>) and the <code>ratio(t)</code> function to find <code>A</code>:</p>
</div> <img class="LaTeX SVG" src="./images/chapters/abc/12aaf0d7fd20b3c551a0ec76b18bd7d2.svg" width="231px" height="39px" loading="lazy">
<p>So: if we have a curve's start and end point, then for any <code>t</code> value, we implicitly know all the ABC values, which gives us the necessary information to reconstruct a curve's "de Casteljau skeleton". Which means that we can now do several things: we can "fit" curves using only three points, which means we can also "mould" curves by moving an on-curve point but leaving its start and end point, and then reconstructing the curve based on where we moved the on-curve point to. These are very useful things, and we'll look at both in the next sections.</p>
<p>There's also another important bit of information that is inherent to the ABC values: while the distances between A and B, and B and C, are dynamic (based on where we put B), the <em>ratio</em> between the two distances is stable. Given some <em>t</em> value, the following always holds:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/385d1fd4aecbd2066e6e284a84408be6.svg" width="251px" height="39px" loading="lazy">
<p>This leads to a pretty powerful bit of knowledge: merely by knowing the <em>t</em> value of some on curve point, we know where C has to be (as per the above note), and because we know B and C, and thus have the distance between them, we know where A has to be:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/12aaf0d7fd20b3c551a0ec76b18bd7d2.svg" width="217px" height="37px" loading="lazy">
<p>And that's it, all values found.</p>
<div class="note">
<p>Much like the <em>u(t)</em> function in the above note, the <em>ratio(t)</em> function depends on whether we're looking at quadratic or cubic curves. Their form is intrinsically related to the <em>u(t)</em> function in that they both come rolling out of the same function evaluation, explained over on <a href="http://mathoverflow.net/questions/122257/finding-the-formula-for-B%C3%A9zier-curve-ratios-hull-point-point-baseline">MathOverflow</a> by Boris Zbarsky and myself. The ratio functions are the "s(t)" functions from the answers there, while the "u(t)" functions have the same name both here and on MathOverflow.</p>
<img class="LaTeX SVG" src="./images/chapters/abc/059000c5c8a37dcc8d7fa04154a05df3.svg" width="245px" height="41px" loading="lazy">
<img class="LaTeX SVG" src="./images/chapters/abc/b4987e9b77b0df604238b88596c5f7c3.svg" width="223px" height="41px" loading="lazy">
<p>Unfortunately, this trick only works for quadratic and cubic curves. Once we hit higher order curves, things become a lot less predictable; the "fixed point <em>C</em>" is no longer fixed, moving around as we move the control points, and projections of <em>B</em> onto the line between start and end may actually lie on that line before the start, or after the end, and there are no simple ratios that we can exploit.</p>
</div>
<p>So: if we know B and its corresponding <em>t</em> value, then we know all the ABC values, which —together with a start and end coordinate— gives us the necessary information to reconstruct a curve's "de Casteljau skeleton", which means that two points and a value between 0 and 1, we can come up with a curve. And that opens up possibilities: curve manipulation by dragging an on-curve point, as well as curve fitting of "a bunch of coordinates". These are useful things, and we'll look at both in the next sections.</p>
</section> </section>
<section id="moulding"> <section id="moulding">

View File

@@ -155,13 +155,14 @@ class Bezier extends Original {
return p; return p;
} }
drawStruts(t) { drawStruts(t, color = `black`) {
const p = t.forEach ? t : this.getStrutPoints(t); const p = t.forEach ? t : this.getStrutPoints(t);
const api = this.api; const api = this.api;
const ctx = api.ctx; const ctx = api.ctx;
ctx.cacheStyle(); ctx.cacheStyle();
api.noFill(); api.noFill();
api.setStroke(color);
let s = this.points.length; let s = this.points.length;
let n = this.points.length; let n = this.points.length;
@@ -176,6 +177,8 @@ class Bezier extends Original {
s += n; s += n;
} }
ctx.restoreStyle(); ctx.restoreStyle();
return p;
} }
drawBoundingBox(color) { drawBoundingBox(color) {

View File

@@ -1547,49 +1547,49 @@ lli = function(line1, line2):
<p>De Casteljau's algorithm is the pivotal algorithm when it comes to Bézier curves. You can use it not just to split curves, but also to draw them efficiently (especially for high-order Bézier curves), as well as to come up with curves based on three points and a tangent. Particularly this last thing is really useful because it lets us "mould" a curve, by picking it up at some point, and dragging that point around to change the curve's shape.</p> <p>De Casteljau's algorithm is the pivotal algorithm when it comes to Bézier curves. You can use it not just to split curves, but also to draw them efficiently (especially for high-order Bézier curves), as well as to come up with curves based on three points and a tangent. Particularly this last thing is really useful because it lets us "mould" a curve, by picking it up at some point, and dragging that point around to change the curve's shape.</p>
<p>How does that work? Succinctly: we run de Casteljau's algorithm in reverse!</p> <p>How does that work? Succinctly: we run de Casteljau's algorithm in reverse!</p>
<p>In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated <em>t</em> value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.</p> <p>In order to run de Casteljau's algorithm in reverse, we need a few basic things: a start and end point, a point on the curve that want to be moving around, which has an associated <em>t</em> value, and a point we've not explicitly talked about before, and as far as I know has no explicit name, but lives one iteration higher in the de Casteljau process then our on-curve point does. I like to call it "A" for reasons that will become obvious.</p>
<p>So let's use graphics instead of text to see where this "A" is, because text only gets us so far: in the following graphic, click anywhere on the curves to see the identity information that we'll be using to run de Casteljau in reverse (you can manipulate the curve even after picking a point. Note the "ratio" value when you do so: does it change?):</p> <p>So let's use graphics instead of text to see where this "A" is, because text only gets us so far: move the sliders for the following graphics to see what, given specific <code>t</code> value, our <code>A</code> coordinate is. As well as some other coordinates, which taken together let us derive a value that the graphics call "ratio": if you move the curve's points around, A, B, and C will move, what happens to that value?</p>
<div class="figure"> <div class="figure">
<Graphic inline={true} title="Projections in a quadratic Bézier curve" setup={this.setupQuadratic} draw={this.draw} onClick={this.onClick} />
<Graphic inline={true} title="Projections in a cubic Bézier curve" setup={this.setupCubic} draw={this.draw} onClick={this.onClick} /> <graphics-element inline={true} title="Projections in a quadratic Bézier curve" width="275" height="275" src="./chapters/abc/abc.js" data-type="quadratic">
<fallback-image>
<img width="275px" height="275px" src="images\chapters\abc\6e40975c21e70b73954a4dce02b9ba75.png" loading="lazy">
Scripts are disabled. Showing fallback image.
</fallback-image>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
<graphics-element inline={true} title="Projections in a cubic Bézier curve" width="275" height="275" src="./chapters/abc/abc.js" data-type="cubic">
<fallback-image>
<img width="275px" height="275px" src="images\chapters\abc\d744a4955a3ff4e2d85760887ea923d4.png" loading="lazy">
Scripts are disabled. Showing fallback image.
</fallback-image>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slide-control">
</graphics-element>
</div> </div>
<p>Clicking anywhere on the curves shows us three things:</p> <p>So these graphics show us several things:</p>
<ol> <ol>
<li>our on-curve point; let's call that <b>B</b>,</li> <li>a point at the tip of the curve construction's "hat": let's call that <code>A</code>, as well as</li>
<li>a point at the tip of B's "hat", on de Casteljau step up; let's call that <b>A</b>, and</li> <li>our on-curve point give our chosen <code>t</code> value: let's call that <code>B</code>, and finally,</li>
<li>a point that we get by projecting B onto the start--end baseline; let's call that <b>C</b>.</li> <li>a point that we get by projecting A, through B, onto the line between the curve's start and end points: let's call that <code>C</code>.</li>
</ol> </ol>
<p>These three values A, B, and C hide an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some <em>t</em> value, the ratio distance of C along the baseline is fixed: if some <em>t</em> value sets up a C that is 20% away from the start and 80% away from the end, then it doesn't matter where the start, end, or control points are; for that <em>t</em> value, C will <em>always</em> lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change. The following function stays true:</p> <p>These three values A, B, and C allow us to derive an important identity formula for quadratic and cubic Bézier curves: for any point on the curve with some <code>t</code> value, the ratio of distances from A to B and B to C is fixed: if some <code>t</code> value sets up a C that is 20% away from the start and 80% away from the end, then <em>it doesn't matter where the start, end, or control points are</em>; for that <code>t</code> value, <code>C</code> will <em>always</em> lie at 20% from the start and 80% from the end point. Go ahead, pick an on-curve point in either graphic and then move all the other points around: if you only move the control points, start and end won't move, and so neither will C, and if you move either start or end point, C will move but its relative position will not change.</p>
<img class="LaTeX SVG" src="./images/chapters/abc/34fe255294faf45ab02128f7997b92ce.svg" width="197px" height="16px" loading="lazy"> <p>So, how can we compute <code>C</code>? We start with our observation that <code>C</code> always lies somewhere between the start and ends points, so logically <code>C</code> will have a function that interpolates between those two coordinates:</p>
<p>So that just leaves finding A.</p> <img class="LaTeX SVG" src="./images/chapters/abc/cd2e47cdc2e23ec86cd1ca1cb4286645.svg" width="241px" height="17px" loading="lazy">
<div class="note"> <p>If we can figure out what the function <code>u(t)</code> looks like, we'll be done. Although we do need to remember that this <code>u(t)</code> will have a different for depending on whether we're working with quadratic or cubic curves. <a href="http://mathoverflow.net/questions/122257/finding-the-formula-for-B%C3%A9zier-curve-ratios-hull-point-point-baseline">Running through the maths</a> (with thanks to Boris Zbarsky) shows us the following two formulae:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/5484dc53e408a4259891a65212ef8636.svg" width="192px" height="40px" loading="lazy">
<p>While that relation is fixed, the function <em>u(t)</em> differs depending on whether we're working <p>And</p>
with quadratic or cubic curves:</p> <img class="LaTeX SVG" src="./images/chapters/abc/63fbe4e666a7dad985ec4110e17c249f.svg" width="167px" height="40px" loading="lazy">
<img class="LaTeX SVG" src="./images/chapters/abc/62f2f984e43a22a6b4bda4d399dedfc6.svg" width="197px" height="87px" loading="lazy"> <p>So, if we know the start and end coordinates, and we know the <em>t</em> value, we know C, without having to calculate the <code>A</code> or even <code>B</code> coordinates. In fact, we can do the same for the ratio function: as another function of <code>t</code>, we technically don't need to know what <code>A</code> or <code>B</code> or <code>C</code> are, we can express it was a pure function of <code>t</code>, too.</p>
<p>So, if we know the start and end coordinates, and we know the <em>t</em> value, we know C:</p> <p>We start by observing that, given <code>A</code>, <code>B</code>, and <code>C</code>, the following always holds:</p>
<div class="figure"> <img class="LaTeX SVG" src="./images/chapters/abc/385d1fd4aecbd2066e6e284a84408be6.svg" width="272px" height="39px" loading="lazy">
<Graphic inline={true} title="Quadratic value of C for t" draw={this.drawQCT} onMouseMove={this.setCT}/> <p>Working out the maths for this, we see the following two formulae for quadratic and cubic curves:</p>
<Graphic inline={true} title="Cubic value of C for t" draw={this.drawCCT} onMouseMove={this.setCT}/> <img class="LaTeX SVG" src="./images/chapters/abc/059000c5c8a37dcc8d7fa04154a05df3.svg" width="255px" height="41px" loading="lazy">
</div> <p>And</p>
<img class="LaTeX SVG" src="./images/chapters/abc/b4987e9b77b0df604238b88596c5f7c3.svg" width="228px" height="41px" loading="lazy">
<p>Mouse-over the graphs to see the expression for C, given the <em>t</em> value at the mouse pointer.</p> <p>Which now leaves us with some powerful tools: given thee points (start, end, and "some point on the curve"), as well as a <code>t</code> value, we can <em>contruct</em> curves: we can compute <code>C</code> using the start and end points, and our <code>u(t)</code> function, and once we have <code>C</code>, we can use our on-curve point (<code>B</code>) and the <code>ratio(t)</code> function to find <code>A</code>:</p>
</div> <img class="LaTeX SVG" src="./images/chapters/abc/12aaf0d7fd20b3c551a0ec76b18bd7d2.svg" width="231px" height="39px" loading="lazy">
<p>So: if we have a curve's start and end point, then for any <code>t</code> value, we implicitly know all the ABC values, which gives us the necessary information to reconstruct a curve's "de Casteljau skeleton". Which means that we can now do several things: we can "fit" curves using only three points, which means we can also "mould" curves by moving an on-curve point but leaving its start and end point, and then reconstructing the curve based on where we moved the on-curve point to. These are very useful things, and we'll look at both in the next sections.</p>
<p>There's also another important bit of information that is inherent to the ABC values: while the distances between A and B, and B and C, are dynamic (based on where we put B), the <em>ratio</em> between the two distances is stable. Given some <em>t</em> value, the following always holds:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/385d1fd4aecbd2066e6e284a84408be6.svg" width="251px" height="39px" loading="lazy">
<p>This leads to a pretty powerful bit of knowledge: merely by knowing the <em>t</em> value of some on curve point, we know where C has to be (as per the above note), and because we know B and C, and thus have the distance between them, we know where A has to be:</p>
<img class="LaTeX SVG" src="./images/chapters/abc/12aaf0d7fd20b3c551a0ec76b18bd7d2.svg" width="217px" height="37px" loading="lazy">
<p>And that's it, all values found.</p>
<div class="note">
<p>Much like the <em>u(t)</em> function in the above note, the <em>ratio(t)</em> function depends on whether we're looking at quadratic or cubic curves. Their form is intrinsically related to the <em>u(t)</em> function in that they both come rolling out of the same function evaluation, explained over on <a href="http://mathoverflow.net/questions/122257/finding-the-formula-for-B%C3%A9zier-curve-ratios-hull-point-point-baseline">MathOverflow</a> by Boris Zbarsky and myself. The ratio functions are the "s(t)" functions from the answers there, while the "u(t)" functions have the same name both here and on MathOverflow.</p>
<img class="LaTeX SVG" src="./images/chapters/abc/059000c5c8a37dcc8d7fa04154a05df3.svg" width="245px" height="41px" loading="lazy">
<img class="LaTeX SVG" src="./images/chapters/abc/b4987e9b77b0df604238b88596c5f7c3.svg" width="223px" height="41px" loading="lazy">
<p>Unfortunately, this trick only works for quadratic and cubic curves. Once we hit higher order curves, things become a lot less predictable; the "fixed point <em>C</em>" is no longer fixed, moving around as we move the control points, and projections of <em>B</em> onto the line between start and end may actually lie on that line before the start, or after the end, and there are no simple ratios that we can exploit.</p>
</div>
<p>So: if we know B and its corresponding <em>t</em> value, then we know all the ABC values, which —together with a start and end coordinate— gives us the necessary information to reconstruct a curve's "de Casteljau skeleton", which means that two points and a value between 0 and 1, we can come up with a curve. And that opens up possibilities: curve manipulation by dragging an on-curve point, as well as curve fitting of "a bunch of coordinates". These are useful things, and we'll look at both in the next sections.</p>
</section> </section>
<section id="moulding"> <section id="moulding">