使用IFS创建照片分形

时间:2016-10-12 07:13:32

标签: javascript fractals iterated-function

我几周前就开始使用分形...我知道迭代函数系统的理论,我可以创建简单的分形(在javascript中),如Fern,Levi Curve,Dragons ......但我和#39;我试图将这个理论应用于数字图像,即给定图像应用IFS然后获得图像的分形。但是我被卡住了:首先我不知道javascript是否可以帮助我解决这个问题。我希望你可以用一段代码或一些语言推荐来帮助我。谢谢:))

PS。这是Fern分形的代码(为数学代码提取)

var canvas  = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    width   = canvas.width  = window.innerWidth,
    height  = canvas.height = window.innerHeight;
var rules = [{
    a:0, b:0, c:0, d:0.16,
    tx:0.203, ty:-0.035,
    p:0.01,
    color:"yellow"
}, {
    a:0.2, b:0.26, c:-0.23, d:0.22,
    tx:0.173, ty:1.628,
    p:0.07,
    color:"blue"
}, {
    a:-0.15, b:-0.28, c:-0.26, d:0.24,
    tx:0.222, ty:0.465,
    p:0.07,
    color:"red"
}, {
    a:0.85, b:-0.04, c:0.04, d:0.85,
    tx:0.029, ty:1.597,
    p:0.85,
    color:"green"
}];

var x = Math.random(),
    y = Math.random();

context.translate(width / 2, height);
iteracion();
function iteracion(){
    for (var i=0; i<150; i++) {
        var rule = getRule(),
            x1 = x*rule.a + y*rule.b + rule.tx,
            y1 = x*rule.c + y*rule.d + rule.ty
        x = x1;
        y = y1;
        plot(x, y, rule.color);
    }
    requestAnimationFrame(iteracion);
}
function getRule() {
    var rand = Math.random();
    for (var i=0; i<rules.length; i++) {
        var rule = rules[i];
        if (rand < rule.p) {
            return rule;
        }
        rand -= rule.p
    }
}
function plot(x, y, color) {
    context.fillStyle = color;
    context.fillRect(x*60, -y*60, 1, 1);
}

0 个答案:

没有答案