This commit is contained in:
Tiago Batista Cardoso
2026-02-28 11:10:17 +01:00
parent 2736884f46
commit 500e03af35
10 changed files with 508 additions and 28 deletions

View File

@@ -1,3 +1,5 @@
#include "render.h"
#include "algorithms.h"
#include "structs.h"
#include <SDL2/SDL.h>
#include <math.h>
@@ -19,7 +21,7 @@ typedef struct layout_node_t layout_node_t;
// 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);
//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,
@@ -118,7 +120,19 @@ static layout_node_t *compute_layout(const graph_t *graph)
return nodes;
}
void render_graph(SDL_Renderer *renderer, const graph_t *graph)
static SDL_Color community_colors[] = {
{ 251, 73, 52, 255 }, // gruvbox red
{ 250, 189, 47, 255 }, // gruvbox yellow
{ 142, 192, 124, 255 }, // gruvbox green
{ 131, 165, 152, 255 }, // gruvbox aqua
{ 69, 133, 136, 255 }, // gruvbox blue
{ 211, 134, 155, 255 }, // gruvbox pink
{ 254, 128, 25, 255 }, // gruvbox orange
};
#define N_COLORS (sizeof(community_colors) / sizeof(community_colors[0]))
void render_graph(SDL_Renderer *renderer, const graph_t *graph,
VISUALIZATION_TYPE type)
{
if (!renderer || !graph || !graph->adj_lists)
return;
@@ -129,11 +143,21 @@ void render_graph(SDL_Renderer *renderer, const graph_t *graph)
if (!layout)
return;
// Clear background
community_result_t *communities = NULL;
louvain_result_t *louvain = NULL;
switch (type) {
case CLIQUE:
communities = find_k_clique_communities(graph, 3);
break;
case LOUVAIN:
louvain = compute_louvain(graph);
break;
}
SDL_SetRenderDrawColor(renderer, 48, 48, 48, 255);
SDL_RenderClear(renderer);
// Draw edges
SDL_SetRenderDrawColor(renderer, 189, 189, 189, 255);
for (int i = 0; i < n; i++) {
node_t *neighbor = graph->adj_lists[i];
@@ -155,10 +179,34 @@ void render_graph(SDL_Renderer *renderer, const graph_t *graph)
int x = (int)layout[i].x;
int y = (int)layout[i].y;
int c;
SDL_Color col;
switch (type) {
case CLIQUE:
c = communities->node_community[i];
col = (c == -1) ? (SDL_Color){ 150, 150, 150, 255 } :
community_colors[c % N_COLORS];
break;
case LOUVAIN:
c = louvain->node_community[i];
col = (c == -1) ? (SDL_Color){ 168, 153, 132, 255 } :
community_colors[c % N_COLORS];
break;
}
SDL_SetRenderDrawColor(renderer, col.r, col.g, col.b, col.a);
// Node fill
draw_node(renderer, x, y, NODE_RADIUS);
}
if (communities != NULL) {
free_community_result(communities);
}
if (louvain != NULL) {
free_louvain_result(louvain);
}
free(layout);
SDL_RenderPresent(renderer);
}