public static string LLToString(LinkedListA <string> linkedList) { string result = ""; for (LinkedListNodeA <string> c = linkedList.first; c != null; c = c.next) { result += c.item; } return(result); }
public static LinkedListA <string> StringToLL(string inputString) { LinkedListA <string> result = new LinkedListA <string>(); for (int i = 0; i < inputString.Length; i++) { result.Enqueue(inputString[i].ToString()); } return(result); }
public void RenderSymbols(LinkedListA <string> ll) { CountRatios(ll); LinkedListNodeA <string> currentNode; for (currentNode = ll.first; currentNode != null; currentNode = currentNode.next) { if (grammar.ContainsKey(currentNode.item)) { grammar[currentNode.item](); } } }
public LinkedListA <string> DoIterations(int n) { LinkedListA <string> ll = StringToLL(axiom); for (int i = 0; i < n; i++) { for (LinkedListNodeA <string> c = ll.first; c != null; c = c.next) { replaceNode(ll, c); } } iterations = n; return(ll); }
// Use this for initialization void Awake() { seed = System.DateTime.Now.Millisecond; Random.InitState(seed); Dictionary <string, List <Rule> > grammar = new Dictionary <string, List <Rule> >(); grammar["T"] = new List <Rule>(); grammar["T"].Add(new Rule(1.0f, "A[#T]T")); grammar["A"] = new List <Rule>(); grammar["A"].Add(new Rule(0.6f, "AA")); grammar["A"].Add(new Rule(0.4f, "A")); LSystem LS = new LSystem("T", grammar); ll = LS.DoIterations(UnityEngine.Random.Range(3, 7)); DoTurtle(0); }
// repalce node with linkedlistA public void Replace(LinkedListNodeA <T> node, LinkedListA <T> linkedList) { if (linkedList.IsEmpty()) { return; } if (node.previous == null) { first = linkedList.first; } else { Link(node.previous, linkedList.first); } if (node.next != null) { Link(linkedList.last, node.next); } }
private void replaceNode(LinkedListA <string> ll, LinkedListNodeA <string> node) { if (Regex.IsMatch(node.item, @"^[a-zA-Z]+$")) { if (grammar.ContainsKey(node.item)) { List <Rule> rules = grammar[node.item]; float totalProbability = 0; // roll dice against each rule for (var i = 0; i < rules.Count; i++) { totalProbability += rules[i].probability; if (totalProbability >= UnityEngine.Random.value) { // apply rule ll.Replace(node, StringToLL(rules[i].str)); break; } } } } }
private void CountRatios(LinkedListA <string> ll) { List <List <int> > lengths = new List <List <int> >(); int i = 0; lengths.Add(new List <int>()); lengths[i].Add(0); LinkedListNodeA <string> currentNode; for (currentNode = ll.first; currentNode != null; currentNode = currentNode.next) { if (currentNode.item == "A" || currentNode.item == "B") { lengths[i][lengths[i].Count - 1]++; } else if (currentNode.item == "[") { i++; if (i >= lengths.Count) { lengths.Add(new List <int>()); } lengths[i].Add(0); } else if (currentNode.item == "]") { i--; } } ratios = new List <float>(); List <float> bases = new List <float>(); List <float> counts = new List <float>(); List <int> islands = new List <int>(); bases.Add(0); counts.Add(0); islands.Add(0); for (currentNode = ll.first; currentNode != null; currentNode = currentNode.next) { if (currentNode.item == "A" || currentNode.item == "B") { ratios.Add(counts[i] += (1 - bases[i]) / lengths[i][islands[i]]); } else if (currentNode.item == "[") { i++; if (i >= bases.Count) { islands.Add(0); bases.Add(counts[i - 1]); counts.Add(counts[i - 1]); } else { islands[i]++; bases[i] = counts[i - 1]; counts[i] = counts[i - 1]; } } else if (currentNode.item == "]") { i--; } } heightInStems = lengths[0][0]; branchLength = height / heightInStems; return; }