# L system rules for drawing corals

**URL:** https://discourse.processing.org/t/l-system-rules-for-drawing-corals/12733
**Category:** Coding Questions
**Created:** [July 15, 2019, 10:26pm UTC](https://discourse.processing.org/t/l-system-rules-for-drawing-corals/12733 "2019-07-15T22:26:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MustafaMerza](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@MustafaMerza](https://discourse.processing.org/u/MustafaMerza)
#### Post date: [July 15, 2019, 10:26pm UTC](https://discourse.processing.org/t/l-system-rules-for-drawing-corals/12733/1 "2019-07-15T22:26:47Z")

</div>

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

```auto

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

```auto
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

```auto
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

```auto
// 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

![18](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eea520623b2642d628d96c1b8f2e8d4fe6cf2c55.jpeg)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 15, 2019, 10:44pm UTC](https://discourse.processing.org/t/l-system-rules-for-drawing-corals/12733/2 "2019-07-15T22:44:25Z")

</div>

Can you show your code please?

What is your problem?

What does the code do now and what do you want it to do instead?

---

<div class="post-metadata">

### Author: ![MustafaMerza](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@MustafaMerza](https://discourse.processing.org/u/MustafaMerza)
#### Post date: [July 15, 2019, 11:14pm UTC](https://discourse.processing.org/t/l-system-rules-for-drawing-corals/12733/3 "2019-07-15T23:14:40Z")

</div>

Okay sorry for the first post, I’ve updated my post to provide more explanation.

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [July 16, 2019, 6:32am UTC](https://discourse.processing.org/t/l-system-rules-for-drawing-corals/12733/4 "2019-07-16T06:32:47Z")

</div>

I don’t know whether it is any use to you, but I wrote a library for generating [LSystems on processing](https://github.com/monkstone/LSystems). I explore with examples the various types of LSystems that can be produced vis:-

1. Context sensitive
2. Stochastic
3. Simple grammar

What I do recommend is this reference [http://algorithmicbotany.org/papers/#abop](http://algorithmicbotany.org/papers/#abop), and by the way good luck.
