Skip to content

Commit acb6fa1

Browse files
committed
translate all examples in 08_Math
1 parent 94aa9a6 commit acb6fa1

20 files changed

+154
-177
lines changed

src/data/examples/zh-Hans/08_Math/00_incrementdecrement.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
2-
* @name Increment Decrement
3-
* @description Writing "a++" is equivalent to "a = a + 1".
4-
* Writing "a--" is equivalent to "a = a - 1".
2+
* @name 增量/减量
3+
* @description "a++" 等于 "a = a + 1"。 "a--" 等于 "a = a - 1"。
54
*/
65
let a;
76
let b;

src/data/examples/zh-Hans/08_Math/01_operatorprecedence.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
/*
2-
* @name Operator Precedence
3-
* @description If you don't explicitly state the order in which an
4-
* expression is evaluated, they are evaluated based on the operator
5-
* precedence. For example, in the statement "4+2*8", the 2 will
6-
* first be multiplied by 8 and then the result will be added to 4.
7-
* This is because the "*" has a higher precedence than the "+". To avoid
8-
* ambiguity in reading the program, it is recommended that is statement
9-
* is written as "4+(2*8)". The order of evaluation can be controlled
10-
* through placement of parenthesis in the code. A table of operator
11-
* precedence follows below.
2+
* @name 操作符优先级
3+
* @description 如果没有明确地指明表达式求值的次序,表达式将根据操作符的优先级来求值。
4+
* 例如,在 "4+2*8" 的语句中, 2 会先乘 8,其结果再加上 4。
5+
* 这是因为 "*" 的优先级比 "+" 的高。
6+
* 为了避免读取程序时的模凌两可,建议将该语句写成 "4+(2*8)"。
7+
* 在代码中加上括号可以控制求值的次序。
8+
* 以下是操作符优先级列表。
129
*/
13-
// The highest precedence is at the top of the list and
14-
// the lowest is at the bottom.
15-
// Multiplicative: * / %
16-
// Additive: + -
17-
// Relational: < > <= >=
18-
// Equality: == !=
19-
// Logical AND: &&
20-
// Logical OR: ||
21-
// Assignment: = += -= *= /= %=
10+
// 最高级(最先执行)的位于列表上方,最低级(最后执行)的位于下方。
11+
// 乘法: * / %
12+
// 加法: + -
13+
// 比较: < > <= >=
14+
// 相等: == !=
15+
// 逻辑与 (AND): &&
16+
// 逻辑或 (OR): ||
17+
// 赋值: = += -= *= /= %=
2218
function setup() {
2319
createCanvas(710, 400);
2420
background(51);
@@ -27,25 +23,23 @@ function setup() {
2723

2824
stroke(204);
2925
for (let i = 0; i < width - 20; i += 4) {
30-
// The 30 is added to 70 and then evaluated
31-
// if it is greater than the current value of "i"
32-
// For clarity, write as "if (i > (30 + 70)) {"
26+
// 30 和 70 先相加,其结果再和现在的 i 值比较大小
27+
// 更清楚的写法是:"if (i > (30 + 70)) {"
3328
if (i > 30 + 70) {
3429
line(i, 0, i, 50);
3530
}
3631
}
3732

3833
stroke(255);
39-
// The 2 is multiplied by the 8 and the result is added to the 4
40-
// For clarity, write as "rect(5 + (2 * 8), 0, 90, 20);"
34+
// 2 和 8 先相乘,其结果再加 4
35+
// 更清楚的写法是:"rect(5 + (2 * 8), 0, 90, 20);"
4136
rect(4 + 2 * 8, 52, 290, 48);
4237
rect((4 + 2) * 8, 100, 290, 49);
4338

4439
stroke(153);
4540
for (let i = 0; i < width; i += 2) {
46-
// The relational statements are evaluated
47-
// first, and then the logical AND statements and
48-
// finally the logical OR. For clarity, write as:
41+
// 先算关系表达式,再是逻辑与 (AND),最后是逻辑或 (OR)
42+
// 更清楚的写法是:
4943
// "if(((i > 20) && (i < 50)) || ((i > 100) && (i < width-20))) {"
5044
if ((i > 20 && i < 50) || (i > 100 && i < width - 20)) {
5145
line(i, 151, i, height - 1);

src/data/examples/zh-Hans/08_Math/02_distance1d.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
2-
* @name Distance 1D
3-
* @description Move the mouse left and right to control
4-
* the speed and direction of the moving shapes.
2+
* @name 一维间距
3+
* @description 左右移动鼠标来控制移动形状的速度和方向。
54
*/
65
let xpos1;
76
let xpos2;

src/data/examples/zh-Hans/08_Math/03_distance2d.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
2-
* @name Distance 2D
3-
* @description Move the mouse across the image to obscure
4-
* and reveal the matrix. Measures the distance from the mouse
5-
* to each square and sets the size proportionally.
2+
* @name 二维间距
3+
* @description 在图片上移动鼠标来遮盖并显示矩阵。
4+
* 测量鼠标到每个正方形的距离,并按比例设置大小。
65
*/
76
let max_distance;
87

src/data/examples/zh-Hans/08_Math/04_sine.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* @name Sine
3-
* @description Smoothly scaling size with the sin() function.
2+
* @name 正弦
3+
* @description 使用 sin() 函数平滑地缩放大小。
44
*/
55
let diameter;
66
let angle = 0;
@@ -18,7 +18,7 @@ function draw() {
1818
let d1 = 10 + (sin(angle) * diameter) / 2 + diameter / 2;
1919
let d2 = 10 + (sin(angle + PI / 2) * diameter) / 2 + diameter / 2;
2020
let d3 = 10 + (sin(angle + PI) * diameter) / 2 + diameter / 2;
21-
21+
2222
ellipse(0, height / 2, d1, d1);
2323
ellipse(width / 2, height / 2, d2, d2);
2424
ellipse(width, height / 2, d3, d3);

src/data/examples/zh-Hans/08_Math/05_sincosine.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* @name Sine Cosine
3-
* @description Linear movement with sin() and cos().
4-
* Numbers between 0 and PI*2 (TWO_PI which angles roughly 6.28)
5-
* are put into these functions and numbers between -1 and 1 are returned.
6-
* These values are then scaled to produce larger movements.
2+
* @name 正弦余弦
3+
* @description sin() 函数和 cos() 函数的线性运动。
4+
* 将 0 到 PI*2 (TWO_PI 的角度大致是 6.28) 之间的数字放进这些函数将返回 -1 到 1 之间的数字。
5+
* 然后这些数字将缩放以产生更大的运动。
76
*/
87
let angle1 = 0;
98
let angle2 = 0;

src/data/examples/zh-Hans/08_Math/06_sinewave.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*
2-
* @name Sine Wave
3-
* @description Render a simple sine wave.
4-
* Original by Daniel Shiffman.
2+
* @name 正弦波
3+
* @description Render 是一个简单的正弦波。
4+
* 作者:Daniel Shiffman
55
*/
66

7-
let xspacing = 16; // Distance between each horizontal location
8-
let w; // Width of entire wave
9-
let theta = 0.0; // Start angle at 0
10-
let amplitude = 75.0; // Height of wave
11-
let period = 500.0; // How many pixels before the wave repeats
12-
let dx; // Value for incrementing x
13-
let yvalues; // Using an array to store height values for the wave
7+
let xspacing = 16; // 每个水平位置的距离
8+
let w; // 波的宽度
9+
let theta = 0.0; // 初始角度为 0
10+
let amplitude = 75.0; // 波的高度
11+
let period = 500.0; // 波在重复前的像素个数
12+
let dx; // x 的增量
13+
let yvalues; // 保存波的高度的数组
1414

1515
function setup() {
1616
createCanvas(710, 400);
@@ -26,11 +26,8 @@ function draw() {
2626
}
2727

2828
function calcWave() {
29-
// Increment theta (try different values for
30-
// 'angular velocity' here)
31-
theta += 0.02;
32-
33-
// For every x value, calculate a y value with sine function
29+
// theta 增量(尝试赋予 ‘角速度’ 不同的数值)
30+
// 对于每一个 x 值,使用正弦函数计算 y 值
3431
let x = theta;
3532
for (let i = 0; i < yvalues.length; i++) {
3633
yvalues[i] = sin(x) * amplitude;
@@ -41,7 +38,7 @@ function calcWave() {
4138
function renderWave() {
4239
noStroke();
4340
fill(255);
44-
// A simple way to draw the wave with an ellipse at each location
41+
// 在波上的每个位置画椭圆
4542
for (let x = 0; x < yvalues.length; x++) {
4643
ellipse(x * xspacing, height / 2 + yvalues[x], 16, 16);
4744
}

src/data/examples/zh-Hans/08_Math/07_additivewave.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
/*
2-
* @name Additive Wave
3-
* @description Create a more complex wave by adding two waves together.
4-
* Original by Daniel Shiffman
2+
* @name 加性波
3+
* @description 通过相加两个波来绘制一个更复杂的波。
4+
* 作者:Daniel Shiffman
55
*/
6-
let xspacing = 8; // Distance between each horizontal location
7-
let w; // Width of entire wave
8-
let maxwaves = 4; // total # of waves to add together
6+
let xspacing = 8; // 每个水平位置的距离
7+
let w; // 波的宽度
8+
let maxwaves = 4; // 相加的波的总数
99

1010
let theta = 0.0;
11-
let amplitude = new Array(maxwaves); // Height of wave
12-
// Value for incrementing X, to be calculated
13-
// as a function of period and xspacing
11+
let amplitude = new Array(maxwaves); // 波的高度
12+
// x 的增量值,根据周期和水平位置距离来计算
1413
let dx = new Array(maxwaves);
15-
// Using an array to store height values
16-
// for the wave (not entirely necessary)
14+
// 用数组保存波的高度(不完全需要)
1715
let yvalues;
1816

1917
function setup() {
@@ -24,7 +22,7 @@ function setup() {
2422

2523
for (let i = 0; i < maxwaves; i++) {
2624
amplitude[i] = random(10, 30);
27-
let period = random(100, 300); // Num pixels before wave repeats
25+
let period = random(100, 300); // 波在重复前的像素个数
2826
dx[i] = (TWO_PI / period) * xspacing;
2927
}
3028

@@ -38,20 +36,19 @@ function draw() {
3836
}
3937

4038
function calcWave() {
41-
// Increment theta (try different values
42-
// for 'angular velocity' here
39+
// theta 增量(尝试赋予 ‘角速度’ 不同的数值)
4340
theta += 0.02;
4441

45-
// Set all height values to zero
42+
// 所有高度设为 0
4643
for (let i = 0; i < yvalues.length; i++) {
4744
yvalues[i] = 0;
4845
}
4946

50-
// Accumulate wave height values
47+
// 累积波的高度
5148
for (let j = 0; j < maxwaves; j++) {
5249
let x = theta;
5350
for (let i = 0; i < yvalues.length; i++) {
54-
// Every other wave is cosine instead of sine
51+
// 每一个其他的波是余弦,而不是正弦
5552
if (j % 2 === 0) yvalues[i] += sin(x) * amplitude[j];
5653
else yvalues[i] += cos(x) * amplitude[j];
5754
x += dx[j];
@@ -60,7 +57,7 @@ function calcWave() {
6057
}
6158

6259
function renderWave() {
63-
// A simple way to draw the wave with an ellipse at each location
60+
// 在波上的每个位置画椭圆
6461
noStroke();
6562
fill(255, 50);
6663
ellipseMode(CENTER);
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/*
22
* @name PolarToCartesian
3-
* @description Convert a polar coordinate (r,theta)
4-
* to cartesian (x,y): x = rcos(theta) y = rsin(theta)
5-
* Original by Daniel Shiffman.
3+
* @description 转换极坐标 (r,theta) 到笛卡尔坐标 (x,y): x = rcos(theta) y = rsin(theta)。
4+
* 作者:Daniel Shiffman
65
*/
76
let r;
87

9-
// Angle and angular velocity, accleration
8+
// 角度,角速度,角加速度
109
let theta;
1110
let theta_vel;
1211
let theta_acc;
1312

1413
function setup() {
1514
createCanvas(710, 400);
1615

17-
// Initialize all values
16+
// 初始化所有值
1817
r = height * 0.45;
1918
theta = 0;
2019
theta_vel = 0;
@@ -24,21 +23,21 @@ function setup() {
2423
function draw() {
2524
background(0);
2625

27-
// Translate the origin point to the center of the screen
26+
// 将原点设为屏幕中心
2827
translate(width/2, height/2);
2928

30-
// Convert polar to cartesian
29+
// 转换极坐标到笛卡尔坐标
3130
let x = r * cos(theta);
3231
let y = r * sin(theta);
3332

34-
// Draw the ellipse at the cartesian coordinate
33+
// 在笛卡尔坐标系上画椭圆
3534
ellipseMode(CENTER);
3635
noStroke();
3736
fill(200);
3837
ellipse(x, y, 32, 32);
3938

40-
// Apply acceleration and velocity to angle
41-
// (r remains static in this example)
39+
// 应用加速度和速度到角度上
40+
// (此示例中 r 保持静态)
4241
theta_vel += theta_acc;
4342
theta += theta_vel;
4443
}

src/data/examples/zh-Hans/08_Math/09_arctangent.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
* @name Arctangent
3-
* @description Move the mouse to change the direction of the eyes.<br>The atan2() function computes the angle from each eye to the cursor.
2+
* @name 反正切
3+
* @description 移动鼠标来改变眼球的方向。<br>
4+
* atan2() 函数计算每个眼球到鼠标的角度。
45
*/
56
let e1, e2, e3;
67

0 commit comments

Comments
 (0)