Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Fractal Triangle, Recursion to make children triangles

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fractal Triangle, Recursion to make children triangles

    I have a FractalGUI and a FractalTriangle class. The FractalTriangle class is what does a lot of the work, it uses ATriangle(s) to make a triangle, and it uses recursion to draw three children triangles along each parent triangle. Fractal Triangle has a method "makeTriangle" private ATriangle makeTriangle( Point p0, Point p1 ) which takes the ATriangles and sets them the right size and everything, but for some reason I can only get one level of children triangles, and they are also already their upon running which they shouldn't be, they should appear when the depth is 2. When the depth is three it should add three triangles each onto the three added at depth two. At depth 4 it should add three each onto the three previously made triangles, and so on until depth reaches 6, the max depth. Here is some code of my FractalTriangle.

    p.s. The Atriangle class given to us ("starter code") does not extend or implement anything, and has basic methods (setLocation, setSize, setThickness, setColor, etc. )

         //---------------- class variables ------------------------------
        //---- recursive generation parameters
        public static  double   sizeRatio = 0.5;   // integer represent %
        public static  double   offset = 0.5;    // offset/100 = parametric value
        // child positioning offset
        public static  double   p2projection = 0.5;
        // parametric value of projection of
        // vertex p2 onto the base; can be < 0
        public static  boolean  outside = true;
        public static  boolean  fillTriangles = true;
        public static  int      maxDepth    = 6;
        public static  int      minHeight   = 7;  // min height of p2 for further
        //   subdivision.
        private static Color[]  colors = { Color.RED, Color.BLUE, Color.CYAN, 
        Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.BLACK };
     
        //---------------- instance variables ------------------------------
        public static ATriangle  _tri;
        private static ATriangle[] children;
        private int        _depth;
        private FractalTriangle _parent = null;
        private static FractalTriangle[] childTri;
     
        //---- triangle shape parameters  
        private double     _p2height;
     
        //------------------------ constructors ----------------------------
        /**
        * initial constructor. Needs an ATriangle object. It also gets 
        * passed the height of this triangle and the projection position of the
        * 3rd point onto the opposite line. These could be computed from the
        * ATriangle object, but it's pretty complicated to do and in the context
        * of this assignment, the caller got those parameters from the user,
        * so it's easiest to just pass them along.
        */
        public FractalTriangle( int depth, ATriangle tri, int height )
        {
        // set up values of instance variables
        _depth    = 1;
        _depth    = depth;
        _p2height = height;
        _parent = null;
        children = new ATriangle[3];
        childTri = new FractalTriangle[3];
     
     
        ////////////////////////////////////////////////
        // Set the triangle's color to a value that depends
        // on its depth.
        //
        // Generate the children triangles for this root.
        // Make sure your code for this is as clean and
        // modular as possible.
        /////////////////////////////////////////////////////
     
        _parent = this;
        _tri = tri;
     
        if ( _depth < maxDepth || _p2height >= minHeight )
        { 
          childTri[ 0 ] = new FractalTriangle( tri, this, depth );
          childTri[ 1 ] = new FractalTriangle( tri, this, depth );
          childTri[ 2 ] = new FractalTriangle( tri, this, depth );
        }
     
     
        }
        /**
        * recursive constructor; it's private, so can only be called from methods
        * of this class. It will get called by the public FractalTriangle.
        */
        private FractalTriangle ( ATriangle tri, FractalTriangle parent, int depth )
        {
     
        ///////////////////////////////////////////////////////////
        // get parent's height from parent parameter to compute this height
        //
        // save other parameters in instance variables
        //
        // recursively create children -- only if recursion depth limit hasn't
        // been reached and the minimum height test passes
        ///////////////////////////////////////////////////////////
        System.err.println( "depth before if: " + depth );
        _tri = tri;
        _depth = depth;
        _parent = this;
        _p2height = _tri.getHeight( );
     
        if ( _depth < maxDepth || _p2height >= minHeight)
        {
          System.err.println( "depth after if: " + depth );
          //_parent = this;
     
     
          Point[] pts = tri.getPoints();
     
     
          children[0] = makeTriangle( pts[0], pts[1] );
          children[1] = makeTriangle( pts[1], pts[2] );
          children[2] = makeTriangle( pts[2], pts[0] );
          System.err.println( "child1: " + children[0] ); 
     
          for( int i = 0; i < children.length; i++ )
          {
          childTri[ i ] = new FractalTriangle( children[ i ], _parent, depth );
          }
        }
        else
        {
          return;
        }
     
        }  
        //--------------------- display( Graphics2D ) -----------------------
        /**
        * method called by Java's repaint mechanism.
        */   
        public void display ( Graphics2D context ) 
        {
        //////////////////////////////////////////////////////
        // 1. Need to determine whether to draw the ATriangle's
        //    display method (which fills it) or its "draw" method
        //    which only does the outline.
        ///////////////////////////////////////////////////////
     
        _tri.draw( context );                               //draws outline on base triangle
     
        if( fillTriangles == true )
        {
          _tri.display( context );                       // fills base triangle
          for( int i = 0; i < children.length; i++ )
          {
            if( children == null )                       
            {
              System.err.println( "Children is null" );
              return;
            }
            children[i].display( context );              // fills children
          }
        }
     
        for( int i = 0; i < children.length; i++ )
          {
            if( children == null )                            
            {
              System.err.println( "Children is null" );
              return;
            }
            else
            {
            children[i].draw( context );                    //draws outline of children
            }
     
          }
        //////////////////////////////////////////////////////
        // 2. Need to recursively invoke the display methods of
        //    all children (if they exist)
        //
        //////////////////////////////////////////////////////
      }



    When I have the private constructor the way it is above, I get this

    tri 2.jpg


    and when I make the private constructor like this ( only difference is I've commented out the recursion step )

         if ( _depth < maxDepth || _p2height >= minHeight)
         {
          System.err.println( "depth after if: " + depth );
          //_parent = this;
     
     
          Point[] pts = tri.getPoints();
     
     
          children[0] = makeTriangle( pts[0], pts[1] );
          children[1] = makeTriangle( pts[1], pts[2] );
          children[2] = makeTriangle( pts[2], pts[0] );
          System.err.println( "child1: " + children[0] ); 
     
          for( int i = 0; i < children.length; i++ )
          {
          childTri[ i ] = new FractalTriangle( children[ i ], _parent, depth );
          }
         }
         else
         {
          return;
         }

    I get this

    tri 1.jpg

    the way it looks without the recursion looks much more right, but I am supposed to have recursion in that constructor but I can't figure out how to get it right with the recursion. It's so weird how with the recursion the big red triangle ( base triangle ) disappears, and you can see the children are really really small.

    I've gotten everything to work perfect, except making the children triangles. Please Helppp.
    Last edited by devingero95; October 7th, 2014 at 07:31 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fractal Triangle, Recursion to make children triangles

    Here's a video of what I'm trying to get it to act like,

    4P movie - YouTube

    I've gotten everything else to work, except adding the children.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Fractal Triangle, Recursion to make children triangles

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Oops, attitude could be a problem, but I'll give this another look when I have more time.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Fractal Triangle, Recursion to make children triangles

    You haven't given us enough information to understand the problem. Could you post a short, runnable example that demonstrates the problem?

    Can you also provide a reference for this:
     /**
        * method called by Java's repaint mechanism.
        */   
        public void display ( Graphics2D context )
    BTW - The use of leading underscores in variable names in Java is not an accepted practice, but it's fine if that's the way you're being taught or some personal convention that you consistently follow. What does it signify in your code?

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fractal Triangle, Recursion to make children triangles

    I didn't mean any attitude haha.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fractal Triangle, Recursion to make children triangles

    Also, I got it all figured out, my private and public constructors were both supposed to be the same, and that was basically it. yay 2.jpg

    yay.jpg

    Programs pretty fun.

Similar Threads

  1. How to make this triangle upside down in java?
    By namenamename in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 04:02 PM
  2. Please help! Nested while loops and asterisk triangles!
    By rockout341 in forum Loops & Control Statements
    Replies: 12
    Last Post: September 15th, 2011, 11:50 AM
  3. Triangles problem - creating triangle objects
    By sparky5783 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 15th, 2011, 12:38 PM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM