Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created November 1, 2012 02:10
Show Gist options
  • Save yao2030/3991198 to your computer and use it in GitHub Desktop.
Save yao2030/3991198 to your computer and use it in GitHub Desktop.
Recursive squares
public class Square
{
public static void drawSquare(int N, double x, double y, double r)
{
if(N == 0)
return;
StdDraw.square(x, y, r);
StdDraw.show(100);
drawSquare(N-1, x - r , y + r, r/2.2);
drawSquare(N-1, x + r , y + r, r/2.2);
drawSquare(N-1, x + r , y - r, r/2.2);
drawSquare(N-1, x - r , y - r, r/2.2);
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
StdDraw.setXscale(-N, N);
StdDraw.setYscale(-N, N);
drawSquare(N, 0, 0, N/2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment