lnvis

nanovg lightning network visualizer
git clone git://jb55.com/lnvis
Log | Files | Refs | README | LICENSE

commit 933299a586d6f761cf6dc84ef733cde2e72b0f00
parent 30c347a2acf41879c15277e1ce9d91132f5d84a9
Author: William Casarin <jb55@jb55.com>
Date:   Sat,  4 Aug 2018 18:46:30 -0700

random network

Diffstat:
Mmain.c | 131++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Mupdate.c | 3++-
2 files changed, 95 insertions(+), 39 deletions(-)

diff --git a/main.c b/main.c @@ -66,53 +66,101 @@ void mouse_click(GLFWwindow *win, int button, int action, int mods) } +void random_network(int ww, int wh, int max_per_node, int num_nodes, struct ln *ln) { + int i, j; + int from, to; + int tries = 0; + struct node *n; + u8 connections[num_nodes][num_nodes]; + memset(connections, 0, sizeof(connections)); + + ln->nodes = malloc(sizeof(*ln->nodes) * num_nodes); + ln->channel_count = 0; + ln->node_count = num_nodes; + ln->channels = malloc(sizeof(*ln->channels) * num_nodes * max_per_node); + + for (i = 0; i < num_nodes; ++i) { + n = &ln->nodes[i]; + + n->alias = "test"; + n->color.r = rand_0to1(); + n->color.g = rand_0to1(); + n->color.b = rand_0to1(); + n->color.a = 1.0; + n->x = ww * rand_0to1(); + n->y = wh * rand_0to1(); + n->ax = 0.0; + n->ay = 0.0; + n->vx = 0.0; + n->vy = 0.0; + n->size = 10; + } + + // connect nodes randomly + for (i = 0; i < num_nodes; ++i) { + + skip: + // for each node, it can have 0 to max_per_node connections + for (j = 0; j < rand() % max_per_node; ++j) { + from = i; + do { + tries++; + if (tries > 5) { + tries = 0; + goto skip; + } + to = rand() % num_nodes; + } + while(connections[from][to]); + tries = 0; + + connections[from][to] = 1; + + struct channel *c = + &ln->channels[ln->channel_count++]; + + c->nodes[0] = &ln->nodes[from]; + c->nodes[1] = &ln->nodes[to]; + + } + + // keep trying until we find on that isn't already connected + } +} + int main() { GLFWwindow *window; + int first = 1; DemoData data; NVGcontext *vg = NULL; PerfGraph fps; double prevt = 0; struct ln ln; - struct node test_nodes[] = { - { - .alias = "@jb55", - .color = { { 1.0, 0, 0, 1.0 } }, - }, - { - .alias = "SuperHub", - .color = { { 0.0, 1.0, 0.0, 1.0 } }, - }, - { - .alias = "satoshis.place", - .color = { { 0.0, 0.5, 1.0, 1.0 } }, - }, - }; - - struct channel test_channels[] = { - { - .nodes = { &test_nodes[0], &test_nodes[1] }, - }, - { - .nodes = { &test_nodes[1], &test_nodes[2] }, - } - }; - - ln.nodes = test_nodes; - ln.node_count = ARRAY_SIZE(test_nodes); - - ln.channels = test_channels; - ln.channel_count = ARRAY_SIZE(test_channels); - - for (u32 i = 0; i < ln.node_count; ++i) { - struct node *n = &ln.nodes[i]; - n->x = 100.0 * (i+1); - n->y = 100.0 * (i+1); - n->vx = 200.0 * rand_0to1(); - n->vy = 200.0 * rand_0to1(); - n->size = 20; - } + /* struct node test_nodes[] = { */ + /* { */ + /* .alias = "@jb55", */ + /* .color = { { 1.0, 0, 0, 1.0 } }, */ + /* }, */ + /* { */ + /* .alias = "SuperHub", */ + /* .color = { { 0.0, 1.0, 0.0, 1.0 } }, */ + /* }, */ + /* { */ + /* .alias = "satoshis.place", */ + /* .color = { { 0.0, 0.5, 1.0, 1.0 } }, */ + /* }, */ + /* }; */ + + /* struct channel test_channels[] = { */ + /* { */ + /* .nodes = { &test_nodes[0], &test_nodes[1] }, */ + /* }, */ + /* { */ + /* .nodes = { &test_nodes[1], &test_nodes[2] }, */ + /* } */ + /* }; */ if (!glfwInit()) { printf("Failed to init GLFW."); @@ -192,6 +240,13 @@ int main() glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); + + + if (first) { + random_network(winWidth, winHeight, 3, 100, &ln); + first = 0; + } + glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. diff --git a/update.c b/update.c @@ -1,6 +1,7 @@ #include "ln.h" #include <math.h> +#include <assert.h> #include <stdio.h> struct node *hit_node(struct ln *ln) { @@ -86,6 +87,6 @@ void update(struct ln *ln, double dt) node->x += node->vx * dt; node->y += node->vy * dt; - } + }