-
Notifications
You must be signed in to change notification settings - Fork 2
/
KochCurve2.java
75 lines (65 loc) · 1.7 KB
/
KochCurve2.java
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
package turtleClasses;
import java.awt.Color;
import turtleClasses.lib.Turtle;
import turtleClasses.lib.World;
public class KochCurve2 {
public static Turtle kevin;
public static Turtle ammar;
public static Turtle paula;
public static Turtle spencer;
public static final int MAX = 4;
public static final int F = 1;
public static int n = 0;
public static void expand(String num, int depth) {
if (depth > MAX) {
return;
}
StringBuilder builder = new StringBuilder();
for (char c : num.toCharArray()) {
if (c == 'F') {
builder.append("F-F+F+FFF-F-F+F");
} else {
builder.append(c);
}
}
String newNum = builder.toString();
System.out.println(newNum);
//if (depth == MAX) {
for (int i = 0; i < newNum.length(); i++) {
char c = newNum.charAt(i);
if (c == 'F') {
ammar.forward(F);
paula.forward(F);
kevin.forward(F);
spencer.forward(F);
}else if (c == '+') {
ammar.turn(90);
paula.turn(90);
kevin.turn(90);
spencer.turn(90);
}else if ( c == '-') {
ammar.turn(-90);
paula.turn(-90);
kevin.turn(-90);
spencer.turn(-90);
}
}
//}
expand(newNum, depth + 1);
}
public static void main (String[] args) {
World w = new World(1000,1000);
ammar = new Turtle(500,500,w);
paula = new Turtle(500,500,w);
kevin = new Turtle(500,500,w);
spencer = new Turtle(500,500,w);
ammar.setColor(Color.RED);
paula.setColor(Color.ORANGE);
kevin.setColor(Color.YELLOW);
spencer.setColor(Color.CYAN);
paula.turn(90);
kevin.turn(180);
spencer.turn(270);
expand("F", 0);
}
}