Skip to content

Instantly share code, notes, and snippets.

@Problematic
Last active March 21, 2018 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Problematic/204d00bd2db65a4a53983e9d3eab067a to your computer and use it in GitHub Desktop.
Save Problematic/204d00bd2db65a4a53983e9d3eab067a to your computer and use it in GitHub Desktop.
L-System Generator
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public interface ILSystemGenerator {
string Generate (string axiom, int depth);
void Clear();
}
public interface ILSystemGenerator<TSuccessor> : ILSystemGenerator, IEnumerable<KeyValuePair<char, TSuccessor>> {
void Add (char predecessor, TSuccessor successor);
TSuccessor this [char successor] { get; set; }
}
public class StochasticLSystemGenerator : ILSystemGenerator<Dictionary<string, double>> {
protected Dictionary<char, Dictionary<string, double>> Productions { get; set; }
public Dictionary<string, double> this [char successor] {
get {
return Productions[successor];
}
set {
Productions[successor] = value;
}
}
protected Random rng;
public StochasticLSystemGenerator (Random rng) {
Productions = new Dictionary<char, Dictionary<string, double>>();
this.rng = rng;
}
public void Add (char predecessor, Dictionary<string, double> successors) {
Productions.Add(predecessor, successors);
}
public string Generate (string axiom, int depth) {
string result = string.Empty;
foreach (var c in axiom) {
if (Productions.ContainsKey(c) && depth > 0) {
result = string.Concat(result, Generate(RandUtils.SampleWeighted(Productions[c], rng), depth - 1));
} else {
result = string.Concat(result, c);
}
}
return result;
}
public void Clear () {
Productions.Clear();
}
public IEnumerator<KeyValuePair<char, Dictionary<string, double>>> GetEnumerator()
{
return Productions.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class LSystemGenerator : ILSystemGenerator<string> {
protected Dictionary<char, string> Productions { get; set; }
public string this [char successor] {
get {
return Productions[successor];
}
set {
Productions[successor] = value;
}
}
public LSystemGenerator () {
Productions = new Dictionary<char, string>();
}
public string Generate (string axiom, int depth) {
string result = string.Empty;
foreach (var c in axiom) {
if (Productions.ContainsKey(c) && depth > 0) {
result = string.Concat(result, Generate(Productions[c], depth - 1));
} else {
result = string.Concat(result, c);
}
}
return result;
}
public void Add (char predecessor, string successor) {
Productions.Add(predecessor, successor);
}
public void Clear () {
Productions.Clear();
}
public IEnumerator<KeyValuePair<char, string>> GetEnumerator()
{
return Productions.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
using System;
using System.Collections.Generic;
public class Program
{
static void Main(string[] args)
{
var generator = new LSystemGenerator {
['A'] = "AB",
['B'] = "A",
};
Console.WriteLine(generator.Generate("A", 4));
// => ABAABABA
generator.Clear();
generator.Add('1', "11");
generator.Add('0', "1[0]0");
Console.WriteLine(generator.Generate("0", 3));
// => 1111[11[1[0]0]1[0]0]11[1[0]0]1[0]0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment