-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbscan.c
More file actions
148 lines (132 loc) · 3.73 KB
/
dbscan.c
File metadata and controls
148 lines (132 loc) · 3.73 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <raylib.h>
#include <raymath.h>
#include "nob.h"
#define POINT_RADIUS 10
#define POINT_COLOR RED
#define VISITED_COLOR BLUE
#define DBSCAN_RADIUS 50
#define DBSCAN_MINPTS 4
Color all_colors[] = {
LIME,
SKYBLUE,
PURPLE,
RED,
BEIGE,
GOLD,
BLUE,
GREEN,
DARKBLUE,
VIOLET,
YELLOW,
DARKBROWN,
MAGENTA,
DARKPURPLE,
ORANGE,
DARKGREEN,
BROWN,
PINK,
MAROON,
};
typedef struct {
Vector2 position;
int label;
} Point;
typedef struct {
Point *items;
size_t count;
size_t capacity;
} Points;
typedef struct {
size_t *items;
size_t count;
size_t capacity;
} Indices;
void get_neighbors(const Points *points, size_t target, float radius, Indices *neighbors)
{
if (target >= points->count) return;
for (size_t index = 0; index < points->count; ++index) {
if (index == target) continue;
Vector2 p1 = points->items[index].position;
Vector2 p2 = points->items[target].position;
if (Vector2Distance(p1, p2) <= radius) {
da_append(neighbors, index);
}
}
}
void bfs_cluster(Points *points)
{
static Indices wave = {0};
static Indices next_wave = {0};
static Indices neighbors = {0};
da_foreach(Point, point, points) {
point->label = -1;
}
for (int label = 1; true; label++) {
wave.count = 0;
for (size_t target = 0; target < points->count; ++target) {
if (points->items[target].label < 0) {
neighbors.count = 0;
get_neighbors(points, target, DBSCAN_RADIUS, &neighbors);
if (neighbors.count + 1 < DBSCAN_MINPTS) {
points->items[target].label = 0;
} else {
points->items[target].label = label;
da_append(&wave, target);
break;
}
}
}
if (wave.count == 0) break;
while (wave.count > 0) {
next_wave.count = 0;
da_foreach(size_t, target, &wave) {
neighbors.count = 0;
get_neighbors(points, *target, DBSCAN_RADIUS, &neighbors);
if (neighbors.count + 1 < DBSCAN_MINPTS) continue;
da_foreach(size_t, nbor, &neighbors) {
if (points->items[*nbor].label <= 0) {
da_append(&next_wave, *nbor);
points->items[da_last(&next_wave)].label = label;
}
}
}
swap(Indices, wave, next_wave);
}
}
}
int main(void)
{
Points points = {0};
InitWindow(800, 600, "Raylib Template");
SetTargetFPS(60);
bool show_radius = true;
while (!WindowShouldClose()) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
Point point = {
.position = GetMousePosition(),
.label = -1,
};
da_append(&points, point);
}
if (IsKeyPressed(KEY_R)) {
show_radius = !show_radius;
}
if (IsKeyPressed(KEY_SPACE)) {
bfs_cluster(&points);
}
BeginDrawing();
ClearBackground(GetColor(0x181818FF));
da_foreach(Point, point, &points) {
Color color = GRAY;
if (point->label > 0) color = all_colors[point->label%ARRAY_LEN(all_colors)];
DrawCircleV(point->position, POINT_RADIUS, color);
if (show_radius) {
DrawRing(point->position, DBSCAN_RADIUS, DBSCAN_RADIUS + 2, 0, 360, 69, color);
}
DrawText(TextFormat("%d", point->label), point->position.x, point->position.y, 28, WHITE);
}
EndDrawing();
}
CloseWindow();
return 0;
}