-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
150 lines (135 loc) · 3.66 KB
/
sketch.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// The array of bubbles
const bubbles = [];
// The scalar field array
const field = [];
// The resolution of the marching square
const resolution = 10;
function setup() {
createCanvas(600, 600);
for (let i = 0; i < 15; i++) bubbles.push(new Bubble());
for (let i = 0; i < width / resolution + 1; i++) {
field[i] = [];
for (let j = 0; j < height / resolution + 1; j++) {
field[i][j] = 0;
}
}
}
function draw() {
background(22);
for (let i = 0; i < field.length; i++) {
for (let j = 0; j < field[i].length; j++) {
field[i][j] = calculateFieldValueAt(i * resolution, j * resolution);
// ✔ Debugging stuffs
// fill(map(field[i][j], 0, 10, 0, 255));
// stroke(0);
// rect(i * resolution, j * resolution, resolution, resolution);
}
}
for (let i = 0; i < field.length - 1; i++) {
for (let j = 0; j < field[i].length - 1; j++) {
const threshold = 3;
const x = i * resolution;
const y = j * resolution;
const a = field[i][j] < threshold ? 0 : 1;
const b = field[i + 1][j] < threshold ? 0 : 1;
const c = field[i + 1][j + 1] < threshold ? 0 : 1;
const d = field[i][j + 1] < threshold ? 0 : 1;
const a_val = field[i][j];
const b_val = field[i + 1][j];
const c_val = field[i + 1][j + 1];
const d_val = field[i][j + 1];
const n = createVector(
lerp(x, x + resolution, (threshold - a_val) / (b_val - a_val)),
y,
);
const e = createVector(
x + resolution,
lerp(y, y + resolution, (threshold - b_val) / (c_val - b_val)),
);
const s = createVector(
lerp(x, x + resolution, (threshold - d_val) / (c_val - d_val)),
y + resolution,
);
const w = createVector(
x,
lerp(y, y + resolution, (threshold - a_val) / (d_val - a_val)),
);
const state = getState(a, b, c, d);
stroke(0, 255, 0);
switch (state) {
case 1:
line(w.x, w.y, s.x, s.y);
break;
case 2:
line(e.x, e.y, s.x, s.y);
break;
case 3:
line(w.x, w.y, e.x, e.y);
break;
case 4:
line(n.x, n.y, e.x, e.y);
break;
case 5:
line(n.x, n.y, w.x, w.y);
line(e.x, e.y, s.x, s.y);
break;
case 6:
line(n.x, n.y, s.x, s.y);
break;
case 7:
line(n.x, n.y, w.x, w.y);
break;
case 8:
line(n.x, n.y, w.x, w.y);
break;
case 9:
line(n.x, n.y, s.x, s.y);
break;
case 10:
line(n.x, n.y, e.x, e.y);
line(w.x, w.y, s.x, s.y);
break;
case 11:
line(n.x, n.y, e.x, e.y);
break;
case 12:
line(w.x, w.y, e.x, e.y);
break;
case 13:
line(e.x, e.y, s.x, s.y);
break;
case 14:
line(w.x, w.y, s.x, s.y);
break;
}
}
}
bubbles.forEach((b) => {
// b.show();
b.update();
});
}
/**
* Calculate the field value at a certain position
* @param {Number} x The x axis coordinate
* @param {Number} y The y axis coordinate
*/
function calculateFieldValueAt(x, y) {
let sum = 0;
bubbles.forEach(
(b) =>
(sum +=
b.radius ** 2 / ((b.position.x - x) ** 2 + (b.position.y - y) ** 2)),
);
return sum;
}
/**
* Get the state code of a given cell
* @param {Number} a The north west corner value
* @param {Number} b The north east corner value
* @param {Number} c The south east corner value
* @param {Number} d The south west corner value
*/
function getState(a, b, c, d) {
return a * 8 + b * 4 + c * 2 + d;
}