Skip to content

Commit 2f4242c

Browse files
committed
bezier
1 parent 5a1c12d commit 2f4242c

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

5-animation/1-bezier-curve/article.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ Four points curve:
2727
If you look closely at these curves, you can immediately notice:
2828

2929
1. **Points are not always on curve.** That's perfectly normal, later we'll see how the curve is built.
30-
2. **Curve order equals the number of points minus one**.
30+
2. **The curve order equals the number of points minus one**.
3131
For two points we have a linear curve (that's a straight line), for three points -- quadratic curve (parabolic), for three points -- cubic curve.
3232
3. **A curve is always inside the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) of control points:**
3333

3434
![](bezier4-e.png) ![](bezier3-e.png)
3535

36-
Because of that last property, in computer graphics it's possible to optimize intersection tests. If convex hulls do not intersect, then curves do not either.
36+
Because of that last property, in computer graphics it's possible to optimize intersection tests. If convex hulls do not intersect, then curves do not either. So checking for the convex hulls intersection first can give a very fast "no intersection" result. Checking the intersection or convex hulls is much easier, because they are rectangles, triangles and so on (see the picture above), much simpler figures than the curve.
3737

3838
The main value of Bezier curves for drawing -- by moving the points the curve is changing *in intuitively obvious way*.
3939

40-
Try to move points using a mouse in the example below:
40+
Try to move control points using a mouse in the example below:
4141

4242
[iframe src="demo.svg?nocpath=1&p=0,0,0.5,0,0.5,1,1,1" height=370]
4343

@@ -53,13 +53,11 @@ Here are some examples:
5353

5454
A Bezier curve can be described using a mathematical formula.
5555

56-
As we'll see soon -- there's no need to know it. But for completeness -- here you go.
56+
As we'll see soon -- there's no need to know it. But for completeness -- here it is.
5757

58-
We have coordinates of control points <code>P<sub>i</sub></code>: the first control point has coordinates <code>P<sub>1</sub> = (x<sub>1</sub>, y<sub>1</sub>)</code>, the second: <code>P<sub>2</sub> = (x<sub>2</sub>, y<sub>2</sub>)</code>, and so on.
58+
Given the coordinates of control points <code>P<sub>i</sub></code>: the first control point has coordinates <code>P<sub>1</sub> = (x<sub>1</sub>, y<sub>1</sub>)</code>, the second: <code>P<sub>2</sub> = (x<sub>2</sub>, y<sub>2</sub>)</code>, and so on, the curve coordinates are described by the equation that depends on the parameter `t` from the segment `[0,1]`.
5959

60-
Then the curve coordinates are described by the equation that depends on the parameter `t` from the segment `[0,1]`.
61-
62-
- For two points:
60+
- The formula for a 2-points curve:
6361

6462
<code>P = (1-t)P<sub>1</sub> + tP<sub>2</sub></code>
6563
- For three points:
@@ -71,24 +69,33 @@ Then the curve coordinates are described by the equation that depends on the par
7169

7270
These are vector equations.
7371

74-
We can rewrite them coordinate-by-coordinate, for instance the 3-point variant:
72+
We can rewrite them coordinate-by-coordinate, for instance the 3-point curve:
7573

7674
- <code>x = (1−t)<sup>2</sup>x<sub>1</sub> + 2(1−t)tx<sub>2</sub> + t<sup>2</sup>x<sub>3</sub></code>
7775
- <code>y = (1−t)<sup>2</sup>y<sub>1</sub> + 2(1−t)ty<sub>2</sub> + t<sup>2</sup>y<sub>3</sub></code>
7876

79-
Instead of <code>x<sub>1</sub>, y<sub>1</sub>, x<sub>2</sub>, y<sub>2</sub>, x<sub>3</sub>, y<sub>3</sub></code> we should put coordinates of 3 control points, and `t` runs from `0` to `1`. The set of values `(x,y)` for each `t` forms the curve.
77+
Instead of <code>x<sub>1</sub>, y<sub>1</sub>, x<sub>2</sub>, y<sub>2</sub>, x<sub>3</sub>, y<sub>3</sub></code> we should put coordinates of 3 control points.
78+
79+
For instance, if control points are `(0,0)`, `(0.5, 1)` and `(1, 0)`, the equations are:
80+
81+
- <code>x = (1−t)<sup>2</sup> * 0 + 2(1−t)t * 0.5 + t<sup>2</sup> * 1 = (1-t)t + t<sup>2</sup> = 1</code>
82+
- <code>y = (1−t)<sup>2</sup> * 0 + 2(1−t)t * 1 + t<sup>2</sup> * 0 = 2(1-t)t = –t<sup>2</sup> + 2t</code>
83+
84+
Now as `t` runs from `0` to `1`, the set of values `(x,y)` for each `t` forms the curve.
8085

8186
That's probably too scientific, not very obvious why curves look like that, and how they depend on control points.
8287

83-
Another drawing algorithm may be easier to understand.
88+
So here's the drawing algorithm that may be easier to understand.
8489

8590
## De Casteljau's algorithm
8691

8792
[De Casteljau's algorithm](https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm) is identical to the mathematical definition of the curve, but visually shows how it is built.
8893

89-
Let's explain it on the 3-points example.
94+
Let's see it on the 3-points example.
95+
96+
Here's the demo, and the explanation follow.
9097

91-
Here's the demo. Points can be moved by the mouse. Press the "play" button to run it.
98+
Points can be moved by the mouse. Press the "play" button to run it.
9299

93100
[iframe src="demo.svg?p=0,0,0.5,1,1,0&animate=1" height=370]
94101

@@ -111,7 +118,7 @@ Here's the demo. Points can be moved by the mouse. Press the "play" button to ru
111118
| ------------------------ | ---------------------- |
112119
| ![](bezier3-draw1.png) | ![](bezier3-draw2.png) |
113120

114-
4. On the <span style="color:#167490">new</span> segment we take a point on the distance proportional to `t`. That is, for `t=0.25` (the left picture) we have a point at the end of the left quarter of the segment, and for `t=0.5` (the right picture) -- in the middle of the segment. On pictures above that point is <span style="color:red">red</span>.
121+
4. Now the <span style="color:#167490">blue</span> segment take a point on the distance proportional to the same value of `t`. That is, for `t=0.25` (the left picture) we have a point at the end of the left quarter of the segment, and for `t=0.5` (the right picture) -- in the middle of the segment. On pictures above that point is <span style="color:red">red</span>.
115122

116123
5. As `t` runs from `0` to `1`, every value of `t` adds a point to the curve. The set of such points forms the Bezier curve. It's red and parabolic on the pictures above.
117124

@@ -130,9 +137,14 @@ The algorithm:
130137
- On the blue segment we take a point proportional to `t`. On the example above it's <span style="color:red">red</span>.
131138
- These points together form the curve.
132139

133-
The algorithm is recursive. For each `t` from `0` to `1` we have first N segments, then take points of them and connect -- N-1 segments and so on till we have one point. These make the curve.
140+
The algorithm is recursive and can be generalized for any number of control points.
141+
142+
Given N of control points, we connect them to get initially N-1 segments.
134143

135-
Press the "play" button in the example above to see it in action.
144+
Then for each `t` from `0` to `1`:
145+
- Take a point on each of segment on the distance proportional to `t` and connect them -- there will be N-2 segments.
146+
- Take a point on each of these segments on the distance proportional to `t` and connect -- there will be N-3 segments, and so on...
147+
- Till we have one point. These points make the curve.
136148

137149
Move examples of curves:
138150

@@ -150,10 +162,10 @@ Not smooth Bezier curve:
150162

151163
[iframe src="demo.svg?p=0,0,1,1,0,1,1,0&animate=1" height=370]
152164

153-
As the algorithm is recursive, we can build Bezier curves of any order: using 5, 6 or more control points. But on practice they are less useful. Usually we take 2-3 points, and for complex lines glue several curves together. That's simpler for support and in calculations.
165+
As the algorithm is recursive, we can build Bezier curves of any order: using 5, 6 or more control points. But in practice they are less useful. Usually we take 2-3 points, and for complex lines glue several curves together. That's simpler to develop and calculate.
154166

155167
```smart header="How to draw a curve *through* given points?"
156-
We use control points for a Bezier curve. As we can see, they are not on the curve. Or, to be precise, the first and the last one do belong to curve, but others don't.
168+
We use control points for a Bezier curve. As we can see, they are not on the curve. Or, to be precise, the first and the last ones do belong to curve, but others don't.
157169
158170
Sometimes we have another task: to draw a curve *through several points*, so that all of them are on a single smooth curve. That task is called [interpolation](https://en.wikipedia.org/wiki/Interpolation), and here we don't cover it.
159171

0 commit comments

Comments
 (0)