forked from dimpeshmalviya/JavaBasicPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextRipple.java
More file actions
61 lines (55 loc) · 1.92 KB
/
TextRipple.java
File metadata and controls
61 lines (55 loc) · 1.92 KB
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
import java.util.Random;
public class TextRipple {
static int width = 80, height = 25;
static float[][] oldWave, newWave;
static final char[] CHARS = " .:-=+*#%@".toCharArray();
static Random rnd = new Random();
public static void main(String[] args) throws Exception {
oldWave = new float[height][width];
newWave = new float[height][width];
long delay = 50;
System.out.println("Press Ctrl+C to stop.\n");
for (int frame = 0; ; frame++) {
if (frame % 25 == 0) dropRandom(); // create ripples occasionally
simulate();
render(frame);
Thread.sleep(delay);
}
}
static void dropRandom() {
int r = rnd.nextInt(height - 4) + 2;
int c = rnd.nextInt(width - 4) + 2;
oldWave[r][c] = 5f; // splash intensity
}
static void simulate() {
for (int r = 1; r < height - 1; r++) {
for (int c = 1; c < width - 1; c++) {
// simple wave equation
newWave[r][c] = (
oldWave[r - 1][c] +
oldWave[r + 1][c] +
oldWave[r][c - 1] +
oldWave[r][c + 1]
) / 2 - newWave[r][c];
newWave[r][c] *= 0.985f; // damping
}
}
// swap
float[][] temp = oldWave;
oldWave = newWave;
newWave = temp;
}
static void render(int frame) {
StringBuilder sb = new StringBuilder("\033[H"); // move cursor home
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
float v = oldWave[r][c];
int idx = (int) Math.max(0, Math.min(CHARS.length - 1, (v + 5) / 10 * CHARS.length));
sb.append(CHARS[idx]);
}
sb.append('\n');
}
sb.append("Frame: ").append(frame).append('\n');
System.out.print(sb);
}
}