洛伦兹吸引子浏览器

MakeCode Arcade 游戏编程
回复
头像
shaoziyang
帖子: 3917
注册时间: 2019年 10月 21日 13:48

洛伦兹吸引子浏览器

#1

帖子 shaoziyang »

洛伦兹吸引子是气象研究中的一个模型,也是著名的蝴蝶效应的由来。下面在arcade中展示了洛伦兹蝴蝶的图像,按任意键将随机产生a/b/c参数并重新绘图。

 
lorenz.gif
lorenz.gif (53.27 KiB) 查看 2057 次
 

在线编辑


参考代码

代码: 全选

function init() {
    scene.backgroundImage().fillRect(0, 0, scene.screenWidth(), scene.screenHeight(), 15)
    x = 1
    y = 1
    z = 1
    a = Math.randomRange(50, 200) / 10
    b = Math.randomRange(150, 300) / 10
    c = Math.randomRange(10, 30) / 10
    scene.backgroundImage().print(a.toString(), 0, 0)
    scene.backgroundImage().print(b.toString(), 0, 10)
    scene.backgroundImage().print(c.toString(), 0, 20)
}
function calc() {
    x1 = x - a * x * dt + a * y * dt
    y1 = y + b * x * dt - y * dt - x * z * dt
    z1 = z - c * z * dt + x * y * dt
    color = Math.abs(x1 - x) + Math.abs(y1 - y) + Math.abs(z1 - z)
    console.logValue("L", color)
    color = Math.round(color * 4) % 14 + 1
    console.logValue("C", color)
    draw((x + y) / 2, z, (x1 + y1) / 2, z1)
    x = x1
    y = y1
    z = z1
}
function draw(x1: number, y1: number, x2: number, y2: number) {
    // color = Math.abs(x1 - x2) + Math.abs(y1 - y2)
    // console.logValue("L", color)
    // color = Math.round(color * 4) % 14 + 1
    // console.logValue("C", color)
    scene.backgroundImage().drawLine(x1 * 3 + scene.screenWidth() / 2, scene.screenHeight() - y1 * 3, x2 * 3 + scene.screenWidth() / 2, scene.screenHeight() - y2 * 3, color)
}
controller.anyButton.onEvent(ControllerButtonEvent.Pressed, function () {
    init()
})
let color = 0
let z1 = 0
let y1 = 0
let x1 = 0
let x = 0
let y = 0
let z = 0
let dt = 0
let a = 0
let b = 0
let c = 0
c = 2
b = 15
a = 5
dt = 0.02
z = 1
y = 1
x = 1
forever(function () {
    calc()
    if (x > 100) {
        music.playTone(262, music.beat(BeatFraction.Eighth))
        init()
    }
})
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章