-
Notifications
You must be signed in to change notification settings - Fork 2
/
SegmentCurve32.java
64 lines (57 loc) · 1.3 KB
/
SegmentCurve32.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
package turtleClasses;
import java.awt.Color;
import turtleClasses.lib.Turtle;
import turtleClasses.lib.World;
public class SegmentCurve32 {
public static Turtle t;
public static final int MAX = 1;
public static final int F = 10;
public static int n = 0;
public static final Color[] colors = {
Color.RED,
Color.ORANGE,
Color.YELLOW,
Color.GREEN,
Color.CYAN,
Color.BLUE,
Color.MAGENTA,
Color.PINK,
Color.GRAY,
Color.BLACK
};
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-F+F+FF-F+F+FF+F-F-FF+FF-FF+F+F-FF-F-F+FF-F-F+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') {
t.setColor(colors[n++ % colors.length]);
t.forward(F);
}else if (c == '+') {
t.turn(90);
}else if ( c == '-') {
t.turn(-90);
}
}
}
expand(newNum, depth + 1);
}
public static void main (String[] args) {
World w = new World(1200,1000);
t = new Turtle(600,900,w);
t.setColor(Color.ORANGE);
expand("F", 0);
}
}