Created
October 30, 2024 22:50
-
-
Save Tolga-dev/265478237cf9dca96744c1998b0e8966 to your computer and use it in GitHub Desktop.
LSystemGenerator
This file contains hidden or 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
| // free to use | |
| // https://en.wikipedia.org/wiki/L-system#Example_1:_algae | |
| // Algae generator | |
| using System; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| namespace WorldGenerators | |
| { | |
| public class LSystemGenerator : MonoBehaviour | |
| { | |
| private void Start() | |
| { | |
| string axiom = "A"; | |
| var rules = new Dictionary<char, string> | |
| { | |
| { 'A', "AB" }, | |
| { 'B', "A" } | |
| }; | |
| int iterations = 7; | |
| string result = GenerateLSystem(axiom, rules, iterations); | |
| Debug.Log($"After {iterations} iterations: {result}"); | |
| } | |
| public string GenerateLSystem(string axiom, Dictionary<char, string> rules, int iterations) | |
| { | |
| string currentString = axiom; | |
| for (int i = 0; i < iterations; i++) | |
| { | |
| string nextString = ""; | |
| foreach (char c in currentString) | |
| { | |
| nextString += rules.TryGetValue(c, out var rule) ? rule : c.ToString(); | |
| } | |
| currentString = nextString; | |
| } | |
| return currentString; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment