快速分形浏览器v2

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

快速分形浏览器v2

#1

帖子 shaoziyang » 2019年 11月 18日 11:22

在第一版基础上,增加了快速预览、guess计算加速、平移加速等方法,极大缩短了计算时间。
 
quick mandelbrot.gif
quick mandelbrot.gif (113.1 KiB) 查看 184 次
 

在线编辑


参考程序

代码: 全选

function calc(x: number, y: number) {
    a = x
    b = y
    iter = 0
    for (let p = 0; p <= iteration; p++) {
        tx = x * x - y * y + a
        ty = 2 * x * y + b
        if (tx * tx + ty * ty > 4) {
            iter = p
            return
        }
        x = tx
        y = ty
    }
}
controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
    if (!(flag)) {
        cx = cx - wx / 8 / zoom
        _pic.fill(15)
        _pic.drawImage(pic, scene.screenWidth() / 8, 0)
        pic.drawImage(_pic, 0, 0)
        draw(0, 0, scene.screenWidth() / 8, scene.screenHeight(), cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx - wx * 6 / (16 * zoom), cy + wy / (2 * zoom))
    }
})
controller.down.onEvent(ControllerButtonEvent.Pressed, function () {
    if (!(flag)) {
        cy = cy + wy / 8 / zoom
        _pic.fill(15)
        _pic.drawImage(pic, 0, -scene.screenHeight() / 8)
        pic.drawImage(_pic, 0, 0)
        draw(0, scene.screenHeight() * 7 / 8, scene.screenWidth(), scene.screenHeight(), cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx + wx / (2 * zoom), cy - wy * 6 / (16 * zoom))
    }
})
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
    if (!(flag)) {
        for (let q = scene.screenWidth() / 2; q >= 0; q--) {
            pic.drawRect(scene.screenWidth() / 2 - q - 1, scene.screenHeight() / 2 - q - 1, q * 2 + 2, q * 2 + 2, 0)
            pic.drawRect(scene.screenWidth() / 2 - q, scene.screenHeight() / 2 - q, q * 2, q * 2, 1)
            pause(1)
        }
        zoom = zoom / 2
        draw(0, 0, scene.screenWidth(), scene.screenHeight(), cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx + wx / (2 * zoom), cy + wy / (2 * zoom))
    }
})
function draw(px1: number, py1: number, px2: number, py2: number, mx1: number, my1: number, mx2: number, my2: number) {
    flag = true
    mySprite = sprites.create(img`
        . . . . c c c b b b b b . . . .
        . . c c b 4 4 4 4 4 4 b b b . .
        . c c 4 4 4 4 4 5 4 4 4 4 b c .
        . e 4 4 4 4 4 4 4 4 4 5 4 4 e .
        e b 4 5 4 4 5 4 4 4 4 4 4 4 b c
        e b 4 4 4 4 4 4 4 4 4 4 5 4 4 e
        e b b 4 4 4 4 4 4 4 4 4 4 4 b e
        . e b 4 4 4 4 4 5 4 4 4 4 b e .
        8 7 e e b 4 4 4 4 4 4 b e e 6 8
        8 7 2 e e e e e e e e e e 2 7 8
        e 6 6 2 2 2 2 2 2 2 2 2 2 6 c e
        e c 6 7 6 6 7 7 7 6 6 7 6 c c e
        e b e 8 8 c c 8 8 c c c 8 e b e
        e e b e c c e e e e e c e b e e
        . e e b b 4 4 4 4 4 4 4 4 e e .
        . . . c c c c c e e e e e . . .
    `, SpriteKind.Player)
    ts = game.runtime()
    s = "" + convertToText(px1) + "," + convertToText(py1) + "," + convertToText(px2) + "," + convertToText(py2) + " " + convertToText(mx1) + "," + convertToText(my1) + "," + convertToText(mx2) + "," + convertToText(my2)
    console.log(s)
    dx = (mx2 - mx1) / (px2 - px1)
    dy = (my2 - my1) / (py2 - py1)
    for (let i = px1; i <= px2; i += 4) {
        for (let j = py1; j <= py2; j += 4) {
            calc(mx1 + i * dx, my1 + j * dy)
            pic.setPixel(i, j, iter)
        }
        pause(0)
    }
    for (let l = px1; l <= px2; l += 4) {
        for (let m = py1; m <= py2; m += 4) {
            calc(mx1 + l * dx, my1 + m * dy)
            c1 = iter
            calc(mx1 + (l + 3) * dx, my1 + m * dy)
            c2 = iter
            calc(mx1 + (l + 3) * dx, my1 + (m + 3) * dy)
            c3 = iter
            calc(mx1 + l * dx, my1 + (m + 3) * dy)
            c4 = iter
            if ((c1 == c2) && (c1 == c3) && (c1 == c4))
                pic.fillRect(l, m, 4, 4, iter)
            else {
                picb.fill(0)
                for (let n = 0; n < 4; n++)
                    for (let o = 0; o < 4; o++) {
                        calc(mx1 + (l + n) * dx, my1 + (m + o) * dy)
                        picb.setPixel(n, o, iter)
                    }
                pic.drawImage(picb, l, m)
            }
        }
        pause(0)
    }
    te = game.runtime()
    s = "Elapsed:" + convertToText(te - ts) + "ms"
    console.log(s)
    mySprite.destroy()
    flag = false
}
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
    if (!(flag)) {
        for (let k = 0; k <= scene.screenWidth() / 2; k++) {
            pic.drawRect(scene.screenWidth() / 2 - k - 1, scene.screenHeight() / 2 - k - 1, k * 2 + 2, k * 2 + 2, 1)
            pic.drawRect(scene.screenWidth() / 2 - k, scene.screenHeight() / 2 - k, k * 2, k * 2, 0)
            pause(1)
        }
        zoom = zoom * 2
        draw(0, 0, scene.screenWidth(), scene.screenHeight(), cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx + wx / (2 * zoom), cy + wy / (2 * zoom))
    }
})
controller.right.onEvent(ControllerButtonEvent.Pressed, function () {
    if (!(flag)) {
        cx = cx + wx / 8 / zoom
        _pic.fill(15)
        _pic.drawImage(pic, -scene.screenWidth() / 8, 0)
        pic.drawImage(_pic, 0, 0)
        draw(scene.screenWidth() * 7 / 8, 0, scene.screenWidth(), scene.screenHeight(), cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx - wx * 6 / (16 * zoom), cy + wy / (2 * zoom))
    }
})
controller.up.onEvent(ControllerButtonEvent.Pressed, function () {
    if (!(flag)) {
        cy = cy - wy / 8 / zoom
        _pic.fill(15)
        _pic.drawImage(pic, 0, scene.screenHeight() / 8)
        pic.drawImage(_pic, 0, 0)
        draw(0, 0, scene.screenWidth(), scene.screenHeight() / 8, cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx + wx / (2 * zoom), cy - 6 * wy / (16 * zoom))
    }
})
let te = 0
let s = ""
let ts = 0
let mySprite: Sprite = null
let flag = false
let y = 0
let x = 0
let ty = 0
let tx = 0
let b = 0
let a = 0
let cy = 0
let zoom = 0
let iteration = 0
let wy = 0
let wx = 0
let cx = 0
let iter = 0
let dy = 0
let dx = 0
let _pic: Image = null
let pic: Image = null
let c1 = 0
let c2 = 0
let c3 = 0
let c4 = 0
cx = -0.5
wx = 3
wy = 3
iteration = 50
pic = image.create(scene.screenWidth(), scene.screenHeight())
_pic = image.create(scene.screenWidth(), scene.screenHeight())
let picb = image.create(4, 4)
scene.setBackgroundImage(pic)
zoom = 1
draw(0, 0, scene.screenWidth(), scene.screenHeight(), cx - wx / (2 * zoom), cy - wy / (2 * zoom), cx + wx / (2 * zoom), cy + wy / (2 * zoom))
 

回复

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