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";
command = in.next();
in.nextLine();

if(command.length() == 1)
else
}
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();
}
else if(command.equals("]"))
{
m.penUp();
m.moveTo(startPos[0], startPos[1]);
m.penDown();
}
else if(command.equals("-"))
m.turn(-30);
else if(command.equals("+"))
m.turn(30);
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();
}
else if(c == ']')
{
m.penUp();
m.moveTo(startPos[0], startPos[1]);
m.penDown();
}
else if(c == '-')
m.turn(-30);
else if(c == '+')
m.turn(30);
}//end of loop
}
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 07:11 PM.

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

3. Senior Member
Join Date
Nov 2009
Posts
235
Rep Power
5
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"

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.

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>

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.

#### Posting Permissions

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