-
Notifications
You must be signed in to change notification settings - Fork 14
/
PercolationVisualizer.java
80 lines (70 loc) · 2.85 KB
/
PercolationVisualizer.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
80
/****************************************************************************
* Compilation: javac PercolationVisualizer.java
* Execution: java PercolationVisualizer input.txt
* Dependencies: Percolation.java StdDraw.java In.java
*
* This program takes the name of a file as a command-line argument.
* From that file, it
*
* - Reads the grid size N of the percolation system.
* - Creates an N-by-N grid of sites (intially all blocked)
* - Reads in a sequence of sites (row i, column j) to open.
*
* After each site is opened, it draws full sites in light blue,
* open sites (that aren't full) in white, and blocked sites in black,
* with with site (1, 1) in the upper left-hand corner.
*
****************************************************************************/
import java.awt.Font;
public class PercolationVisualizer {
// delay in miliseconds (controls animation speed)
private static final int DELAY = 100;
// draw N-by-N percolation system
public static void draw(Percolation perc, int N) {
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setXscale(0, N);
StdDraw.setYscale(0, N);
StdDraw.filledSquare(N/2.0, N/2.0, N/2.0);
// draw N-by-N grid
int opened = 0;
for (int row = 1; row <= N; row++) {
for (int col = 1; col <= N; col++) {
if (perc.isFull(row, col)) {
StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
opened++;
}
else if (perc.isOpen(row, col)) {
StdDraw.setPenColor(StdDraw.WHITE);
opened++;
}
else
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledSquare(col - 0.5, N - row + 0.5, 0.45);
}
}
// write status text
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text(.25*N, -N*.025, opened + " open sites");
if (perc.percolates()) StdDraw.text(.75*N, -N*.025, "percolates");
else StdDraw.text(.75*N, -N*.025, "does not percolate");
}
public static void main(String[] args) {
In in = new In(args[0]); // input file
int N = in.readInt(); // N-by-N percolation system
// turn on animation mode
StdDraw.show(0);
// repeatedly read in sites to open and draw resulting system
Percolation perc = new Percolation(N);
draw(perc, N);
StdDraw.show(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, N);
StdDraw.show(DELAY);
}
}
}