From 9ccf81abc2fb6165af854bac7560c0d52af104e0 Mon Sep 17 00:00:00 2001 From: Zhao Xin <7176466@qq.com> Date: Sat, 17 Dec 2022 19:31:24 +0800 Subject: [PATCH] update --- 10000.测试用.html => 10.单摆.html | 22 ++--- 11.风扇.html | 150 ++++++++++++++++++++++++++++++ 6.引力模拟.html | 7 +- 9.圆周与正弦波.html | 62 ++++++++++++ TEST.html | 28 ++++++ lib/pendulum.js | 37 ++++++++ lib/utils.js | 8 +- 7 files changed, 297 insertions(+), 17 deletions(-) rename 10000.测试用.html => 10.单摆.html (65%) create mode 100644 11.风扇.html create mode 100644 9.圆周与正弦波.html create mode 100644 TEST.html create mode 100644 lib/pendulum.js diff --git a/10000.测试用.html b/10.单摆.html similarity index 65% rename from 10000.测试用.html rename to 10.单摆.html index bb40d1e..393009b 100644 --- a/10000.测试用.html +++ b/10.单摆.html @@ -5,25 +5,25 @@ - 测试 + 单摆 + diff --git a/11.风扇.html b/11.风扇.html new file mode 100644 index 0000000..481c12d --- /dev/null +++ b/11.风扇.html @@ -0,0 +1,150 @@ + + + + + + + + 风扇 + + + + + + + +
+ + diff --git a/6.引力模拟.html b/6.引力模拟.html index 2297bb6..e99b068 100644 --- a/6.引力模拟.html +++ b/6.引力模拟.html @@ -11,7 +11,7 @@ + + + + + + +
+ + diff --git a/TEST.html b/TEST.html new file mode 100644 index 0000000..85b5b22 --- /dev/null +++ b/TEST.html @@ -0,0 +1,28 @@ + + + + + + + + 风扇 + + + + + + + +
+ + diff --git a/lib/pendulum.js b/lib/pendulum.js new file mode 100644 index 0000000..c342ec2 --- /dev/null +++ b/lib/pendulum.js @@ -0,0 +1,37 @@ +class SimplePendulum extends p5.Vector { + constructor(x, y) { + super(x, y); + this.角度 = 0; + this.角速度 = 0; + this.摆长 = 100; + this.重锤 = createVector(this.x, this.y + this.摆长); + } + + update(damping = 0) { + if (mouseIsPressed) { + this.重锤 = createVector(mouseX, mouseY); + this.摆长 = this.dist(this.重锤); + this.角速度 = 0; + const 向下矢量 = createVector(0, 1); + this.角度 = p5.Vector.sub(this.重锤, this).angleBetween(向下矢量); + } else { + // 未考虑时间精确性及重力加速度 + this.角速度 -= sin(this.角度) / this.摆长; // -sin(Θ)g/L + this.角速度 = lerp(this.角速度, 0, damping); // 模拟阻尼 + this.角度 += this.角速度; + this.重锤.x = this.x + this.摆长 * sin(this.角度); + this.重锤.y = this.y + this.摆长 * cos(this.角度); + } + } + + draw() { + push(); + fill(128); + stroke(255); + strokeWeight(1); + line(this.x, this.y, this.重锤.x, this.重锤.y); + circle(this.重锤.x, this.重锤.y, 20); + circle(this.x, this.y, 4); + pop(); + } +} diff --git a/lib/utils.js b/lib/utils.js index b01161f..5858fe9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -9,21 +9,21 @@ function playdemos(list, delay = 10) { list[n](); } -function drawGrid(fade = 64) { +function drawGrid(fade = 64, color = 255) { push(); colorMode(RGB); - stroke(200, fade); + stroke(color, fade); strokeWeight(1); for (let x = 0; x < width; x += 10) line(x, 0, x, height); for (let y = 0; y < height; y += 10) line(0, y, width, y); pop(); } -function textInfo(title, date, name = "赵海洋爸爸") { +function textInfo(title, date, name = "赵海洋爸爸", color = 255) { push(); colorMode(RGB); + fill(color); noStroke(); - fill(200); text(`作品:${title}`, 30, height - 70); text(`作者:${name}`, 30, height - 50); text(`日期:${date}`, 30, height - 30);