Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************************
- * Compilation: javac Dragon.java
- * Execution: java Dragon N
- *
- * Draws dragon curve of order N.
- *
- * % java Dragon 7
- *
- * Limitations
- * -------------
- * - N must be between 1 and 15
- *
- *************************************************************************/
- public class Dragon {
- private Turtle turtle;
- public Dragon(int N) {
- /**************************************************************************
- * The following constants are used to figure out where to start
- * drawing the dragon curve. left[i] = maximum number of steps taken
- * to the left in dragon(i). right[i], up[i], down[i] are similar.
- **************************************************************************/
- int[] left = { 0, 0, 0, 2, 4, 5, 5, 5, 5, 5, 10, 42, 74, 81, 85, 85 };
- int[] right = { 1, 1, 1, 1, 1, 1, 2, 10, 18, 21, 21, 21, 21, 21, 57, 170 };
- int[] up = { 0, 1, 2, 2, 2, 2, 2, 2, 5, 21, 37, 42, 42, 42, 42, 42 };
- int[] down = { 0, 0, 0, 0, 1, 5, 9, 10, 10, 10, 10, 10, 23, 85, 149, 165 };
- double size = Math.max(left[N] + right[N], up[N] + down[N]);
- double x = (right[N] - left[N]) / 2.0;
- double y = (up[N] - down[N]) / 2.0;
- turtle = new Turtle(0.0, 0.0, 0.0);
- turtle.setXscale(x - size/2, x + size/2);
- turtle.setYscale(y - size/2, y + size/2);
- dragon(N);
- }
- // dragon curve of order n
- public void dragon(int n) {
- if (n == 0) {
- turtle.goForward(1.0);
- }
- else {
- dragon(n-1);
- turtle.turnLeft(90);
- nogard(n-1);
- }
- }
- // reverse dragon curve of order n
- public void nogard(int n) {
- if (n == 0) {
- turtle.goForward(1.0);
- }
- else {
- dragon(n-1);
- turtle.turnLeft(-90);
- nogard(n-1);
- }
- }
- public static void main(String[] args) {
- int N = Integer.parseInt(args[0]);
- if (N < 0 || N > 15) {
- System.out.println("Try a number between 1 and 15 next time.");
- System.exit(0);
- }
- new Dragon(N);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement