Skip to content

Instantly share code, notes, and snippets.

@Tolga-dev
Created October 30, 2024 22:50
Show Gist options
  • Select an option

  • Save Tolga-dev/265478237cf9dca96744c1998b0e8966 to your computer and use it in GitHub Desktop.

Select an option

Save Tolga-dev/265478237cf9dca96744c1998b0e8966 to your computer and use it in GitHub Desktop.
LSystemGenerator
// 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