-
Notifications
You must be signed in to change notification settings - Fork 1
/
PercolationVisualizer.java
72 lines (63 loc) · 2.31 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
import java.awt.Font;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import java.util.Scanner;
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.05*n, 1.05*n);
StdDraw.setYscale(-0.05*n, 1.05*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);
}
}
StdDraw.setFont(new Font("SansSerif", Font.BOLD, 15));
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text(0.25*n, -0.025*n, opened + " open sites");
if (perc.percolates()) StdDraw.text(0.75*n, -0.025*n, "percolates");
else StdDraw.text(0.75*n, -0.025*n, "does not percolate");
}
public static void main(String[] args) {
int n;
System.out.print("Enter the order of matrix: ");
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
scan.close();
Generator g = new Generator();
g.generateCods(n);
In in = new In("input.txt"); // input file
// turn on animation mode
StdDraw.enableDoubleBuffering();
// repeatedly read in sites to open and draw resulting system
Percolation perc = new Percolation(n);
draw(perc, n);
StdDraw.show();
StdDraw.pause(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, n);
StdDraw.show();
StdDraw.pause(DELAY);
}
}
}