# Problem with drawing a Pythagoras Tree using L-system

**URL:** <https://discourse.processing.org/t/problem-with-drawing-a-pythagoras-tree-using-l-system/28270>\
**Category:** Coding Questions\
**Created:** [March 5, 2021, 12:04am UTC](https://discourse.processing.org/t/problem-with-drawing-a-pythagoras-tree-using-l-system/28270 "2021-03-05T00:04:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esther](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@Esther](https://discourse.processing.org/u/Esther)\
**Post date:** [March 5, 2021, 12:04am UTC](https://discourse.processing.org/t/problem-with-drawing-a-pythagoras-tree-using-l-system/28270/1 "2021-03-05T00:04:55Z")

</div>

Hi, I am drawing a Pythagoras Tree using L-system and 2 production rules. I managed to use the first rule called “F-rule” to draw the right handside of the tree. I tried using the second rule “H-rule” to draw the left handside. NO matter what I change in the H-rule, it messes up the result of the right handside. Any idea how I can create the leftside similar to the right ? Many thanks.

```auto
int SZ = 800;  
Tree tree;
  
void settings() {
  size(SZ,SZ);  
}

void setup() {
  int d = 100;
  int x = SZ/2;
  int y = SZ/2;
  float branchAngle = radians(180);
  float branchAngle_Left = radians(-45); 
  float branchAngle_Right = radians(45); 
  float initOrientation = PI;  
  String state = "F"; // initiator        
  float scaleFactor = 0.8;      
  String F_rule = "F[+sH]s+F";      
  String H_rule = "";  
  String f_rule = "";           
  int numIterations = 6;
  
  background(255);
  noLoop();
  
 tree = new Tree(d, x, y, branchAngle, branchAngle_Right, branchAngle_Left, initOrientation, state, scaleFactor, F_rule, H_rule, f_rule, numIterations);  
}

void draw() {
  tree.draw();
  }

```

```auto
class Tree {

 
  int m_lineLength; // turtle line length
  int m_x; // initial x position
  int m_y; // initial y position
  float m_branchAngle_Right; // turtle rotation at branch
  float m_branchAngle_Left; // turtle rotation at branch
  float m_branchAngle;
  float m_initOrientation; // initial orientation
  String m_state; // initial state
  float m_scaleFactor; // branch scale factor
  String m_F_rule; // F-rule substitution
  String m_H_rule; // H-rule substitution
  String m_f_rule; // f-rule substitution
  int m_numIterations; // number of times to substitute
  
  // constructor
  // (d = line length, x & y = start position of drawing)
  Tree(int d, int x, int y, float branchAngle, float branchAngle_Right, float branchAngle_Left, float initOrientation, String state, float scaleFactor, 
  String F_rule, String H_rule, String f_rule, int numIterations) {
    m_lineLength = d;
    m_x = x;
    m_y = y; 
    m_branchAngle= branchAngle;
    m_branchAngle_Right = branchAngle_Right;
    m_branchAngle_Left = branchAngle_Left;
    m_initOrientation = initOrientation;
    m_state = state;
    m_scaleFactor = scaleFactor;
    m_F_rule = F_rule;
    m_H_rule = H_rule;
    m_f_rule = f_rule;
    m_numIterations = numIterations;

    // Perform L rounds of substitutions on the initial state
    for (int k=0; k < m_numIterations; k++) {
      m_state = substitute(m_state);
    }
  }
  
  void draw() {
    pushMatrix();
    pushStyle();
    
    stroke(0);
    translate(m_x, m_y); // initial position
    rotate(m_initOrientation); // initial rotation
    
    
    for (int i=0; i < m_state.length(); i++) {
      turtle(m_state.charAt(i));
    }
    
    popStyle();
    popMatrix();
  }
  
  
  void turtle(char c) {
    switch(c) {
    case 'F': // drop through to next case
    case 'H':
    //line (0, 0, 0, m_lineLength);
      rect(0, 0, m_lineLength, m_lineLength);
      translate(0, m_lineLength);
      break;
    case 'f':
      translate(0, m_lineLength);
      break;
      case 't':
      translate(0, -m_lineLength);
      break;
    case 's':
      scale(m_scaleFactor);
      break;
    case '&':
      rotate(m_branchAngle);
      break;
    case '-':
      rotate(m_branchAngle_Left);
      break;
    case '+':
      rotate(m_branchAngle_Right);
      break;
    case '[':
      pushMatrix();
      break;
    case ']':
      popMatrix();
      break;
    default:
      println("Bad character: " + c);
      exit();
    }
  }
  
  // apply substitution rules to string s and return the resulting string
  String substitute(String s) {
    String newState = new String();
    for (int j=0; j < s.length(); j++) {
      switch (s.charAt(j)) {
      case 'F':
        newState += m_F_rule;
        break;
      case 'H':
        newState += m_H_rule;
        break;
      case 'f':
        newState += m_f_rule;
        break;
      default:
        newState += s.charAt(j);
      }
    }
    return newState;
  }
  
}

```

---

<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:** [March 5, 2021, 8:25pm UTC](https://discourse.processing.org/t/problem-with-drawing-a-pythagoras-tree-using-l-system/28270/2 "2021-03-05T20:25:37Z")

</div>

Hi Esther, welcome to the processing forum, please could you format your code, it’s much easier to read that way, especially when there are lots of comments.

---

<div class="post-metadata">

**Author:** ![Esther](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@Esther](https://discourse.processing.org/u/Esther)\
**Post date:** [March 5, 2021, 9:14pm UTC](https://discourse.processing.org/t/problem-with-drawing-a-pythagoras-tree-using-l-system/28270/3 "2021-03-05T21:14:55Z")

</div>

Hi Martin,

Thank you for the comment. I am new to this forum, therefore, still navigating how to use all functions at the toolbar.

I have formatted the codes. Hope you could shed some light on my school assignment.

Many thanks.

---

<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:** [March 6, 2021, 10:53am UTC](https://discourse.processing.org/t/problem-with-drawing-a-pythagoras-tree-using-l-system/28270/4 "2021-03-06T10:53:42Z")

</div>

Seems to be a complicated way to draw a pythogorus tree, when compared with [Pythagoras tree - Rosetta Code](https://rosettacode.org/wiki/Pythagoras_tree#Processing). It would probably help to split problem up, your Tree class is overly complicated. I would split the lsystem generation from the drawing code, that way you can test that the rules are being generated correctly. It doesn’t seem as though you are not sure what the rules should be?  
In your turtle switch statement there is an `&` that does not appear in your lsystems grammar, it could be important?
