//L-System iterator class static class LSystem { static char [] alphabet = { //Various instructions 'F', '+', '-', '[', ']' }; static String axiom; //Initial argument static String [] rule = { //Rule elements are swapped with detected alphabet elements "F", "+", "-", "[", "]" }; static String tree; //Storage for eventual L-System LSystem(String axiom, String ruleset){ rule[0] = ruleset; this.axiom = axiom; tree = ""; } //Replace detected alphabet elements with rule elements void iterate(int maxLength){ tree = new String(axiom); int [] ruleLength = new int[alphabet.length]; for (int i = 0; i < alphabet.length; i++){ ruleLength[i] = rule[i].length(); } for (int i = 0; i < maxLength; i++){ int newLength = 0; for (int j = 0; j < tree.length(); j++){ char c = tree.charAt(j); for (int k = 0; k < alphabet.length; k++){ if (c == alphabet[k]){ newLength += ruleLength[k]; break; } } } StringBuffer newTree = new StringBuffer(newLength); for (int j = 0; j < tree.length(); j++){ char c = tree.charAt(j); for (int k = 0; k < alphabet.length; k++){ if (c == alphabet[k]){ newTree.append(rule[k]); break; } } } tree = newTree.toString(); } } }