# Problem with drawing a Pythagoras Tree using L-system

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.

``````int SZ = 800;
Tree tree;

void settings() {
size(SZ,SZ);
}

void setup() {
int d = 100;
int x = SZ/2;
int y = SZ/2;
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();
}
``````
``````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:
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;
}

}
``````

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.

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.

Seems to be a complicated way to draw a pythogorus tree, when compared with Pythagoras tree - Rosetta Code. 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?