Created
February 27, 2013 20:54
-
-
Save problem/5051625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import processing.pdf.*; | |
| boolean savePDF = false; | |
| import java.util.Stack; // I use java Stack objects | |
| // written by Jeff - www.fiz-ix.com | |
| String wseq = "0"; // the starting string of the L-System | |
| Stack xSave = new Stack(); // a Stack to store x coordinates of plant | |
| Stack ySave = new Stack(); // a Stack to store y coordinates of plant | |
| Stack aSave = new Stack(); // a Stack to store angles of branches | |
| int height = 600; // height of output | |
| int width = 400; // width of output | |
| int zz = 0; // interation of L-System | |
| void setup() { | |
| size(width,height); | |
| background(255,255,255); // blue | |
| frameRate(1); | |
| } | |
| void draw() { | |
| if ( savePDF ) { | |
| beginRecord( PDF, "pdf/myartwork-####.pdf" ); | |
| } | |
| zz++; | |
| if (zz > 7) { // start growing from scratch again | |
| zz = 1; | |
| wseq = "0"; | |
| background(255,255,255); | |
| } | |
| int treeLength = height/15; | |
| int xStart = width/2; | |
| int yStart = 0; | |
| background(255,255,255); | |
| drawPlant(zz, treeLength, xStart, yStart); | |
| iteratePlant(); | |
| delay(100); | |
| } | |
| void iteratePlant() { // a function to generate successive | |
| // iterations of the L-System | |
| String wseq0 = wseq; | |
| wseq = ""; | |
| for (int ii=0;ii < wseq0.length();ii++) { | |
| String cchar = wseq0.substring(ii,ii+1); | |
| if (cchar.equals("0")) { // replace 0 with following string | |
| wseq = wseq + "1MLL0RP0RP1LP10RM0"; | |
| } | |
| else if (cchar.equals("L")) { // keep L | |
| wseq = wseq + 'L'; | |
| } | |
| else if (cchar.equals("R")) { // keep R | |
| wseq = wseq + "R"; | |
| } | |
| else if (cchar.equals("P")) { // keep P | |
| wseq = wseq + "P"; | |
| } | |
| else if (cchar.equals("M")) { // keep M | |
| wseq = wseq + "M"; | |
| } | |
| else if (cchar.equals("1")) { // replace 1 with 11 | |
| wseq = wseq + "11"; | |
| } | |
| } | |
| } | |
| void drawPlant(int iteration, int treeLength, int xStart, int yStart) { | |
| int lineLength = treeLength/iteration; | |
| int nBNow = 0; // keep track of the number of branches in the plant | |
| // initial parameters | |
| color lc = color(255, 204, 0); // leaf color is gold | |
| color bc = color(153, 153, 153); // branch color is grey | |
| int xNow = xStart; | |
| int yNow = yStart; | |
| double aNow = 1.57079632679; // 90 degrees | |
| double aDelta = 0.436332313; // 25 degrees | |
| for (int ii=0;ii<wseq.length();ii++) { | |
| String cchar = wseq.substring(ii,ii+1); | |
| // the following are the rules for the "turtle" to draw the | |
| // L-System | |
| if (cchar.equals("0")) { | |
| // do nothing | |
| } | |
| else if (cchar.equals("1")) { // draw a line segment | |
| int xStop = xNow+(int)(lineLength*Math.cos(aNow)); | |
| int yStop = yNow+(int)(lineLength*Math.sin(aNow)); | |
| fill(bc); | |
| stroke(bc); | |
| strokeWeight(0.15*(zz*3-nBNow)); | |
| line(xNow,height-yNow,xStop,height-yStop); | |
| xNow = xStop; | |
| yNow = yStop; | |
| } | |
| else if (cchar.equals("M")) { // change direction by aDelta | |
| aNow = aNow + aDelta; | |
| } | |
| else if (cchar.equals("P")) { // change direction by -aDelta | |
| aNow = aNow - aDelta; | |
| } | |
| else if (cchar.equals("L")) { // push the current x and y | |
| // coordinates and angle to the Stacks | |
| xSave.push(new Integer(xNow)); | |
| ySave.push(new Integer(yNow)); | |
| aSave.push(new Double(aNow)); | |
| nBNow++; // one more branch | |
| } | |
| else if (cchar.equals("R")) { // draw a leaf and recover last | |
| // items from the Stacks | |
| fill(lc); | |
| stroke(lc); | |
| strokeWeight(1); | |
| ellipse(xNow,height-yNow,3+iteration,3+iteration); | |
| // draw a leaf | |
| xNow = (Integer)xSave.pop(); // recover last saved x coordinate | |
| yNow = (Integer)ySave.pop(); // recover last saved y coordinate | |
| aNow = (Double)aSave.pop(); // recover last saved angle | |
| nBNow--; | |
| } | |
| } | |
| if ( savePDF ) { | |
| endRecord(); | |
| savePDF = false; | |
| } | |
| } | |
| void keyPressed() | |
| { | |
| if ( key == 's' ) { | |
| savePDF = true; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment