-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYarnPattern.java
58 lines (52 loc) · 1.81 KB
/
YarnPattern.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
/*
* File: YarnPattern.java
* ----------------------
* This program illustrates the use of the GLine class to simulate
* winding a piece of colored yarn around a set of pegs equally
* spaced along the edges of the canvas. At each step, the yarn is
* stretched from its current peg to the one DELTA pegs further on.
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import java.util.*;
public class YarnPattern extends GraphicsProgram {
/** Runs the program */
public void run() {
ArrayList pegs = createPegList();
int thisPeg = 0;
int nextPeg = -1;
while (thisPeg != 0 || nextPeg == -1) {
nextPeg = (thisPeg + DELTA) % pegs.size();
GPoint p0 = (GPoint) pegs.get(thisPeg);
GPoint p1 = (GPoint) pegs.get(nextPeg);
GLine line = new GLine(p0.getX(), p0.getY(),
p1.getX(), p1.getY());
line.setColor(Color.MAGENTA);
add(line);
thisPeg = nextPeg;
}
}
/* Create an array list containing the locations of the pegs */
private ArrayList createPegList() {
ArrayList pegs = new ArrayList();
for (int i = 0; i < N_ACROSS; i++) {
pegs.add(new GPoint(i * PEG_SEP, 0));
}
for (int i = 0; i < N_DOWN; i++) {
pegs.add(new GPoint(N_ACROSS * PEG_SEP, i * PEG_SEP));
}
for (int i = N_ACROSS; i > 0; i--) {
pegs.add(new GPoint(i * PEG_SEP, N_DOWN * PEG_SEP));
}
for (int i = N_DOWN; i > 0; i--) {
pegs.add(new GPoint(0, i * PEG_SEP));
}
return pegs;
}
/* Private constants */
private static final int N_ACROSS = 50;
private static final int N_DOWN = 30;
private static final int PEG_SEP = 10;
private static final int DELTA = 67;
}