-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSquareSpiral.java
80 lines (65 loc) · 1.62 KB
/
MultiSquareSpiral.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
76
77
78
79
public class MultiSquareSpiral {
public static void main(String[] args) {
Turtle dan = new Turtle();
dan.penup();
dan.backward(400);
dan.pendown();
multiSquare(dan, 20, 5);
dan.penup();
dan.forward(150);
dan.pendown();
multiSquare2(dan, 20, 5);
dan.penup();
dan.forward(250);
dan.pendown();
concentricSquares(dan, 20, 5);
dan.penup();
dan.forward(250);
dan.pendown();
squareSpiral(dan, 10, 20);
}
public static void square(Turtle t, double x) {
for (int i = 0; i < 4; i++) {
t.forward(x);
t.left(90);
}
}
public static void multiSquare(Turtle t, double base, int numSquares) {
for (int i = 0; i < numSquares; i++) {
//draws a square that grows
square(t, (i + 1) * base);
}
}
public static void multiSquare2(Turtle t, double base, int numSquares) {
for (int i = 1; i <= numSquares; i++) {
square(t, i * base);
}
}
public static void concentricSquares(Turtle t, double base, int numSquares) {
for (int i = 0; i < numSquares; i++) {
square(t, (2 * i + 1) * base);
t.penup();
t.backward(base);
t.right(90);
t.forward(base);
t.left(90);
t.pendown();
}
t.penup();
t.forward(numSquares * base);
t.left(90);
t.forward(numSquares * base);
t.right(90);
t.pendown();
}
public static void squareSpiral(Turtle t, double base, int numSides) {
for (int i = 0; i < numSides; i++) {
t.forward((i + 1) * base);
t.left(90);
}
for (int i = numSides; i > 0; i--){
t.right(90);
t.backward(i * base);
}
}
}