From 2736884f467eb2de2b2a9442b42f8d2dfbd60762 Mon Sep 17 00:00:00 2001 From: Tiago Batista Cardoso Date: Tue, 24 Feb 2026 18:28:01 +0100 Subject: [PATCH] colorz --- .gitignore | 1 + render.c | 44 +++++++++++++++----------------------------- 2 files changed, 16 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/render.c b/render.c index f31c28a..1a87bbd 100644 --- a/render.c +++ b/render.c @@ -6,17 +6,20 @@ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 -#define NODE_RADIUS 15 -#define ITERATIONS 500 // layout simulation steps -#define COOLING 0.97 // temperature cooling rate +#define NODE_RADIUS 5 +#define ITERATIONS 1000 // layout simulation steps +#define COOLING 1 // temperature cooling rate -typedef struct { +struct layout_node_t { double x, y; // position double vx, vy; // velocity / displacement -} layout_node_t; +}; +typedef struct layout_node_t layout_node_t; -static void draw_circle(SDL_Renderer *renderer, int cx, int cy, int r) +// display a simple node of radius r +static void draw_node(SDL_Renderer *renderer, int cx, int cy, int r) { + SDL_SetRenderDrawColor(renderer, 215, 153, 33, 255); for (int dy = -r; dy <= r; dy++) { int dx = (int)sqrt((double)(r * r - dy * dy)); SDL_RenderDrawLine(renderer, cx - dx, cy + dy, cx + dx, @@ -24,7 +27,7 @@ static void draw_circle(SDL_Renderer *renderer, int cx, int cy, int r) } } -// Fruchterman-Reingold force-directed layout +// compute a layout based on the fruchtermann-reingold algorithm. static layout_node_t *compute_layout(const graph_t *graph) { int n = graph->n; @@ -32,8 +35,6 @@ static layout_node_t *compute_layout(const graph_t *graph) if (!nodes) return NULL; - // Seed random positions - srand((unsigned)time(NULL)); for (int i = 0; i < n; i++) { nodes[i].x = (double)(rand() % (WINDOW_WIDTH - 100)) + 50; nodes[i].y = (double)(rand() % (WINDOW_HEIGHT - 100)) + 50; @@ -42,15 +43,13 @@ static layout_node_t *compute_layout(const graph_t *graph) } double area = (WINDOW_WIDTH - 200) * (WINDOW_HEIGHT - 200); - double k = sqrt(area / n); // optimal distance between nodes - double temp = WINDOW_WIDTH * 0.1; // initial temperature + double k = sqrt(area / n); + double temp = WINDOW_WIDTH * 0.1; for (int iter = 0; iter < ITERATIONS; iter++) { - // Reset displacements for (int i = 0; i < n; i++) nodes[i].vx = nodes[i].vy = 0.0; - // Repulsive forces (all pairs) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double dx = nodes[i].x - nodes[j].x; @@ -70,7 +69,6 @@ static layout_node_t *compute_layout(const graph_t *graph) } } - // Attractive forces (edges only) for (int i = 0; i < n; i++) { node_t *neighbor = graph->adj_lists[i]; while (neighbor) { @@ -95,7 +93,6 @@ static layout_node_t *compute_layout(const graph_t *graph) } } - // Apply displacements, clamped to temperature for (int i = 0; i < n; i++) { double disp = sqrt(nodes[i].vx * nodes[i].vx + nodes[i].vy * nodes[i].vy); @@ -115,7 +112,6 @@ static layout_node_t *compute_layout(const graph_t *graph) nodes[i].y)); } - // Cool the temperature temp *= COOLING; } @@ -134,11 +130,11 @@ void render_graph(SDL_Renderer *renderer, const graph_t *graph) return; // Clear background - SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255); + SDL_SetRenderDrawColor(renderer, 48, 48, 48, 255); SDL_RenderClear(renderer); // Draw edges - SDL_SetRenderDrawColor(renderer, 30, 30, 30, 255); + SDL_SetRenderDrawColor(renderer, 189, 189, 189, 255); for (int i = 0; i < n; i++) { node_t *neighbor = graph->adj_lists[i]; @@ -160,17 +156,7 @@ void render_graph(SDL_Renderer *renderer, const graph_t *graph) int y = (int)layout[i].y; // Node fill - SDL_SetRenderDrawColor(renderer, 100, 149, 237, 255); - draw_circle(renderer, x, y, NODE_RADIUS); - - // Node border - SDL_SetRenderDrawColor(renderer, 200, 220, 255, 255); - for (int deg = 0; deg < 360; deg++) { - double a = deg * M_PI / 180.0; - SDL_RenderDrawPoint(renderer, - x + (int)(NODE_RADIUS * cos(a)), - y + (int)(NODE_RADIUS * sin(a))); - } + draw_node(renderer, x, y, NODE_RADIUS); } free(layout);