Created
October 6, 2020 02:50
-
-
Save TheCuttlefish/d317069944f944a3937b1908e9e3c611 to your computer and use it in GitHub Desktop.
Lsystem example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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