0

I am writing a recursion code for my triangle drawing. For some reason, it wont do the recursion, but it will do the base drawing. Would like some help in figuring out why my code isn't repeating. Also, if someone could help me figure out how to fill the triangles with a color, that would be much appreciated.

public class Triangle {
     public static void draw(int n, double size, double x, double y) {
        if (n == 0) return;
        StdDraw.setPenColor(StdDraw.BLUE);
        
        StdDraw.setPenColor(StdDraw.BLACK);
        //StdDraw.line(xy, size/2)

       double x1 = x + size/2;
       double y1 = y + size/2; 
       double x2 = x1+ size/2;
       
       // bottom triangle
       StdDraw.line(x,y, x1, y1);
       StdDraw.line(x1, y1, x2, y );
       StdDraw.line(x2, y, x1, y);
       //right triangle
       StdDraw.line(y, y, x2, x2);
       StdDraw.line(x1, x1, x1, y1);
       
       // top triangle and left triangle
       StdDraw.line(y1, x1, x, x2);
       StdDraw.line(x2, x2, x, y);
      
       draw(n-1, size -3, x, y);
       
    }

public static void main(String[] args) {
    double x = 0.0;
    double y = 0.0;
    double size = 1.0;
    int n = 2;
    
    draw(n, size, x, y);    
}

}

  • What is your expected output? I think the 2nd recursion is drawing exactly what 1st recursion draw, hence you see no recursion. They are pixel level identical. 2nd recursion overwrite 1st recursion. – Mr. Brickowski May 03 '21 at 06:30
  • My expected output is that the first drawing will happen and then the second drawing will be a smaller version of it inside of it. – shan_wahl_ May 03 '21 at 19:03
  • How about the inner triangle displacement? Can you upload a image of the expected output? – Mr. Brickowski May 04 '21 at 04:27

0 Answers0