Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By sunde887

Thread: Sierpinski Triangles. Have a few questions.

  1. #1
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Sierpinski Triangles. Have a few questions.

    So I was working on this program call Sierpinski Triangles. The program is working but doesn't seems to be right.
    When I run the program "java Sierpinski Triangles 4" I got one big triangle as the output. Am I doing something wrong here?




    Java Code:
    // SierpinskiTriangles.java
    
    public class SierpinskiTriangles
    {
        // Recursively draws Sierpinski triangles. The argument n specifies the 
        // recursion depth. The rest of the arguments are the (x, y) coordinates 
        // of the vertices of an equilateral triangle; a being the bottom left vertex, 
        // b the bottom right vertex, and c the top vertex.
        private static void draw(int n, double ax, double ay, double bx, double by, 
                                 double cx, double cy)
        {
            // TBD
          double x = 0.0;
          double y = 0.0;
          
          double[] xs = {ax , bx , cx};
          double[] ys = {ay , by , cy};
          
          for(int i =0; i < n; i++);
          {
            int k = StdRandom.uniform(1);
            if(n ==0)
            {
              double x1 = (xs[k] * x) + (xs[k] * y) + (xs[k]);
              double y1 = (ys[k] * x) + (ys[k] * y) + (ys[k]);                    
            }
          }
          StdDraw.polygon(xs,ys);
        }
    
        // Entry point.
        public static void main(String[] args)
        {
            int n = Integer.parseInt(args[0]); // Recursion depth.
            StdDraw.setXscale(0.0, 1.0);
            StdDraw.setYscale(0.0, 1.0);
            StdDraw.show(0);
            draw(n, 0.0, 0.0, 1.0, 0.0, 0.5, 0.866);
            StdDraw.show(0);
        }
    }
    Last edited by Fubarable; 03-30-2012 at 02:37 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Sierpinski Triangles. Have a few questions.

    Quote Originally Posted by xdrazkalnytex View Post
    So I was working on this program call Sierpinski Triangles. The program is working but doesn't seems to be right.
    When I run the program "java Sierpinski Triangles 4" I got one big triangle as the output. Am I doing something wrong here?


    Java Code:
    // SierpinskiTriangles.java
    
    public class SierpinskiTriangles
    {
        // ****Recursively****  draws Sierpinski triangles. The argument n specifies the 
        // recursion depth. The rest of the arguments are the (x, y) coordinates 
        // of the vertices of an equilateral triangle; a being the bottom left vertex, 
        // b the bottom right vertex, and c the top vertex.
        private static void draw(int n, double ax, double ay, double bx, double by, 
                                 double cx, double cy)
        {
            // TBD
          double x = 0.0;
          double y = 0.0;
          
          double[] xs = {ax , bx , cx};
          double[] ys = {ay , by , cy};
          
          for(int i =0; i < n; i++);
          {
            int k = StdRandom.uniform(1);
            if(n ==0)
            {
              double x1 = (xs[k] * x) + (xs[k] * y) + (xs[k]);
              double y1 = (ys[k] * x) + (ys[k] * y) + (ys[k]);                    
            }
          }
          StdDraw.polygon(xs,ys);
        }
    
        // Entry point.
        public static void main(String[] args)
        {
            int n = Integer.parseInt(args[0]); // Recursion depth.
            StdDraw.setXscale(0.0, 1.0);
            StdDraw.setYscale(0.0, 1.0);
            StdDraw.show(0);
            draw(n, 0.0, 0.0, 1.0, 0.0, 0.5, 0.866);
            StdDraw.show(0);
        }
    }
    Note the word highlighted by the **** **** -- your comments state that this is supposed to be an example of recursion. So where is your recursion?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Sierpinski Triangles. Have a few questions.

    First, I don't see any recursive calls. Also, in the loop in draw you create local variables in the loops body and never use them.

  4. #4
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: Sierpinski Triangles. Have a few questions.

    So does that mean I dont need a loop?
    I didn't know there was a recursive, when I first got the file for this program I just fill out the body.
    Also, recursive means the triangles supposed to draw 3 more triangles inside of the large one right?
    Can someone help me with the recursive calls since I have no idea how to get started.
    Thanks!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Sierpinski Triangles. Have a few questions.

    Using recursion is a general practice, which is common in mathematics, as well as computer science. Recursion is the process of defining a base case and giving a rule which uses previous terms. As a concrete example, here is a recursive definition of n! (n factorial)

    Java Code:
    public int factorial(int n){
      if(n == 0){
        return 1;
      }
      return n * factorial(n - 1);
    }
    With each iteration you perform a smaller sub task. Generally, loops aren't used in recursion. Perhaps for the recursive definition of Sierpinski, you may stop when n reaches a certain depth, and each depth will call on itself to draw three sub Sierpinski triangles inside of the initial one.
    Fubarable likes this.

  6. #6
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: Sierpinski Triangles. Have a few questions.

    how would I start my recursive for this program?
    the public int......can't be place at the beginning of the program.

Similar Threads

  1. Two triangles of stars next to each other
    By Aero in forum New To Java
    Replies: 3
    Last Post: 09-26-2011, 07:20 PM
  2. Recursive Sierpinski's Carpet
    By baf06 in forum New To Java
    Replies: 1
    Last Post: 04-16-2011, 03:26 PM
  3. Drawing out triangles
    By leiferouis in forum New To Java
    Replies: 24
    Last Post: 01-16-2009, 08:18 PM
  4. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 09:18 PM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Posting Permissions

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