当前位置: 首页 > 知识库问答 >
问题:

如何在加工中创建一个简单的L-系统

曹兴贤
2023-03-14
A →
AB →
ABA →
ABAAB →
ABAABABA → etc...

(附:我不太懂加工)

共有1个答案

常献
2023-03-14

我建议看看处理>示例>主题>Franctals和L-系统,以及Daniel Shiffman的代码书的辉煌性质,特别是第8章:分形(8.6L-系统)

另外,请务必查看Daniel Jones的L-System Project:in包括解释和处理源代码。

Bellow是PenroseTile的例子,从处理到移植到Javascript,并在p5.js端口上运行。

var ds;
function setup() {
  createCanvas(640, 360);
  ds = new PenroseLSystem();
  ds.simulate(4);
}

function draw() {
  background(0);
  ds.render();
}

//*
function PenroseLSystem(){

  this.steps = 0;
  this.ruleW = "YF++ZF4-XF[-YF4-WF]++";
  this.ruleX = "+YF--ZF[3-WF--XF]+";
  this.ruleY = "-WF++XF[+++YF++ZF]-";
  this.ruleZ = "--YF++++WF[+ZF++++XF]--XF";
  
  this.axiom = "[X]++[X]++[X]++[X]++[X]";
  this.rule = "F+F-F";
  this.production = "";

  this.startLength = 460.0;
  this.theta = radians(36);  
  
  this.generations = 0;

  
  this.reset = function() {
    this.production = this.axiom;
    this.drawLength = this.startLength;
    this.generations = 0;
  }

  this.reset();

  this.getAge = function() {
    return this.generations;
  }

  this.iterate = function(prod_,rule_) {
    var newProduction = "";
    for (var i = 0; i < prod_.length; i++) {
      var step = this.production.charAt(i);
      if (step == 'W') {
        newProduction = newProduction + this.ruleW;
      } 
      else if (step == 'X') {
        newProduction = newProduction + this.ruleX;
      }
      else if (step == 'Y') {
        newProduction = newProduction + this.ruleY;
      }  
      else if (step == 'Z') {
        newProduction = newProduction + this.ruleZ;
      } 
      else {
        if (step != 'F') {
          newProduction = newProduction + step;
        }
      }
    }

    this.drawLength = this.drawLength * 0.5;
    this.generations++;
    return newProduction;
  }
  
  this.simulate = function(gen) {
    while (this.getAge() < gen) {
      this.production = this.iterate(this.production, this.rule);
    }
  }

  this.render = function() {
    translate(width/2, height/2);
    var pushes = 0;
    var repeats = 1;
    this.steps += 12;          
    if (this.steps > this.production.length) {
      this.steps = this.production.length;
    }

    for (var i = 0; i < this.steps; i++) {
      var step = this.production.charAt(i);
      var stepCode = this.production.charCodeAt(i);
      if (step == 'F') {
        stroke(255, 60);
        for (var j = 0; j < repeats; j++) {
          line(0, 0, 0, -this.drawLength);
          noFill();
          translate(0, -this.drawLength);
        }
        repeats = 1;
      } 
      else if (step == '+') {
          rotate(this.theta * repeats);
        repeats = 1;
      } 
      else if (step == '-') {
          rotate(-this.theta * repeats);
        repeats = 1;
      } 
      else if (step == '[') {
        pushes++;            
        //pushMatrix();
        push();
      } 
      else if (step == ']') {
        //popMatrix();
        pop();
        pushes--;
      } 
      else if ( (stepCode >= 48) && (stepCode <= 57) ) {
        repeats = stepCode - 48;
      }
    }

    // Unpush if we need too
    while (pushes > 0) {
      // popMatrix();
      pop();
      pushes--;
    }
  }


}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>
 类似资料:
  • 问题内容: 我正在尝试创建一个代理服务器,以将请求从客户端传递到第三方网站(例如google)。我的代理只需将传入请求镜像到目标站点上的相应路径,因此,如果我的客户请求的url为: 应提供以下资源: 这是我想出的: 它适用于html页面,但对于其他类型的文件,它仅返回空白页面或来自目标站点的错误消息(在不同站点中有所不同)。 问题答案: 我认为处理从第三方服务器收到的响应不是一个好主意。这只会增加

  • 问题内容: 我正在寻找与JavaScript相同的效果。 我今天下午使用Twisted.web编写了一个基于Web的简单解释器。您基本上是通过表单提交Python代码块的,客户端来抓取并执行它。我希望能够发出一个简单的弹出消息,而不必每次都重写一堆样板wxPython或TkInter代码(因为该代码通过表单提交然后消失了)。 我尝试过tkMessageBox: 但这会在后台用tk图标打开另一个窗口

  • 因此,目标是每次锤子或水桶落下时添加一个黑色圆圈而不击中人,然后每次人进门时添加4个圆圈,当第一行圆圈被填满时,它将进入第二行,直到整个屏幕被填满,然后重新设置。而且,随着人被锤子或水桶击中,会出现一整排的红圈。我不知道该怎么做,谢谢。

  • 问题内容: 如何在Magento中完成以下任务? 使用控制器/视图/模型方法显示“ Hello World”消息。因此,如果我去过,它将显示字符串“ Hello World”。能够在我的网站模板中显示此字符串(例如,页眉,页脚等)将是一个好处。 如何向该控制器(如果需要,或新控制器)添加方法,该方法与模型交互并执行查询,并将行(包含columns )返回给控制器?然后使用控制器包含一个视图,该视图

  • 本文向大家介绍构建一个简单的CaaS系统,包括了构建一个简单的CaaS系统的使用技巧和注意事项,需要的朋友参考一下 在CaaS系统出现前企业应用架构基本被IaaS/SaaS/PaaS等模式垄断,直到Docker的出现为我们打开了另一个扇大门,废话不说了,我们直奔主题。 我们先了解下一个简单的CaaS系统是如何为用户提供服务的: 企业用户上传它的应用代码或其他代码托管方式,我们生成用户应用的镜像,或

  • 我想通过在处理中使用一个草图来创建两个窗口。 我想做的是,如果我在一个窗口中单击一个按钮,那么一些图像就会出现在另一个窗口中。 我搜索了谷歌,找到了一些例子。实际上,我在这个“堆栈溢出网络”中发现了相同的问题。以下是链接。 在处理中为单个草图创建多个窗口http://forum.processing.org/one/topic/multiple-windows-2-4-2011.html 这是第二