Results 1 to 6 of 6

Thread: Fractal Tree

  1. #1
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default 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[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!");
        }
    }
    Last edited by ArcherSam; 08-12-2010 at 06:11 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Can you add code tags to your code?

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

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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. #5
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  2. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 12:51 PM
  3. B tree
    By wide in forum New To Java
    Replies: 2
    Last Post: 06-30-2009, 11:37 PM
  4. Zooming in on fractal
    By Mr.Beans in forum Java 2D
    Replies: 1
    Last Post: 04-18-2009, 05:00 AM
  5. Need Help With Tree Adt
    By avi in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 03:11 AM

Posting Permissions

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