-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-1d.js
135 lines (110 loc) · 2.88 KB
/
main-1d.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
const gW = 5;
const grid = [];
let cellSize;
let showStats = false;
function setup() {
createCanvas(900, 100);
for (let i = 0; i < gW; i++) {
grid.push(new Site());
}
cellSize = width/gW;
// Init fluid
//for (let i = 0; i < gW; i++) {
//for (let j = 0; j < gH; j++) {
//// setEquil takes velocity vector and density
//// Setting an x flow between 0 and 0.120 works the best
////grid[i][j].setEquil(new p5.Vector(0.120, 0), 1);
//grid[i][j].setEquil(new p5.Vector(0, 0.07), 1);
//}
//}
}
let gridMouseX = 0;
let gridMouseY = 0;
function mouseMoved() {
gridMouseX = Math.floor(map(mouseX, 0, width, 0, gW));
showStats = true;
}
//function mouseReleased() {
//showStats = false;
//}
function drawArrow(base, vec, myColor) {
push();
stroke(myColor);
strokeWeight(2);
fill(myColor);
translate(base.x, base.y);
line(0, 0, vec.x, vec.y);
rotate(vec.heading());
let arrowSize = 4;
translate(vec.mag() - arrowSize, 0);
line(0, -arrowSize/2, arrowSize, 0);
line(0, arrowSize/2, arrowSize, 0);
pop();
}
function mapColor(density) {
return map(density, 0, 9, 255, 0);
}
function paint() {
for (let i = 0; i < gW; i++) {
const drawx = i * cellSize;
const drawy = 0;
push();
stroke("red");
strokeWeight(4)
square(drawx, drawy, cellSize);
pop();
drawArrow(createVector(drawx + cellSize/2, drawy + cellSize/2), grid[i].velocity, "#f00");
push();
translate(drawx, drawy);
noFill();
const boxSize = cellSize/3;
//for (let dx = 0; dx < 3; dx++) {
//for (let dy = 0; dy < 3; dy++) {
//const xpos = drawx + boxSize * dx
//const ypos = drawy + boxSize * dy
//square(xpos, ypos, boxSize)
//}
//}
// W
push();
fill(mapColor(grid[i].displacements.w.density), 150);
rect(0, 0, boxSize, cellSize);
text(grid[i].displacements.w.density, 50, 50)
pop();
// C
push();
fill(mapColor(grid[i].displacements.c.density), 150);
rect(1 * boxSize, 0, boxSize, cellSize);
pop();
// E
push();
fill(mapColor(grid[i].displacements.e.density), 150);
rect(2 * boxSize, 0, boxSize, cellSize);
pop();
pop();
}
}
function draw() {
strokeWeight(0.5)
background(255);
paint();
grid[0].setEquil(createVector(0.120, 0), 1);
grid[gW-1].setEquil(createVector(0.120, 0), 1);
// THIS IS COLLIDING
for (let i = 1; i < gW; i++) {
grid[i].collide();
}
// WHY??
//grid[gW-1].displacements.w = grid[gW-2].displacements.w;
// THIS IS STREAMING
for (let i = 1; i < gW - 1; i++) {
grid[i].displacements.e = grid[i-1].displacements.e;
grid[i].displacements.w = grid[i+1].displacements.w
}
textSize(16);
text(`[${gridMouseX}, ${gridMouseY}]:`, 10, 10)
text(`Omega: ${grid[gridMouseX].OMEGA.toFixed(4)}`, 10 + 10, 10 + 20)
text(`Density: ${grid[gridMouseX].density.toFixed(4)}`, 10 + 10, 10 + 40)
text(`Velocity (X): ${grid[gridMouseX].velocity.x.toFixed(4)}`, 10 + 10, 10 + 60)
text(`Velocity (Y): ${grid[gridMouseX].velocity.y.toFixed(4)}`, 10 + 10, 10 + 80)
}