diff --git a/docs/chapters/abc/abc.js b/docs/chapters/abc/abc.js new file mode 100644 index 00000000..c811c8f8 --- /dev/null +++ b/docs/chapters/abc/abc.js @@ -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); +} diff --git a/docs/chapters/abc/content.en-GB.md b/docs/chapters/abc/content.en-GB.md index 04bb7667..73170fea 100644 --- a/docs/chapters/abc/content.en-GB.md +++ b/docs/chapters/abc/content.en-GB.md @@ -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. -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?
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.
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.
-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?
Clicking anywhere on the curves shows us three things:
+So these graphics show us several things:
A
, as well ast
value: let's call that B
, and finally,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:
-So that just leaves finding A.
-While that relation is fixed, the function u(t) differs depending on whether we're working -with quadratic or cubic curves:
-So, if we know the start and end coordinates, and we know the t value, we know C:
-Mouse-over the graphs to see the expression for C, given the t value at the mouse pointer.
-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:
-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:
-And that's it, all values found.
-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 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.
-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.
-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.
+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:
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 (with thanks to Boris Zbarsky) shows us the following two formulae:
And
+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.
We start by observing that, given A
, B
, and C
, the following always holds:
Working out the maths for this, we see the following two formulae for quadratic and cubic curves:
+And
+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
:
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.
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.
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.
-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?
Clicking anywhere on the curves shows us three things:
+So these graphics show us several things:
A
, as well ast
value: let's call that B
, and finally,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:
-So that just leaves finding A.
-While that relation is fixed, the function u(t) differs depending on whether we're working -with quadratic or cubic curves:
-So, if we know the start and end coordinates, and we know the t value, we know C:
-Mouse-over the graphs to see the expression for C, given the t value at the mouse pointer.
-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:
-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:
-And that's it, all values found.
-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 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.
-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.
-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.
+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:
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 (with thanks to Boris Zbarsky) shows us the following two formulae:
And
+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.
We start by observing that, given A
, B
, and C
, the following always holds:
Working out the maths for this, we see the following two formulae for quadratic and cubic curves:
+And
+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
:
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.
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.
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.
-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?
Clicking anywhere on the curves shows us three things:
+So these graphics show us several things:
A
, as well ast
value: let's call that B
, and finally,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:
-So that just leaves finding A.
-While that relation is fixed, the function u(t) differs depending on whether we're working -with quadratic or cubic curves:
-So, if we know the start and end coordinates, and we know the t value, we know C:
-Mouse-over the graphs to see the expression for C, given the t value at the mouse pointer.
-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:
-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:
-And that's it, all values found.
-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 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.
-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.
-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.
+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:
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 (with thanks to Boris Zbarsky) shows us the following two formulae:
And
+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.
We start by observing that, given A
, B
, and C
, the following always holds:
Working out the maths for this, we see the following two formulae for quadratic and cubic curves:
+And
+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
:
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.