Skip to content

Commit

Permalink
Add marker visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmb committed Apr 27, 2017
1 parent 17b3f18 commit aa32484
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions euler.cxx
Original file line number Diff line number Diff line change
@@ -1,45 +1,89 @@
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>

#include <algorithm>
#include <random>

#include "math/vec2f.h"
#include "misc/terminal.h"

const size_t X = 10;
const size_t Y = 10;

float g_p[X][Y];
float g_u[X][Y];
const float k_s = 1; // side length
const float k_invs = 1/k_s;
const float k_d = 1.0; // density
const float k_invd = 1/k_d;

float g_p[Y][X];
float g_u[Y][X];
float g_v[Y][X];
float g_utmp[Y][X];
float g_vtmp[Y][X];

bool g_solid[Y][X];

float g_q[X][Y];
bool g_solid[X][Y];
const size_t N = 4*4*4;
vec2f g_markers[N];

const vec2f k_g{0,-9.81};
const float k_g = -9.81;

void sim_init() {
// setup walls
for (size_t i = 0; i < X; ++i) {
g_solid[i][0] = true;
g_solid[0][i] = true;
}
for (size_t i = 0; i < Y; ++i) {
g_solid[0][i] = true;
g_solid[X-1][i] = true;
g_solid[i][0] = true;
g_solid[i][X-1] = true;
}

std::mt19937 rng_engine(123456789u);
std::uniform_real_distribution<float> distribution(0.f, 0.25f);
auto rng = [&](){ return distribution(rng_engine); };

// setup fluid markers
size_t idx = 0;
for (size_t i = 1; i < 5; ++i) {
for (size_t j = 1; j < 5; ++j) {
for (size_t k = 0; k < 4; ++k) {
float x = i + (k < 2 ? 0 : 0.5f) + rng();
float y = j + (k % 2 ? 0 : 0.5f) + rng();
g_markers[idx++] = k_s*vec2f{x,y};
}
}
}
}

void sim_step() {
}

void draw_rows(struct buffer* buf) {
for (size_t y = 0; y < Y; y++) {
buffer_append(buf, "~", 1);
if (y < Y - 1) {
unsigned char marker_count[Y][X] = {};
for (size_t i = 0; i < N; ++i) {
size_t x = (size_t)floor(g_markers[i].x());
size_t y = Y-1-(size_t)floor(g_markers[i].y());
marker_count[y][x]++;
}
const char* symbol[4] = {" ","o","O","0"};
const unsigned char max_symbol = 3;
for (int y = Y; y-- > 0;) {
for (size_t x = 0; x < X; x++) {
if (g_solid[y][x]) {
buffer_append(buf, "X", 1);
} else {
unsigned char i = std::min(marker_count[y][x], max_symbol);
buffer_append(buf, symbol[i], 1);
}
}
if (y > 0) {
buffer_append(buf, "\r\n", 2);
}
}
}

#include <errno.h>
#include <unistd.h>

void refresh_screen(buffer* buf) {
buffer_clear(buf);

Expand Down Expand Up @@ -69,7 +113,6 @@ int main(int argc, char** argv) {

enable_raw_mode();
buffer buf = { 0, 0 };
clear_screen(&buf);

sim_init();

Expand Down

0 comments on commit aa32484

Please sign in to comment.