#StackBounty: #java #performance #beginner #recursion Drawing a Thue-Morse pattern recursively

Bounty: 50

This is web exercise 3.1.63. from the book Computer Science An Interdisciplinary Approach by Sedgewick & Wayne:

Write a program that reads in a command line input N and plots the
N-by-N Thue-Morse pattern. Below are the Thue-Morse patterns for
N = 4, 8, and 16.

Here is my program:

``````public class ThueMorse
{
public static void drawThueMorse1(int n, double x, double y, double size)
{
if (n == 0) return;
double x1 = x - size/2, x2 = x + size/2;
double y1 = y - size/2, y2 = y + size/2;
StdDraw.setPenColor(StdDraw.BOOK_BLUE);
StdDraw.filledRectangle(x+(3*size/4),y,size/4,size/2);
//StdDraw.pause(300);
StdDraw.filledRectangle(x-(3*size/4),y,size/4,size/2);
//StdDraw.pause(300);
StdDraw.filledRectangle(x,y+(3*size/4),size/2,size/4);
//StdDraw.pause(300);
StdDraw.filledRectangle(x,y-(3*size/4),size/2,size/4);
//StdDraw.pause(300);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledSquare(x,y,size/2);
//StdDraw.pause(300);
StdDraw.filledSquare(x+(3*size/4),y+(3*size/4),size/4);
//StdDraw.pause(300);
StdDraw.filledSquare(x+(3*size/4),y-(3*size/4),size/4);
//StdDraw.pause(300);
StdDraw.filledSquare(x-(3*size/4),y-(3*size/4),size/4);
//StdDraw.pause(300);
StdDraw.filledSquare(x-(3*size/4),y+(3*size/4),size/4);
//StdDraw.pause(300);
drawThueMorse1(n-1, x1, y1, size/2);
drawThueMorse2(n-1, x1, y2, size/2);
drawThueMorse2(n-1, x2, y1, size/2);
drawThueMorse1(n-1, x2, y2, size/2);
}
public static void drawThueMorse2(int n, double x, double y, double size)
{
if (n == 0) return;
double x1 = x - size/2, x2 = x + size/2;
double y1 = y - size/2, y2 = y + size/2;
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(x+(3*size/4),y,size/4,size/2);
//StdDraw.pause(300);
StdDraw.filledRectangle(x-(3*size/4),y,size/4,size/2);
//StdDraw.pause(300);
StdDraw.filledRectangle(x,y+(3*size/4),size/2,size/4);
//StdDraw.pause(300);
StdDraw.filledRectangle(x,y-(3*size/4),size/2,size/4);
//StdDraw.pause(300);
StdDraw.setPenColor(StdDraw.BOOK_BLUE);
StdDraw.filledSquare(x,y,size/2);
//StdDraw.pause(300);
StdDraw.filledSquare(x+(3*size/4),y+(3*size/4),size/4);
//StdDraw.pause(300);
StdDraw.filledSquare(x+(3*size/4),y-(3*size/4),size/4);
//StdDraw.pause(300);
StdDraw.filledSquare(x-(3*size/4),y-(3*size/4),size/4);
//StdDraw.pause(300);
StdDraw.filledSquare(x-(3*size/4),y+(3*size/4),size/4);
//StdDraw.pause(300);
drawThueMorse1(n-1, x1, y1, size/2);
drawThueMorse2(n-1, x1, y2, size/2);
drawThueMorse2(n-1, x2, y1, size/2);
drawThueMorse1(n-1, x2, y2, size/2);
}
public static int log2(int x)
{
return (int) (Math.log(x)/Math.log(2));
}
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
n = log2(n)-1;
drawThueMorse1(n, 0.5, 0.5, 0.5);
}
}
``````

StdDraw is a simple API written by the authors of the book. I checked my program and it works. Here is one instance of it:

Input: N = 256

Output:

Is there any way that I can improve my program?