public class Hilbert { // turtle graphics position and angle (yuck, static variables) private static double x = 0.0, y = 0.0; private static double orientation = 0.0; // rotate turtle's orientation his many degrees public static void rotate(double angle) { orientation += angle; } // move turtle forward d units in current direction public static void forward(double d) { double x0 = x, y0 = y; x += d * Math.cos(Math.toRadians(orientation)); y += d * Math.sin(Math.toRadians(orientation)); StdDraw.line(x0, y0, x, y); } // Hilbert curve public static void hilbert0(int n) { if (n == 0) return; rotate(90); hilbert1(n-1); forward(1.0); rotate(-90); hilbert0(n-1); forward(1.0); hilbert0(n-1); rotate(-90); forward(1.0); hilbert1(n-1); rotate(90); } // evruc trebliH public static void hilbert1(int n) { if (n == 0) return; rotate(-90); hilbert0(n-1); forward(1.0); rotate(90); hilbert1(n-1); forward(1.0); hilbert1(n-1); rotate(90); forward(1.0); hilbert0(n-1); rotate(-90); } // plot a Hilber curve of order N public static void main(String args[]) { int N = 5;//Integer.parseInt(args[0]); double max = Math.pow(2, N); StdDraw.setXscale(0, max); StdDraw.setYscale(0, max); x = 0.5; y = 0.5; hilbert0(N); } }