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 4 of 4

Thread: Sierpinski Triangle bug

  1. #1
    Junior Member
    Join Date
    Jun 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sierpinski Triangle bug

    I'm trying to make a Sierpinski Triangle using recursion and StdDraw, but I can't figure out how to make it work correctly. I know how to draw the triangles in the correct spots, but I can't make the recursion work in a way that it can draw the multiple triangles in one loop of the recursion. What I need to figure out is how to make that pattern in every subsequent triangle that is drawn as a result of the recursion.

    public class Triangle
    {
        public static void drawTri(double x, double y, double s) {
            if (s<5 ) { return; }
            else {
                double[] a = {x,x+s/2,x+s};
                double[] b = {y,y+s*Math.sqrt(3)/2,y};
                StdDraw.polygon(a,b);
                double[] c = {x,x+s/4,x+s/2};
                double[] d = {y,y+s*Math.sqrt(3)/4,y};
                double[] e = {x+s/2,x+3*s/4,x+s};
                double[] f = {y,y+s*Math.sqrt(3)/4,y};
                double[] g = {x+s/4,x+s/2,x+3*s/4};
                double[] h = {y+s*Math.sqrt(3)/4,y+s*Math.sqrt(3)/2,y+s*Math.sqrt(3)/4};
                StdDraw.polygon(c,d);
                StdDraw.polygon(e,f);
                StdDraw.polygon(g,h);
                drawTri(x,y,s/2);
            }
        }
     
        public static void main(int size) {
            StdDraw.setXscale(0, size);
            StdDraw.setYscale(0, size);
            StdDraw.setPenColor(StdDraw.BLUE);
            drawTri(0, 0, size);
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski Triangle bug

    Where are the import statements that refer to the package that the StdDraw class is in?
    Where is the starting main method for the class? The starting main method has a String array as its args
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sierpinski Triangle bug

    1. I use BlueJ, and the code for StdDraw is in the same project folder
    2. I was given starter code to complete and that was just how the main method was. I don't think it has any affect on the code anyway
    Last edited by Randomness; June 14th, 2023 at 06:36 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski Triangle bug

    that was just how the main method was. I don't think it has any affect on the code anyway
    The java program needs a main method with this signature:
    public static void main(String[] args) {

    I use BlueJ, and the code for StdDraw is in the same project folder
    I do not jave BlueJ nor the code for StdDraw. I can not compile and execute the code for testing without the StdDraw class and a proper main method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Inverted Right Triangle
    By KillerToFu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2013, 09:12 AM
  2. need help with a mumbers triangle
    By umairbaloch in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 16th, 2012, 10:05 AM
  3. Sierpinski's Triangle in Graphics
    By Europa in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 20th, 2012, 10:18 PM
  4. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM