This repository was archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInteractivePercolationVisualizer.java
61 lines (49 loc) · 2.1 KB
/
InteractivePercolationVisualizer.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
/******************************************************************************
* Compilation: javac InteractivePercolationVisualizer.java
* Execution: java InteractivePercolationVisualizer n
* Dependencies: PercolationVisualizer.java Percolation.java
* StdDraw.java StdOut.java
*
* This program takes the grid size n as a command-line argument.
* Then, the user repeatedly clicks sites to open with the mouse.
* 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.
*
******************************************************************************/
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
public class InteractivePercolationVisualizer {
public static void main(String[] args) {
// n-by-n percolation system (read from command-line, default = 10)
int n = 10;
if (args.length == 1) n = Integer.parseInt(args[0]);
// repeatedly open site specified my mouse click and draw resulting system
StdOut.println(n);
StdDraw.enableDoubleBuffering();
Percolation perc = new Percolation(n);
PercolationVisualizer.draw(perc, n);
StdDraw.show();
while (true) {
// detected mouse click
if (StdDraw.isMousePressed()) {
// screen coordinates
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
// convert to row i, column j
int i = (int) (n - Math.floor(y));
int j = (int) (1 + Math.floor(x));
// open site (i, j) provided it's in bounds
if (i >= 1 && i <= n && j >= 1 && j <= n) {
if (!perc.isOpen(i, j)) {
StdOut.println(i + " " + j);
}
perc.open(i, j);
}
// draw n-by-n percolation system
PercolationVisualizer.draw(perc, n);
StdDraw.show();
}
StdDraw.pause(20);
}
}
}