# Thread: Help with recursive squares

1. Member Join Date
Sep 2010
Posts
62
Rep Power
0

## Help with recursive squares

Hello, I have to write a program that produces the recursive pattern shown below.

I tried different recursions but I cannot get it done. This is what I've got so far:

Java Code:
```static void drawSquare(int n, double x, double y, double size){
if(n <= 0){  // n is number of squares

// I use predefined class StdDraw
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.filledSquare(x, y, size);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.square(x, y, size);
}
else{

// x0, y0, x1, y1 - coordinates of the squares, produced after each recursive step
x0 = x/2;
y0 = y/2;
x1 = x + x/2;
y1 = y + y/2;
size = size/2.2;

}
}```
I haven't included the recursive calls in the else statement because they don't make sense.

The pseudocode would be something like:

Java Code:
```draw the first big square;
change x, y, size;
draw first smaller square;
change x, y, size;
draw second smaller square;
change x, y, size;
draw third smaller square;
change x, y, size;
draw fourth smaller square;
.
.
.
so on```
but I am not able to put this pseudodoce into real recursion and actually I am not sure if this would be the proper sequence of steps to draw the squares.

Any ideas or suggestions are greatly appreciated.

Luke  Reply With Quote

2. Member Join Date
Sep 2010
Posts
62
Rep Power
0

## Any ideas?  Reply With Quote

#### Posting Permissions

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