float turtle_x, turtle_y, turtle_angle; float pen_x, pen_y; float turtle_scalar; int place_on_stack; float[] stack_x; float[] stack_y; //component of branching stack float[] stack_angle; char[] l_string; String l_str; String l_list[]; String lexicon[]; String dictionary; String gen_string[]; float curr_degree; float counter; boolean re_draw = false; boolean animate = false; float gravity = .5; float seek_x,seek_y; float seek_amount; void setup() { stroke(255,255,255); background(0,0,0); gen_string = new String[50]; l_list = new String[50]; dictionary = "F - +"; lexicon = splitStrings(dictionary); seek_x = 100; seek_y = 100; seek_amount = 0; float piece; int j; for (j = 0; j < 50; j++) { piece = random(3); // l_list[j] = lexicon[(int)piece]; } //This is the string to alter to change the shape of the L-System l_str = "[ F [ + F F F [ - - F - F + F - F + F ] [ - - F - F + F - F + F [ + + F - F + F - F + F ] [ - - F - F + F - F + F ] ] F - F - F - F - F - F - F ]"; // l_str = join(gen_string); l_list = splitStrings(l_str); size(500,500); turtle_scalar = 15; turtle_angle = 180; float rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; pen_x = width/2; pen_y = height/2; curr_degree = 0; place_on_stack = 0; stack_x = new float[100]; stack_y = new float[100]; stack_angle = new float[100]; DrawLSystem(); } void loop() { seek_x = mouseX; seek_y = mouseY; checkStuff(); if (animate == true) { counter += .01; float rad = radians(curr_degree); rad = sin(counter) * 10; curr_degree = degrees(rad); } if (counter > (3.141592653 * 2)) { counter = 0; } turtle_scalar = 15; turtle_x = 0; turtle_y = turtle_scalar; turtle_angle = 90; place_on_stack = 0; pen_x = width/2; pen_y = height/2; DrawLSystem(); color white = color(255, 255, 255); set ((int)seek_x,(int)seek_y,white); } void DrawLSystem() { int l_length = l_list.length; int i; float rad; if (re_draw == true) { background(0,0,0); } for (i = 0; i < (l_length - 1); i++) { if (l_list[i].equals("F")) { float new_pen_x, new_pen_y; new_pen_x = pen_x + turtle_x; new_pen_y = pen_y + turtle_y - gravity; if (new_pen_x < seek_x) { new_pen_x += seek_amount; } else { new_pen_x -= seek_amount; } if (new_pen_y < seek_y) { new_pen_y += seek_amount; } else { new_pen_y -= seek_amount; } //normalize float xDiff, yDiff; xDiff = new_pen_x - pen_x; yDiff = new_pen_y - pen_y; float new_length = sqrt((xDiff * xDiff) + (yDiff * yDiff)); xDiff = xDiff/new_length; yDiff = yDiff/new_length; xDiff *= turtle_scalar; yDiff *= turtle_scalar; new_pen_x = pen_x + xDiff; new_pen_y = pen_y + yDiff; line(pen_x, pen_y, new_pen_x,new_pen_y); pen_x = new_pen_x; pen_y = new_pen_y; } if (l_list[i].equals("+")) { turtle_angle += curr_degree; rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; } if (l_list[i].equals("-")) { turtle_angle -= curr_degree; rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; } if (l_list[i].equals("[")) //push { stack_x[place_on_stack] = pen_x; stack_y[place_on_stack] = pen_y; stack_angle[place_on_stack] = turtle_angle; place_on_stack += 1; } if (l_list[i].equals("]")) //pop { place_on_stack -= 1; pen_x = stack_x[place_on_stack]; pen_y = stack_y[place_on_stack]; turtle_angle = stack_angle[place_on_stack]; rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; } } } /*void DrawLSystem() { int l_length = l_list.length; int i; float rad; for (i = 0; i < (l_length - 1); i++) { if (l_list[i].equals("F")) { line(pen_x,pen_y, pen_x + turtle_x, pen_y + turtle_y); pen_x += turtle_x; pen_y += turtle_y; } if (l_list[i].equals("+")) { turtle_angle += 15; rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; } if (l_list[i].equals("-")) { turtle_angle -= 15; rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; } if (l_list[i].equals("[")) //push { stack_x[place_on_stack] = pen_x; stack_y[place_on_stack] = pen_y; stack_angle[place_on_stack] = turtle_angle; place_on_stack += 1; } if (l_list[i].equals("]")) //pop { place_on_stack -= 1; pen_x = stack_x[place_on_stack]; pen_y = stack_y[place_on_stack]; turtle_angle = stack_angle[place_on_stack]; rad = radians(turtle_angle); turtle_x = cos(rad) * turtle_scalar; turtle_y = sin(rad) * turtle_scalar; } } }*/ void mouseReleased() { animate = !animate; } void checkStuff() { if(keyPressed) { if (key == 'd' || key == 'D') { re_draw = !re_draw; } if (key == 'a' || key == 'A') { gravity -= 1; } if (key == 's' || key == 'S') { gravity += 1; //if (gravity > 0) gravity = 0; } if (key == 'z' || key == 'Z') { curr_degree -= 1; } if (key == 'x' || key == 'X') { curr_degree += 1; } if (key == 'e' || key == 'E') { seek_amount -= .1; if (seek_amount < 0) seek_amount = 0; } if (key == 'r' || key == 'R') { seek_amount += .1; } } }