# Thread: Fractal Tree

1. Member Join Date
Aug 2010
Posts
7
Rep Power
0

## 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 ?

Java 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 = m.getXPos();
startPos = m.getYPos();
startPos = (int) m.getHeading();
}
else if(command.equals("]"))
{
m.penUp();
m.moveTo(startPos, startPos);
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 = m.getXPos();
startPos = m.getYPos();
startPos = (int) m.getHeading();
}
else if(c == ']')
{
m.penUp();
m.moveTo(startPos, startPos);
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!");
}
}```
Last edited by ArcherSam; 08-12-2010 at 06:11 PM.  Reply With Quote

2. ## Can you add code tags to your code?

I can't get it right
Can you describe what the problem is in more detail?  Reply With Quote

3. Senior Member Join Date
Nov 2009
Posts
236
Rep Power
10

## 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"  Reply With Quote

4. ## 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.  Reply With Quote

5. Member Join Date
Aug 2010
Posts
7
Rep Power
0

## 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>  Reply With Quote

6. ## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•