Skip to content

Commit e40b8e7

Browse files
committed
bezier
1 parent c4870fd commit e40b8e7

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

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

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,49 +47,12 @@ Here are some examples:
4747

4848
![](bezier-car.png) ![](bezier-letter.png) ![](bezier-vase.png)
4949

50-
## Maths
51-
52-
A Bezier curve can be described using a mathematical formula.
53-
54-
As we'll see soon -- there's no need to know it. But for completeness -- here it is.
55-
56-
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]`.
57-
58-
- The formula for a 2-points curve:
59-
60-
<code>P = (1-t)P<sub>1</sub> + tP<sub>2</sub></code>
61-
- For three points:
62-
63-
<code>P = (1−t)<sup>2</sup>P<sub>1</sub> + 2(1−t)tP<sub>2</sub> + t<sup>2</sup>P<sub>3</sub></code>
64-
- For four points:
65-
66-
<code>P = (1−t)<sup>3</sup>P<sub>1</sub> + 3(1−t)<sup>2</sup>tP<sub>2</sub> +3(1−t)t<sup>2</sup>P<sub>3</sub> + t<sup>3</sup>P<sub>4</sub></code>
67-
68-
These are vector equations.
69-
70-
We can rewrite them coordinate-by-coordinate, for instance the 3-point curve:
71-
72-
- <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>
73-
- <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>
74-
75-
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.
76-
77-
For instance, if control points are `(0,0)`, `(0.5, 1)` and `(1, 0)`, the equations are:
78-
79-
- <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> = t</code>
80-
- <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>
81-
82-
Now as `t` runs from `0` to `1`, the set of values `(x,y)` for each `t` forms the curve.
83-
84-
That's probably too scientific, not very obvious why curves look like that, and how they depend on control points.
85-
86-
So here's the drawing algorithm that may be easier to understand.
87-
8850
## De Casteljau's algorithm
8951

90-
[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.
52+
There's a mathematical formula for Bezier curves, but let's cover it a bit later, because
53+
[De Casteljau's algorithm](https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm) it is identical to the mathematical definition and visually shows how it is constructed.
9154

92-
Let's see it on the 3-points example.
55+
First let's see the 3-points example.
9356

9457
Here's the demo, and the explanation follow.
9558

@@ -116,19 +79,19 @@ Points can be moved by the mouse. Press the "play" button to run it.
11679
| ------------------------ | ---------------------- |
11780
| ![](bezier3-draw1.png) | ![](bezier3-draw2.png) |
11881

119-
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>.
82+
4. Now in 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>.
12083

12184
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.
12285

12386
That was a process for 3 points. But the same is for 4 points.
12487

125-
The demo for 4 points (points can be moved by mouse):
88+
The demo for 4 points (points can be moved by a mouse):
12689

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

129-
The algorithm:
92+
The algorithm for 4 points:
13093

131-
- Control points are connected by segments: 1 -> 2, 2 -> 3, 3 -> 4. We have 3 <span style="color:#825E28">brown</span> segments.
94+
- Connect control points by segments: 1 -> 2, 2 -> 3, 3 -> 4. There will be 3 <span style="color:#825E28">brown</span> segments.
13295
- For each `t` in the interval from `0` to `1`:
13396
- We take points on these segments on the distance proportional to `t` from the beginning. These points are connected, so that we have two <span style="color:#0A0">green segments</span>.
13497
- On these segments we take points proportional to `t`. We get one <span style="color:#167490">blue segment</span>.
@@ -137,30 +100,37 @@ The algorithm:
137100

138101
The algorithm is recursive and can be generalized for any number of control points.
139102

140-
Given N of control points, we connect them to get initially N-1 segments.
103+
Given N of control points:
104+
105+
1. We connect them to get initially N-1 segments.
106+
2. Then for each `t` from `0` to `1`, we take a point on each segment on the distance proportional to `t` and connect them. There will be N-2 segments.
107+
3. Repeat step 2 until there is only one point.
141108

142-
Then for each `t` from `0` to `1`:
143-
- Take a point on each of segment on the distance proportional to `t` and connect them -- there will be N-2 segments.
144-
- 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...
145-
- Till we have one point. These points make the curve.
109+
These points make the curve.
110+
111+
```online
112+
**Run and pause examples to clearly see the segments and how the curve is built.**
113+
```
146114

147-
Move examples of curves:
115+
116+
A curve that looks like `y=1/t`:
148117

149118
[iframe src="demo.svg?p=0,0,0,0.75,0.25,1,1,1&animate=1" height=370]
150119

151-
With other points:
120+
121+
With zig-zag control points:
152122

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

155125
Loop form:
156126

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

159-
Not smooth Bezier curve:
129+
A non-smooth Bezier curve (yeah, that's possible too):
160130

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

163-
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.
133+
As the algorithm is recursive, we can build Bezier curves of any order: using 5, 6 or more control points. But in practice many points are less useful. Usually we take 2-3 points, and for complex lines glue several curves together. That's simpler to develop and calculate.
164134

165135
```smart header="How to draw a curve *through* given points?"
166136
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.
@@ -172,6 +142,42 @@ There are mathematical formulas for such curves, for instance [Lagrange polynomi
172142
In computer graphics [spline interpolation](https://en.wikipedia.org/wiki/Spline_interpolation) is often used to build smooth curves that connect many points.
173143
```
174144

145+
146+
## Maths
147+
148+
A Bezier curve can be described using a mathematical formula.
149+
150+
As we saw -- there's actually no need to know it. But for completeness -- here it is.
151+
152+
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]`.
153+
154+
- The formula for a 2-points curve:
155+
156+
<code>P = (1-t)P<sub>1</sub> + tP<sub>2</sub></code>
157+
- For three points:
158+
159+
<code>P = (1−t)<sup>2</sup>P<sub>1</sub> + 2(1−t)tP<sub>2</sub> + t<sup>2</sup>P<sub>3</sub></code>
160+
- For four points:
161+
162+
<code>P = (1−t)<sup>3</sup>P<sub>1</sub> + 3(1−t)<sup>2</sup>tP<sub>2</sub> +3(1−t)t<sup>2</sup>P<sub>3</sub> + t<sup>3</sup>P<sub>4</sub></code>
163+
164+
165+
These are vector equations. In other words, we can put `x` and `y` instead of `P` to get corresponding coordinates.
166+
167+
For instance, the 3-point curve is formed by points `(x,y)` calculated as:
168+
169+
- <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>
170+
- <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>
171+
172+
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 then as `t` moves from `0` to `1`, for each value of `t` we'll have `(x,y)` of the curve.
173+
174+
For instance, if control points are `(0,0)`, `(0.5, 1)` and `(1, 0)`, the equations become:
175+
176+
- <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> = t</code>
177+
- <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>
178+
179+
Now as `t` runs from `0` to `1`, the set of values `(x,y)` for each `t` forms the curve for such control points.
180+
175181
## Summary
176182

177183
Bezier curves are defined by their control points.

0 commit comments

Comments
 (0)