-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.js
370 lines (310 loc) · 9.21 KB
/
test2.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
elem = document.getElementById("container");
setCanvas(elem);
w = WIDTH;
h = HEIGHT;
var t = 1;
//define the implicit function
function f(x, y) {
x = x - w / 2;
y = y - h / 2 - 50;
return (x / 100) ** 2 + ((-y / 100) - sqrt(abs(x / 100))) ** 2 + -t;
}
ii = 1;
var ll = 2000;
function draw() {
clearCanvas();
t0 = performance.now();
//make a radial gradient
new Gradient("radial", [
["0%", "#f049"],
["100%", "#f040"]
], "pad", "grad")
//draw the implicit function
var points = marchingSquares(f, 0, 0, w, 0, h, h / 2);
/*
//remove the points that are too close to each other
var pointsfar = [];
for (var i = 0; i < points.length; i++) {
var p = points[i];
var far = true;
for (var j = 0; j < pointsfar.length; j++) {
var q = pointsfar[j];
if (dist(p[0], p[1], q[0], q[1]) < 1) {
far = false;
break;
}
}
if (far) pointsfar.push(p);
}
points = pointsfar;
*/
/*
//take the first point and sort the points such that the next point is the closest to the previous point
var points2 = [];
var p = points[0];
points2.push(p);
points.splice(0,1);
while (points.length > 0) {
var min = 1000000
var index = 0;
for (var i = 0; i < points.length; i++) {
var d = dist(p[0], p[1], points[i][0], points[i][1]);
if (d < min) {
min = d;
index = i;
}
}
p = points[index];
points2.push(p);
points.splice(index,1);
}
*/
//draw the points
//new polygon(points2.splice(0,ll), "red", 0, "red",2,false);
//draw a grid
for (var i = 0; i < w; i += w / 10) {
new line(i, 0, i, h, "#0005", 1);
}
for (var i = 0; i < h; i += h / 10) {
new line(0, i, w, i, "#0005", 1);
}
new circle(w / 2, h / 2, 300, "url(#grad)", 1, "#0000", 1);
t1 = performance.now();
console.log("Call to calculate took " + (t1 - t0) + " milliseconds.")
t0 = performance.now();
for (var i = 0; i < points.length; i++) {
var p1 = points[i][0];
var p2 = points[i][1];
new line(p1[0], p1[1], p2[0], p2[1], "black", 1.5);
}
//requestAnimationFrame(draw);
//make a blurry circle
ll += 1;
t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
t0 = performance.now();
quadtree(f, 0,0,0,w,h,0);
t1 = performance.now();
console.log("Call to quadtree took " + (t1 - t0) + " milliseconds.")
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
draw();
////////////////////////
// Marching Squares
////////////////////////
function marchingSquares(zFunc, c, xMin, xMax, yMin, yMax, resolution) {
var xStep = (xMax - xMin) / resolution;
var yStep = (yMax - yMin) / resolution;
var points = [];
for (var x = xMin; x < xMax; x += xStep) {
for (var y = yMin; y < yMax; y += yStep) {
var z1 = zFunc(x, y); // bottom left corner
var z2 = zFunc(x + xStep, y); // bottom right corner
var z4 = zFunc(x + xStep, y + yStep); // top right corner
var z8 = zFunc(x, y + yStep); // top left corner
var n = 0;
if (z1 > c) n += 1;
if (z2 > c) n += 2;
if (z4 > c) n += 4;
if (z8 > c) n += 8;
// calculate linear interpolation values along the given sides.
// to simplify, could assume each is 0.5*xStep or 0.5*yStep accordingly.
var bottomInterp = (c - z1) / (z2 - z1) * xStep;
var topInterp = (c - z8) / (z4 - z8) * xStep;
var leftInterp = (c - z1) / (z8 - z1) * yStep;
var rightInterp = (c - z2) / (z4 - z2) * yStep;
// for a visual diagram of cases: https://en.wikipedia.org/wiki/Marching_squares
if (n == 1 || n == 14) // lower left corner
points.push([
[x, y + leftInterp, c],
[x + bottomInterp, y, c]
]);
else if (n == 2 || n == 13) // lower right corner
points.push([
[x + bottomInterp, y, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 4 || n == 11) // upper right corner
points.push([
[x + topInterp, y + yStep, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 8 || n == 7) // upper left corner
points.push([
[x, y + leftInterp, c],
[x + topInterp, y + yStep, c]
]);
else if (n == 3 || n == 12) // horizontal
points.push([
[x, y + leftInterp, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 6 || n == 9) // vertical
points.push([
[x + bottomInterp, y, c],
[x + topInterp, y + yStep, c]
]);
else if (n == 5) // should do subcase // lower left & upper right
points.push([
[x, y + leftInterp, c],
[x + bottomInterp, y, c],
[x + topInterp, y + yStep, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 10) // should do subcase // lower right & upper left
points.push([
[x + bottomInterp, y, c],
[x + xStep, y + rightInterp, c],
[x, y + yStep / 2, c],
[x, y + leftInterp, c],
[x + topInterp, y + yStep, c]
]);
else if (n == 0 || n == 15) // no line segments appear in this grid square.
points.push();
}
}
return points;
}
////////////////////////
// Quadtree to calculate only the points that are needed
////////////////////////
function quadtree(zFunc, c, x, y, dx, dy, depth) {
//draw the rectangle around the quadtree
new rect(x,y,dx,dy,"#0001",0,"#0004",1);
var SEARCH_DEPTH = 1;
var PLOT_DEPTH = 8;
//console.log("quadtree");
//console.log(depth);
if (depth < SEARCH_DEPTH) {
dx = dx / 2;
dy = dy / 2;
quadtree(zFunc, c, x, y, dx, dy, depth + 1);
quadtree(zFunc, c, x + dx, y, dx, dy, depth + 1);
quadtree(zFunc, c, x, y + dy, dx, dy, depth + 1);
quadtree(zFunc, c, x + dx, y + dy, dx, dy, depth + 1);
//console.log("searching 1");
} else {
if (hasSegment(zFunc, c, x, y, dx, dy)) {
if (depth >= PLOT_DEPTH) {
// console.log("plotting");
a=addSegment(zFunc, c, x, y, dx, dy);
// console.log(a);
} else {
dx = dx / 2;
dy = dy / 2;
// console.log("searching 2");
quadtree(zFunc, c, x, y, dx, dy, depth + 1);
quadtree(zFunc, c, x + dx, y, dx, dy, depth + 1);
quadtree(zFunc, c, x, y + dy, dx, dy, depth + 1);
quadtree(zFunc, c, x + dx, y + dy, dx, dy, depth + 1);
}
}
else{
//console.log("no segment");
}
}
}
function hasSegment(zFunc, c, x, y, dx, dy) {
var z1 = zFunc(x, y); // bottom left corner
var z2 = zFunc(x + dx, y); // bottom right corner
var z4 = zFunc(x + dx, y + dy); // top right corner
var z8 = zFunc(x, y + dy); // top left corner
var n = 0;
if (z1 > c) n += 1;
if (z2 > c) n += 2;
if (z4 > c) n += 4;
if (z8 > c) n += 8;
//console.log(n != 0 && n != 15);
return n != 0 && n != 15;
}
function addSegment(zFunc, c, x, y, dx, dy) {
var xStep = dx;
var yStep = dy;
var points = [];
var z1 = zFunc(x, y); // bottom left corner
var z2 = zFunc(x + xStep, y); // bottom right corner
var z4 = zFunc(x + xStep, y + yStep); // top right corner
var z8 = zFunc(x, y + yStep); // top left corner
var n = 0;
if (z1 > c) n += 1;
if (z2 > c) n += 2;
if (z4 > c) n += 4;
if (z8 > c) n += 8;
// calculate linear interpolation values along the given sides.
// to simplify, could assume each is 0.5*xStep or 0.5*yStep accordingly.
var bottomInterp = (c - z1) / (z2 - z1) * xStep;
var topInterp = (c - z8) / (z4 - z8) * xStep;
var leftInterp = (c - z1) / (z8 - z1) * yStep;
var rightInterp = (c - z2) / (z4 - z2) * yStep;
// for a visual diagram of cases: https://en.wikipedia.org/wiki/Marching_squares
if (n == 1 || n == 14) // lower left corner
points.push([
[x, y + leftInterp, c],
[x + bottomInterp, y, c]
]);
else if (n == 2 || n == 13) // lower right corner
points.push([
[x + bottomInterp, y, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 4 || n == 11) // upper right corner
points.push([
[x + topInterp, y + yStep, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 8 || n == 7) // upper left corner
points.push([
[x, y + leftInterp, c],
[x + topInterp, y + yStep, c]
]);
else if (n == 3 || n == 12) // horizontal
points.push([
[x, y + leftInterp, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 6 || n == 9) // vertical
points.push([
[x + bottomInterp, y, c],
[x + topInterp, y + yStep, c]
]);
else if (n == 5) // should do subcase // lower left & upper right
points.push([
[x, y + leftInterp, c],
[x + bottomInterp, y, c],
[x + topInterp, y + yStep, c],
[x + xStep, y + rightInterp, c]
]);
else if (n == 10) // should do subcase // lower right & upper left
points.push([
[x + bottomInterp, y, c],
[x + xStep, y + rightInterp, c],
[x, y + yStep / 2, c],
[x, y + leftInterp, c],
[x + topInterp, y + yStep, c]
]);
else if (n == 0 || n == 15) // no line segments appear in this grid square.
points.push();
//draw the line segments
for (var i = 0; i < points.length; i++) {
var p1=points[i][0];
var p2=points[i][1];
new line(p1[0], p1[1], p2[0], p2[1],"#000",2);
}
return points;
}
//check speed difference in vanilla js math and mathjs
function mathTest() {
t0=performance.now();
for (var i = 0; i < 100000; i++) {
Math.sin(i);
}
t1=performance.now();
console.log("Math.sin took " + (t1 - t0) + " milliseconds.");
t0=performance.now();
for (var i = 0; i < 100000; i++) {
math.evaluate("sin("+i+")");
}
t1=performance.now();
console.log("math.sin took " + (t1 - t0) + " milliseconds.");
}