Skip to content

Commit 8b2897d

Browse files
CoolRiceleviding
authored andcommitted
3-animation/1-bezier-curve and 3-animation/index.md (javascript-tutorial#211)
* 3-animation/1-bezier-curve and 3-animation/index.md 3-animation/1-bezier-curve and 3-animation/index.md * update according to correcter * Update article.md
1 parent 4717d88 commit 8b2897d

File tree

2 files changed

+89
-89
lines changed

2 files changed

+89
-89
lines changed

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

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,193 @@
1-
# Bezier curve
1+
# 贝塞尔曲线
22

3-
Bezier curves are used in computer graphics to draw shapes, for CSS animation and in many other places.
3+
贝塞尔曲线用于计算机图形绘制形状,CSS 动画和许多其他地方。
44

5-
They are actually a very simple thing, worth to study once and then feel comfortable in the world of vector graphics and advanced animations.
5+
他们其实非常简单,值得学习一次并且在矢量图形和高级动画的世界里非常受用。
66

7-
## Control points
7+
## 控制点
88

9-
A [bezier curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve) is defined by control points.
9+
[贝塞尔曲线](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)由控制点定义。
1010

11-
There may be 2, 3, 4 or more.
11+
这些点可能有 2、3、4 或更多。
1212

13-
For instance, two points curve:
13+
例如,两点曲线:
1414

1515
![](bezier2.png)
1616

17-
Three points curve:
17+
三点曲线:
1818

1919
![](bezier3.png)
2020

21-
Four points curve:
21+
四点曲线:
2222

2323
![](bezier4.png)
2424

25-
If you look closely at these curves, you can immediately notice:
25+
如果仔细观察这些曲线,你会立即注意到:
2626

27-
1. **Points are not always on curve.** That's perfectly normal, later we'll see how the curve is built.
28-
2. **The curve order equals the number of points minus one**.
29-
For two points we have a linear curve (that's a straight line), for three points -- quadratic curve (parabolic), for four points -- cubic curve.
30-
3. **A curve is always inside the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) of control points:**
27+
1. **控制点不总是在曲线上**这是非常正常的,稍后我们将看到曲线是如何构建的。
28+
2. **曲线的阶次等于控制点的数量减一**
29+
对于两个点我们能得到一条线性曲线(直线),三个点 — 一条二阶曲线,四个点 — 一条三阶曲线。
30+
3. **曲线总是在控制点的[凸包](https://en.wikipedia.org/wiki/Convex_hull)内部:**
3131

3232
![](bezier4-e.png) ![](bezier3-e.png)
3333

34-
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.
34+
由于最后一个属性,在计算机图形学中,可以优化相交测试。如果凸包不相交,则曲线也不相交。因此,首先检查凸包的交叉点可以非常快地给出“无交叉”结果。检查交叉区域或凸包更容易,因为它们是矩形,三角形等(见上图),比曲线简单的多。
3535

36-
The main value of Bezier curves for drawing -- by moving the points the curve is changing *in intuitively obvious way*.
36+
贝塞尔曲线绘制的主要重点 —— 通过移动曲线,曲线**以直观明显的**方式变化。
3737

38-
Try to move control points using a mouse in the example below:
38+
尝试在下面的示例中使用鼠标移动控制点:
3939

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

42-
**As you can notice, the curve stretches along the tangential lines 1 -> 2 and 3 -> 4.**
42+
**可以注意到,曲线沿切线 1 -> 2 3 -> 4 延伸。**
4343

44-
After some practice it becomes obvious how to place points to get the needed curve. And by connecting several curves we can get practically anything.
44+
经过一些练习后,很明显我们知道怎样通过放置控制点来获得所需要的曲线。通过连接几条曲线,我们几乎可以得到任何东西。
4545

46-
Here are some examples:
46+
这里有一些例子:
4747

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

50-
## Maths
50+
## 数学
5151

52-
A Bezier curve can be described using a mathematical formula.
52+
贝塞尔曲线可以使用数学方程式来描述。
5353

54-
As we'll see soon -- there's no need to know it. But for completeness -- here it is.
54+
很快我们就能看到 —— 没必要知道它。但是为了完整性 —— 请看这里。
5555

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]`.
56+
给定控制点 <code>P<sub>i</sub></code> 的坐标:第一个控制点的坐标为 <code>P<sub>1</sub> = (x<sub>1</sub>, y<sub>1</sub>)</code>,第二个控制点的坐标为 <code>P<sub>2</sub> = (x<sub>2</sub>, y<sub>2</sub>)</code>,以此类推,曲线坐标由方程式描述,这个方程式依赖属于区间 `[0,1]` 的参数 `t`
5757

58-
- The formula for a 2-points curve:
58+
- 有两个控制点的曲线方程:
5959

6060
<code>P = (1-t)P<sub>1</sub> + tP<sub>2</sub></code>
61-
- For three points:
61+
- 有三个控制点的曲线方程:
6262

6363
<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:
64+
- 有四个控制点的曲线方程:
6565

6666
<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>
6767

68-
These are vector equations.
68+
这些是矢量方程。
6969

70-
We can rewrite them coordinate-by-coordinate, for instance the 3-point curve:
70+
我们可以逐坐标重写它们,例如 3 点曲线:
7171

7272
- <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>
7373
- <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>
7474

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.
75+
我们应该放置 3 个控制点的坐标,而不是 <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>
7676

77-
For instance, if control points are `(0,0)`, `(0.5, 1)` and `(1, 0)`, the equations are:
77+
例如,如果控制点是 `(0,0)``(0.5, 1)` `(1, 0)`,则方程式为:
7878

7979
- <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>
8080
- <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>
8181

82-
Now as `t` runs from `0` to `1`, the set of values `(x,y)` for each `t` forms the curve.
82+
现在随着 `t` `0` `1` 变化,每个 `t` 对应的 `(x,y)` 集合可以构成曲线。
8383

84-
That's probably too scientific, not very obvious why curves look like that, and how they depend on control points.
84+
这可能太学术化了,对于曲线为什么看起来像这样以及它们如何依赖于控制点的描述并不是很明显。
8585

86-
So here's the drawing algorithm that may be easier to understand.
86+
所以绘制算法可能更容易理解。
8787

88-
## De Casteljau's algorithm
88+
## 德卡斯特里奥算法
8989

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.
90+
[德卡斯特里奥算法](https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm)与曲线的数学定义相同,但直观地显示了曲线是如何被建立的。
9191

92-
Let's see it on the 3-points example.
92+
让我们看看 3 个控制点的例子。
9393

94-
Here's the demo, and the explanation follow.
94+
这里是一个演示,随后会有解释。
9595

96-
Points can be moved by the mouse. Press the "play" button to run it.
96+
控制点可以用鼠标移动,点击 “play” 运行演示。
9797

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

100-
**De Casteljau's algorithm of building the 3-point bezier curve:**
100+
**德卡斯特里奥算法构造三点贝塞尔曲线:**
101101

102-
1. Draw control points. In the demo above they are labeled: `1`, `2`, `3`.
103-
2. Build segments between control points 1 -> 2 -> 3. In the demo above they are <span style="color:#825E28">brown</span>.
104-
3. The parameter `t` moves from `0` to `1`. In the example above the step `0.05` is used: the loop goes over `0, 0.05, 0.1, 0.15, ... 0.95, 1`.
102+
1. 绘制控制点。在上面的演示中,它们标有:`1``2``3`
103+
2. 创造控制点 1 -> 2 -> 3 间的线段. 在上面的演示中它们是<span style="color:#825E28">棕色</span>的。
104+
3. 参数 `t` `0` to `1` 变化。 在上面的演示中取值 `0.05`:循环遍历 `0, 0.05, 0.1, 0.15, ... 0.95, 1`
105105

106-
For each of these values of `t`:
106+
对于每一个 `t` 的取值:
107107

108-
- On each <span style="color:#825E28">brown</span> segment we take a point located on the distance proportional to `t` from its beginning. As there are two segments, we have two points.
108+
- 在每一个<span style="color:#825E28">棕色</span>线段上我们取一个点,这个点距起点的距离按比例 `t` 取值。由于有两条线段,我们能得到两个点。
109109

110-
For instance, for `t=0` -- both points will be at the beginning of segments, and for `t=0.25` -- on the 25% of segment length from the beginning, for `t=0.5` -- 50%(the middle), for `t=1` -- in the end of segments.
110+
例如,当 `t=0` — 所有点都在线段起点处,当 `t=0.25` — 点到起点的距离为线段长度的 25%,当 `t=0.5` 50%(中间),当 `t=0.25` — 线段终点。
111111

112-
- Connect the points. On the picture below the connecting segment is painted <span style="color:#167490">blue</span>.
112+
- 连接这些点,下面这张图中连好的线被绘制成<span style="color:#167490">蓝色</span>
113113

114114

115-
| For `t=0.25` | For `t=0.5` |
115+
| `t=0.25` | `t=0.5` |
116116
| ------------------------ | ---------------------- |
117117
| ![](bezier3-draw1.png) | ![](bezier3-draw2.png) |
118118

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>.
119+
4. 现在在<span style="color:#167490">蓝色</span>线段上取一个点,距离比例取相同数值的 `t`。也就是说,当 `t=0.25`(左图)时,我们取到的点位于线段的左 1/4 终点处,当 `t=0.5`(右图)时 — 线段中间。在上图中这一点是<span style="color:red">红色</span>的。
120120

121-
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.
121+
5. 随着 `t` `0` to `1` 变化,每一个 `t` 的值都会添加一个点到曲线上。这些点的集合就形成的贝塞尔曲线。它在上面的图中是红色的,并且是抛物线状的。
122122

123-
That was a process for 3 points. But the same is for 4 points.
123+
这是三控制点的处理过程,但是对于 4 个点同样适用。
124124

125-
The demo for 4 points (points can be moved by mouse):
125+
4 个控制点的演示(点可以被鼠标移动):
126126

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

129-
The algorithm:
129+
算法:
130130

131-
- Control points are connected by segments: 1 -> 2, 2 -> 3, 3 -> 4. We have 3 <span style="color:#825E28">brown</span> segments.
132-
- For each `t` in the interval from `0` to `1`:
133-
- 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>.
134-
- On these segments we take points proportional to `t`. We get one <span style="color:#167490">blue segment</span>.
135-
- On the blue segment we take a point proportional to `t`. On the example above it's <span style="color:red">red</span>.
136-
- These points together form the curve.
131+
- 控制点通过线段连接:1 -> 22 -> 33 -> 4。 我们能得到 3 条<span style="color:#825E28">棕色</span>的线段。
132+
- 对于 `0` to `1` 之间的每一个 `t`
133+
- 我们在这些线段上距起点距离比例为 `t` 的位置取点。把这些点连接起来,然后得到两条<span style="color:#0A0">绿色线段</span>
134+
- 在这些线段上同样按比例 `t` 取点,得到一条<span style="color:#167490">蓝色线段</span>
135+
- 在蓝色线段按比例 `t` 取点。在上面的例子中是<span style="color:red">红色</span>的。
136+
- 这些点在一起组成了曲线。
137137

138-
The algorithm is recursive and can be generalized for any number of control points.
138+
该算法是递归的,并且可以适应于任意数量的控制点。
139139

140-
Given N of control points, we connect them to get initially N-1 segments.
140+
给定 N 个控制点,我们将它们连接起来以获得初始的 N-1 个线段。
141141

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.
142+
然后对从 `0``1` 的每一个 `t`
143+
- 在每条线段上按 `t` 比例距离取一个点并且连接 —— 会得到 N-2 个线段。
144+
- 在上面得到的每条线段上按 `t` 比例距离取一个点并且连接 —— 会得到 N-3 个线段,以此类推……
145+
- 直到我们得到一个点。得到的这些点就形成了曲线。
146146

147-
Move examples of curves:
147+
曲线的移动演示:
148148

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

151-
With other points:
151+
和其它的点:
152152

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

155-
Loop form:
155+
环形:
156156

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

159-
Not smooth Bezier curve:
159+
非平滑贝塞尔曲线:
160160

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

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.
163+
由于算法是递归的,我们可以构建任何顺序的贝塞尔曲线:使用 5 个、6 个或更多个控制点。但在实践中它们没那么有用。通常我们取 2-3 个点,对于复杂的线条,将几条曲线拼接在一起。这更容易开发和计算。
164164

165-
```smart header="How to draw a curve *through* given points?"
166-
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.
165+
```smart header="如何通过给定点绘制曲线?"
166+
我们使用控制点制作贝塞尔曲线。正如我们所见,它们并不在曲线上。或者更准确地说,第一个和最后一个在曲线上,但其它的不在。
167167
168-
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.
168+
有时我们有另一种任务:绘制一条曲线**通过几个点**,让它们都在一条平滑曲线上。这种任务叫[插值](https://en.wikipedia.org/wiki/Interpolation),这里我们不覆盖讲解它。
169169
170-
There are mathematical formulas for such curves, for instance [Lagrange polynomial](https://en.wikipedia.org/wiki/Lagrange_polynomial).
170+
这些曲线有数学方程式,例如[拉格朗日多项式](https://en.wikipedia.org/wiki/Lagrange_polynomial)
171171
172-
In computer graphics [spline interpolation](https://en.wikipedia.org/wiki/Spline_interpolation) is often used to build smooth curves that connect many points.
172+
在计算机图形中[样条插值](https://en.wikipedia.org/wiki/Spline_interpolation)通常用于构建连接多个点的平滑曲线。
173173
```
174174

175-
## Summary
175+
## 总结
176176

177-
Bezier curves are defined by their control points.
177+
贝塞尔曲线由其控制点定义。
178178

179-
We saw two definitions of Bezier curves:
179+
贝塞尔曲线的两种定义方法:
180180

181-
1. Using a mathematical formulas.
182-
2. Using a drawing process: De Casteljau's algorithm
181+
1. 使用数学方程式。
182+
2. 使用绘图过程:德卡斯特里奥算法
183183

184-
Good properties of Bezier curves:
184+
贝塞尔曲线的优点:
185185

186-
- We can draw smooth lines with a mouse by moving around control points.
187-
- Complex shapes can be made of several Bezier curves.
186+
- 我们可以通过控制点移动来用鼠标绘制平滑线条。
187+
- 复杂的形状可以由多条贝塞尔曲线组成。
188188

189-
Usage:
189+
用途:
190190

191-
- In computer graphics, modeling, vector graphic editors. Fonts are described by Bezier curves.
192-
- In web development -- for graphics on Canvas and in the SVG format. By the way, "live" examples above are written in SVG. They are actually a single SVG document that is given different points as parameters. You can open it in a separate window and see the source: [demo.svg](demo.svg?p=0,0,1,0.5,0,0.5,1,1&animate=1).
193-
- In CSS animation to describe the path and speed of animation.
191+
- 在计算机图形学,建模,矢量图形编辑器中。字体由贝塞尔曲线描述。
192+
- 在 Web 开发中 — 用于 Canvas 上的图形和 SVG 格式。顺便说一下,上面的“实时”示例是用 SVG 编写的。它们实际上是一个 SVG 文档,被赋予不同的控制点做参数。你可以在单独的窗口中打开它并查源码:[demo.svg](demo.svg?p=0,0,1,0.5,0,0.5,1,1&animate=1)
193+
- CSS 动画中描述动画的路径和速度。

3-animation/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Animation
1+
# 动画
22

3-
CSS and JavaScript animations.
3+
CSS JavaScript 动画。

0 commit comments

Comments
 (0)