我想在Processing中创建一个简单的l系统。我想改变每一封信' A'到' AB'每一封信' B'到' A',从' A'开始。
结果应如下所示:
A →
AB →
ABA →
ABAAB →
ABAABABA → etc...
(P.S。我对处理知之甚少)
答案 0 :(得分:2)
我建议您查看处理>例子>主题> Franctals和L-Systems 以及Daniel Shiffman的辉煌Nature of Code book,特别是Chapter 8: Fractals (8.6 L-Systems)
另外,请务必查看Daniel Jones' L-System project:in include说明和处理源代码。
Bellow是来自Processing的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>
答案 1 :(得分:1)
我试图和老师一起创建一段代码。这是非常基本的,但我想分享结果:
String txt = "A";
for (int i = 0; i < 5; i ++) {
println(txt);
String newTxt = "";
for (int j = 0; j < txt.length(); j++) {
String letter = txt.substring(j, j+1);
if (letter.equals("A")) {
newTxt += "AB";
} else {
newTxt += "A";
}
}
txt = newTxt;
}
再次感谢您的帮助!
答案 2 :(得分:0)
这是我使用Openframework编写的代码。我相信它可以循环生产出重量最轻,最漂亮的L系统植物,并且不使用任何灯光或纹理。
#include "ofMain.h"
#include "application.h"
int main( )
{
ofSetupOpenGL(512, 512, OF_WINDOW);
ofRunApp(new Application());
}
--------------------------
#include "renderer.h"
void Renderer::setup()
{
ofSetFrameRate(3);
ofSetWindowShape(kWindow_width, kWindow_height);
}
void Renderer::draw()
{
ofFill();
ofSetLineWidth(10);
ofClear(0);
ofScale(1.0f, -1.0f);
ofTranslate(kWindow_width / 2, -kWindow_height + 75);
ofLine(0,0,0, 300);
angleDepart -= 1;
splitLine(0, 300, 20, angleDepart); // faire varier le 20, en 10 et 20, c'est le plus intéressant
}
void Renderer::splitLine(int iX, int iY, int iThickness, float iAngle)
{
if (iThickness - 1 > 0)
{
ofPushMatrix();
ofTranslate(iX, iY);
ofRotateZ(iAngle);
ofSetLineWidth(iThickness - 1);
ofLine(0, 0, iX * 0.75, iY * 0.75);
splitLine(iX * 0.75, iY * 0.75, iThickness - 1, iAngle);
ofPopMatrix();
ofPushMatrix();
ofLine(0, 0, iX * 0.75, iY * 0.75);
ofSetColor(65, ofRandom(255), 0);
splitLine(iX * 0.75, iY * 0.75, iThickness - 2, -iAngle);
ofPopMatrix();
}
}
----------------------------------
#pragma once
#include "ofMain.h"
class Renderer
{
public:
void setup();
void draw();
int angleDepart = 0;
const int kWindow_height = 1000;
const int kWindow_width = 1200;
void splitLine(int iX, int iY, int iThickness, float angle);
};