【问题标题】:Drawing fractals in javascript? [closed]在javascript中绘制分形? [关闭]
【发布时间】:2015-07-31 08:31:26
【问题描述】:

我正在开发一个 2d(对于初学者)分形项目,作为一种有趣的方式来了解有关递归函数的更多信息。我正在寻找一些函数,它根据一些条件规则输出基于线的分形的坐标。类似于 H 树。

这样的函数/算法有名称吗?是否有任何简单的通用实现可供查看?

【问题讨论】:

    标签: javascript math fractals


    【解决方案1】:

    只是为了好玩:

    var c = document.getElementById("cnv").getContext("2d");
    var f = Math.sqrt(2);
    
    function hor(x, y, len) {
      if(len < 1) return;
    
      c.beginPath();
      c.moveTo(x - len/2, y);
      c.lineTo(x + len/2, y);
      c.stroke();
    
      setTimeout(function() {
        ver(x - len/2, y, len/f);
        ver(x + len/2, y, len/f);
      }, 500);
    }
    
    function ver(x, y, len) {
      if(len < 1) return;
    
      c.beginPath();
      c.moveTo(x, y - len/2);
      c.lineTo(x, y + len/2);
      c.stroke();
    
      setTimeout(function() {
        hor(x, y - len/2, len/f);
        hor(x, y + len/2, len/f);
      }, 500);
    }
    
    hor(300, 90, 150)
    &lt;canvas id="cnv" width="600" height="180" /&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 2019-05-20
      相关资源
      最近更新 更多