Listing 2: Drawing the Hilbert curve
enum { UP, LEFT, DOWN, RIGHT, }; void hilbert(int level,int direction=UP) { if (level==1) { switch (direction) { /* move() could draw a line in... */ /* ...the indicated direction */ case LEFT: move(RIGHT); move(DOWN); move(LEFT); break; case RIGHT: move(LEFT); move(UP); move(RIGHT); break; case UP: move(DOWN); move(RIGHT); move(UP); break; case DOWN: move(UP); move(LEFT); move(DOWN); break; } /* switch */ } else { switch (direction) { case LEFT: hilbert_level(level-1,UP); move(RIGHT); hilbert_level(level-1,LEFT); move(DOWN); hilbert_level(level-1,LEFT); move(LEFT); hilbert_level(level-1,DOWN); break; case RIGHT: hilbert_level(level-1,DOWN); move(LEFT); hilbert_level(level-1,RIGHT); move(UP); hilbert_level(level-1,RIGHT); move(RIGHT); hilbert_level(level-1,UP); break; case UP: hilbert_level(level-1,LEFT); move(DOWN); hilbert_level(level-1,UP); move(RIGHT); hilbert_level(level-1,UP); move(UP); hilbert_level(level-1,RIGHT); break; case DOWN: hilbert_level(level-1,RIGHT); move(UP); hilbert_level(level-1,DOWN); move(LEFT); hilbert_level(level-1,DOWN); move(DOWN); hilbert_level(level-1,LEFT); break; } /* switch */ } /* if */ } /* End of File */