Hello guys,
I’m sorry that I’m posting here but I haven’t found any other solution for my problem.
I’m using an example code for L-System that comes when downloading examples from “The Nature of Code by Daniel Shiffman” in my version which is 3.5.3
My main problem is that I need to generate images of corals but I need the rewriting rules for the L-System to generate those images.
Could anyone help me?
This is a my code for the main class
LSystem lsys;
Turtle turtle;
void setup() {
size(500, 500);
Rule[] ruleset = new Rule[3];
ruleset[0] = new Rule('A', "[B][B]C");
ruleset[1] = new Rule('B', "+H+H+H+H+H\"A");
ruleset[2] = new Rule('C', "T-H-H-H-H\"A");
lsys = new LSystem("A", ruleset);
turtle = new Turtle(lsys.getSentence(), height/2, radians(7));
}
void draw() {
background(255);
fill(0);
text("Click mouse to generate", 10, height-10);
translate(width/2, height);
rotate(-PI/2);
turtle.render();
noLoop();
}
int counter = 0;
void mousePressed() {
if (counter < 10) {
pushMatrix();
lsys.generate();
turtle.setToDo(lsys.getSentence());
turtle.changeLen(0.7);
popMatrix();
redraw();
counter++;
}
}
- Class LSystem
class LSystem {
String sentence; // The sentence (a String)
Rule[] ruleset; // The ruleset (an array of Rule objects)
int generation; // Keeping track of the generation #
// Construct an LSystem with a startin sentence and a ruleset
LSystem(String axiom, Rule[] r) {
sentence = axiom;
ruleset = r;
generation = 0;
}
// Generate the next generation
void generate() {
// An empty StringBuffer that we will fill
StringBuffer nextgen = new StringBuffer();
// For every character in the sentence
for (int i = 0; i < sentence.length(); i++) {
// What is the character
char curr = sentence.charAt(i);
// We will replace it with itself unless it matches one of our rules
String replace = "" + curr;
// Check every rule
for (int j = 0; j < ruleset.length; j++) {
char a = ruleset[j].getA();
// if we match the Rule, get the replacement String out of the Rule
if (a == curr) {
replace = ruleset[j].getB();
break;
}
}
// Append replacement String
nextgen.append(replace);
}
// Replace sentence
sentence = nextgen.toString();
// Increment generation
generation++;
}
String getSentence() {
return sentence;
}
int getGeneration() {
return generation;
}
}
- Class Rule
class Rule {
char a;
String b;
Rule(char a_, String b_) {
a = a_;
b = b_;
}
char getA() {
return a;
}
String getB() {
return b;
}
}
- Class Turtle
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class Turtle {
String todo;
float len;
float theta;
Turtle(String s, float l, float t) {
todo = s;
len = l;
theta = t;
}
void render() {
stroke(0, 255);
strokeWeight(2);
for (int i = 0; i < todo.length(); i++) {
char c = todo.charAt(i);
if (c == 'F' || c == 'T' || c == 'H' || c == '"') {
line(0, 0, len, 0);
translate(len, 0);
} else if (c == '+') {
rotate(theta);
} else if (c == '-') {
rotate(-theta);
} else if (c == '[') {
pushMatrix();
} else if (c == ']') {
popMatrix();
}
}
}
void setLen(float l) {
len = l;
}
void changeLen(float percent) {
len *= percent;
}
void setToDo(String s) {
todo = s;
}
}
And it gives me the image which is on the left.
But I need to be able to generate something like these corals on the rigth