# Fractal Tree

• 08-12-2010, 05:28 PM
ArcherSam
Fractal Tree
Hey, i'm trying to create a fractal tree and when applying the rule through iterations, I get a pattern similar to train tracks rather than a fractal tree. What should the rule be in the second step?

IDE: BlueJ
userlib: bookClasses <http://learn.flvs.net/webdav/educator_cprogram1_v9/module06/mod_labscript/06_02/bookClasses.zip>
Tutorial: <http://www.jcu.edu/math/vignettes/lsystems.htm>

Axiom: F
Angle: 30
Rule: F->F[-FF]F[+FF]F

Step String
0 F
1 F[-FF]F[+FF]F
2 ?

Code:

```/**  * This purpose of this program is to draw fractals using rules.  * @author Sam W.  * @version 08/10/08  */ import java.awt.*; import java.util.Scanner; class FractalEngine {     public void instructions(Scanner in)     {       System.out.println("Welcome to TurtleSketch");       System.out.println("A Freeware Drawing Tool");       System.out.println("You can also enter strings!");       System.out.println("=======================");       System.out.println("Press F to go forward.");       System.out.println("Press b to go backward.");       System.out.println("Press [ to begin a branch.");       System.out.println("Press ] to end a branch.");       System.out.println("Press + to turn right.");       System.out.println("Press - to turn left.");       System.out.println("Press Q to quit.");       System.out.println();       System.out.print("Press any key to continue.");       in.next();       in.nextLine();       System.out.println();     }//end of instructions method     public void drawFractal(Turtle m, Scanner in, int lineLength)     {         m.turnRight();         String command = "";         int xPos = 0;         int yPos = 0;         int zPos = 0;         int[] startPos = {xPos, yPos, zPos};         do         {             command = "a";             System.out.print("Your command? " );             command = in.next();             in.nextLine();                         if(command.length() == 1)                 readChar(m, in, lineLength, command, startPos);             else                 readString(m, in, lineLength, command, startPos);         }         while(!command.equalsIgnoreCase("Q"));//end of loop     }//end of drawFractal method     public void readChar(Turtle m, Scanner in, int lineLength, String command, int[] startPos)     {         if(command.equalsIgnoreCase("F"))             m.forward(lineLength);         else if(command.equals("b"))             m.backward(lineLength);         else if(command.equals("["))         {                startPos[0] = m.getXPos();             startPos[1] = m.getYPos();             startPos[2] = (int) m.getHeading();         }         else if(command.equals("]"))         {             m.penUp();             m.moveTo(startPos[0], startPos[1]);             m.setHeading(startPos[2]);             m.penDown();         }         else if(command.equals("-"))             m.turn(-30);         else if(command.equals("+"))             m.turn(30);     }// end of readChar method     public void readString(Turtle m, Scanner in, int lineLength, String command, int[] startPos)     {                  for (int i = 0; i < command.length(); i++)         {             char c = command.charAt(i);                    //Process char             if(c == 'F')                 m.forward(lineLength);             else if(c == 'b')                 m.backward(lineLength);             else if(c == '[')             {                 startPos[0] = m.getXPos();                 startPos[1] = m.getYPos();                 startPos[2] = (int) m.getHeading();             }             else if(c == ']')             {                 m.penUp();                 m.moveTo(startPos[0], startPos[1]);                 m.setHeading(startPos[2]);                 m.penDown();             }             else if(c == '-')                 m.turn(-30);             else if(c == '+')                 m.turn(30);         }//end of loop     }//end of readString method } public class FractalEngineTester {     public static void main(String[] args)     {       World worldObj = new World();       Turtle m = new Turtle(200, 200, worldObj);       Scanner in = new Scanner(System.in);       FractalEngine fe = new FractalEngine();             int lineLength = 30;                fe.instructions(in);       fe.drawFractal(m, in, lineLength);             System.out.println("Thank you for using Fractal Enginge!");     } }```
• 08-12-2010, 05:40 PM
Norm

Quote:

I can't get it right
Can you describe what the problem is in more detail?
• 08-13-2010, 11:26 PM
collin389
So the rule would be to replace any instance of 'F' with "F[-FF]F[+FF]F"
so 2 would be: "F[-FF]F[+FF]F[-F[-FF]F[+FF]FF[-FF]F[+FF]F]F[-FF]F[+FF]F[+F[-FF]F[+FF]FF[-FF]F[+FF]F]F[-FF]F[+FF]F"
• 08-13-2010, 11:39 PM
Norm
Is that replacement being done in a String? You want to replace one string with another.
Have you looked at the String class methods? There are several that might work.
• 08-13-2010, 11:44 PM
ArcherSam
Yeah, I tried that early today and it didn't work. When you look at the code 'F[-FF]F[+FF]F[-F[' the italics open-bracket resest the drawing object back to the last position of the child branch, instead of the parent branch. For instance, whenever I had a open-bracket inside of a open-bracket the, drawing shifts from the parent branch.

[-FF] made a correct branch, but [[-FF] wouldn't make a correct branch, the drawing object would start back at the second saved location instead of the first.

Sorry if this all seems confusing.

<http://rosettacode.org/wiki/Fractal_tree#Java>
• 08-14-2010, 01:41 AM
Norm
Sorry, I was responding to your stating the rule that any 'F' be replaced by "F[-FF]F[+FF]F".
Obviously the replacement rules are not that simple.

You'll have to find the right set of rules to use.