Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created October 6, 2020 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCuttlefish/d317069944f944a3937b1908e9e3c611 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/d317069944f944a3937b1908e9e3c611 to your computer and use it in GitHub Desktop.
Lsystem example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LSystem : MonoBehaviour
{
private string axiom = "AA";
private string rule1 = "B"; // A -> B
private string rule2 = "AB"; // B -> AB
private int index; // character index
private string newGen; //new generation
public void Start()
{
Iterate(4); // run 4 iterations
}
private void Iterate(int genNum)
{
while (genNum > 0) // allow number of iterations
{
while (axiom.Length > index) // character length must be bigger than index value
{
if (axiom[index].ToString() == "A") newGen += rule1; //apply rule 1
if (axiom[index].ToString() == "B") newGen += rule2; //apply rule 2
index++; // increase index value
}
axiom = newGen; //store the new generation in the axiom string
newGen = null; //reset new generation string
index = 0; //reset index value
genNum--; //decrease the number of available generations
print(axiom); //output a generation
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment