-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragon.java
More file actions
76 lines (64 loc) · 2.22 KB
/
Dragon.java
File metadata and controls
76 lines (64 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/******************************************************************************
* 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) {
StdOut.println("Try a number between 1 and 15 next time.");
return;
}
new Dragon(n);
}
}