Beginner here. I need help understanding how i introduce parameters into an lsystem. I know how to code it when its only strings and characters with a ruleset like
‘X’, “[FX[-FX]+FX]”
‘F’, “F”
But when its parameters i get confused. I understand the formula and all but have no clue how to plot it into C#.
Like, id : pred : cond → succ
With an example.
Axiom : B(2)A(4, 4)
p1 : A(x, y) : y < 3 → A(x * 2; x + y)
p2 : A(x, y) : y >= 3 → B(x)A(x/y, 0)
p3 : B(x) : x < 1 → C
p4 : B(x) : x >= 1 → B(x - 1)
private void Generate()
{
currentString = axiom;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < iterations; i++)
{
foreach (char c in currentString)
{
sb.Append(rules.ContainsKey(c) ? rules[c] : c.ToString());
}
currentString = sb.ToString();
sb = new StringBuilder();
}
foreach (char c in currentString)
{
switch (c)
{
case 'F':
Vector3 initialPosition = transform.position;
transform.Translate(Vector3.up * length);
treeSegment = Instantiate(Branch);
treeSegment.GetComponent<LineRenderer>().SetPosition(0, initialPosition);
treeSegment.GetComponent<LineRenderer>().SetPosition(1, transform.position);
treeSegment.transform.parent = gameObject.transform;
break;
case 'X':
break;
case '+':
transform.Rotate(Vector3.back * angle);
break;
case '-':
transform.Rotate(Vector3.forward * angle);
break;
case '[':
transformStack.Push(new TransformInfo()
{
position = transform.position,
rotation = transform.rotation
});
break;
case ']':
TransformInfo ti = transformStack.Pop();
transform.position = ti.position;
transform.rotation = ti.rotation;
break;
default:
throw new InvalidOperationException("Invalid L-Tree operation");
}
}
}
I would normaly use this to read the string for every character and then draw the lines. But how do you get the numbers from the string and read it like ‘‘B(2)’’ and not just ‘‘B’’,‘’(‘’,‘‘2’’ and so on?