0

我正在为我的三角形绘图编写递归代码。出于某种原因,它不会进行递归,但会进行基础绘图。在弄清楚为什么我的代码不重复时需要一些帮助。另外,如果有人可以帮助我弄清楚如何用颜色填充三角形,那将不胜感激。

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);    
}

}

4

0 回答 0