summaryrefslogtreecommitdiff
path: root/examples/graph.c
blob: 6cb8198f6a6e025e6fc9a33723fc344cdf2b9c33 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#if 0 /*
#/  ================================================================
#/
#/    graph.c
#/
#/  ================================================================
#/
#/    Self-compilation shell script
#/
SRC=${0##*./}
BIN=${SRC%.*}
gcc                                         \
  -Wall -Wextra -Werror -pedantic           \
  -Wno-old-style-declaration                \
  -Wno-missing-braces                       \
  -Wno-unused-variable                      \
  -Wno-unused-but-set-variable              \
  -Wno-unused-parameter                     \
  -Wno-overlength-strings                   \
  -O3                                       \
  -fsanitize=undefined,address,leak -mshstk \
  -lX11 -lm                                 \
  -o $BIN $SRC &&                           \
  ./$BIN $@ && rm $BIN
exit $? # */
#endif

#include "../graphics.c"

enum {
  MAX_NUM_NODES = 1024,
  MAX_NUM_EDGES = 1024,
};

typedef struct {
  b8  enabled;
  f64 x;
  f64 y;
  f64 radius;
  b8  hover;
} Node;

typedef struct {
  b8  enabled;
  i64 src;
  i64 dst;
  f64 width;
  b8  hover;
} Edge;

typedef struct {
  Node nodes[MAX_NUM_NODES];
  Edge edges[MAX_NUM_EDGES];
} World;

World world = {0};

b8 check_node_index(i64 node_index) {
  return
       node_index >= 0 && node_index < MAX_NUM_NODES
    && world.nodes[node_index].enabled;
}

b8 check_edge_index(i64 edge_index) {
  return
       edge_index >= 0 && edge_index < MAX_NUM_EDGES
    && world.edges[edge_index].enabled
    && check_node_index(world.edges[edge_index].src)
    && check_node_index(world.edges[edge_index].dst);
}

void draw_node(i64 node_index) {
  assert(check_node_index(node_index));

  Node n = world.nodes[node_index];

  u32 color = 0; // black color

  if (n.hover)
    color = 0x007f00; // green color

  fill_ellipse(
    OP_SET, // set pixels
    color,
    n.x - n.radius,
    n.y - n.radius,
    n.radius * 2,
    n.radius * 2
  );
}

void draw_edge(i64 edge_index) {
  assert(check_edge_index(edge_index));

  Edge e  = world.edges[edge_index];
  Node n0 = world.nodes[e.src];
  Node n1 = world.nodes[e.dst];

  u32 color = 0x7f7f7f; // grey color

  if (e.hover)
    color = 0x007f00; // green color

  fill_line(
    OP_SET, // set pixels
    color,
    n0.x,
    n0.y,
    n1.x,
    n1.y,
    e.width
  );
}

void draw_graph(void) {
  //  draw all edges
  for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
    if (world.edges[i].enabled)
      draw_edge(i);

  //  draw all nodes
  for (i64 i = 0; i < MAX_NUM_NODES; ++i)
    if (world.nodes[i].enabled)
      draw_node(i);
}

void update_node(i64 node_index) {
  assert(check_node_index(node_index));

  Node n = world.nodes[node_index];

  f64 cx = platform.cursor_x - n.x;
  f64 cy = platform.cursor_y - n.y;

  world.nodes[node_index].hover = cx * cx + cy * cy <= n.radius * n.radius;
}

void update_edge(i64 edge_index) {
  assert(check_edge_index(edge_index));

  Edge e  = world.edges[edge_index];
  Node n0 = world.nodes[e.src];
  Node n1 = world.nodes[e.dst];

  f64 cx = platform.cursor_x - n0.x;
  f64 cy = platform.cursor_y - n0.y;

  // normalized line direction vector
  f64 rx = n1.x - n0.x;
  f64 ry = n1.y - n0.y;
  f64 r  = sqrt(rx * rx + ry * ry);
  if (r >= EPSILON) {
    rx /= r;
    ry /= r;
  }

  // tangent
  f64 tx = -ry;
  f64 ty = rx;

  // distance to the line (dot-product)
  f64 d = cx * tx + cy * ty;

  // distance to the tangent line
  f64 l = cx * rx + cy * ry;

  world.edges[edge_index].hover = d <= e.width / 2 && d >= -e.width / 2 && l >= 0 && l <= r;
}

void remove_edge(i64 edge_index) {
  assert(check_edge_index(edge_index));

  world.edges[edge_index].enabled = 0;
}

void remove_node(i64 node_index) {
  assert(check_node_index(node_index));

  for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
    if (world.edges[i].enabled && (world.edges[i].src == node_index || world.edges[i].dst == node_index))
      remove_edge(i);

  world.nodes[node_index].enabled = 0;
}

void add_node(f64 x, f64 y) {
  i64 node_index = 0;

  while (node_index < MAX_NUM_NODES && world.nodes[node_index].enabled)
    ++node_index;

  assert(node_index < MAX_NUM_NODES);

  world.nodes[node_index] = (Node) {
    .enabled = 1,
    .x       = x,
    .y       = y,
    .radius  = 50,
  };
}

void add_edge(i64 src, i64 dst) {
  assert(check_node_index(src));
  assert(check_node_index(dst));

  if (src == dst)
    return;

  for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
    if (world.edges[i].enabled && world.edges[i].src == src && world.edges[i].dst == dst)
      return;

  i64 edge_index = 0;

  while (edge_index < MAX_NUM_EDGES && world.edges[edge_index].enabled)
    ++edge_index;

  assert(edge_index < MAX_NUM_EDGES);

  world.edges[edge_index] = (Edge) {
    .enabled = 1,
    .src     = src,
    .dst     = dst,
    .width   = 30,
  };
}

i32 main(i32 argc, c8 **argv) {
  (void) argc;
  (void) argv;

  platform = (Platform) {
    .title        = "Graph",
    .frame_width  = 960,
    .frame_height = 720,
  };

  p_init();

  add_node(100, 100);
  add_node(300, 100);
  add_node(120, 300);

  add_edge(0, 1);
  add_edge(0, 2);
  add_edge(1, 2);

  b8  adding_edge = 0;
  i64 adding_src  = 0;
  i64 adding_dst  = 0;

  while (!platform.done) {
    p_wait_events();

    //  Input events

    b8 hover_node = 0;

    for (i64 i = 0; i < MAX_NUM_NODES; ++i)
      if (world.nodes[i].enabled) {
        update_node(i);
        if (world.nodes[i].hover)
          hover_node = 1;
      }

    for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
      if (world.edges[i].enabled) {
        if (hover_node)
          world.edges[i].hover = 0;
        else
          update_edge(i);
      }

    if (platform.key_pressed[KEY_DELETE]) {
      for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
        if (world.edges[i].enabled && world.edges[i].hover)
          remove_edge(i);

      for (i64 i = 0; i < MAX_NUM_NODES; ++i)
        if (world.nodes[i].enabled && world.nodes[i].hover)
          remove_node(i);
    }

    if (platform.key_pressed[BUTTON_LEFT])
      add_node(platform.cursor_x, platform.cursor_y);

    if (platform.key_pressed[BUTTON_RIGHT])
      for (i64 i = 0; i < MAX_NUM_NODES; ++i)
        if (world.nodes[i].enabled && world.nodes[i].hover) {
          adding_edge = 1;
          adding_src  = i;
          adding_src  = i;
          break;
        }

    if (adding_edge)
      for (i64 i = 0; i < MAX_NUM_NODES; ++i)
        if (world.nodes[i].enabled && world.nodes[i].hover) {
          adding_dst = i;
          break;
        }

    if (adding_edge && !platform.key_down[BUTTON_RIGHT]) {
      adding_edge = 0;
      add_edge(adding_src, adding_dst);
    }

    //  Render

    fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width, platform.frame_height);

    if (adding_edge) {
      f64 x0 = world.nodes[adding_src].x;
      f64 y0 = world.nodes[adding_src].y;
      f64 x1 = platform.cursor_x;
      f64 y1 = platform.cursor_y;

      if (adding_src != adding_dst) {
        x1 = world.nodes[adding_dst].x;
        y1 = world.nodes[adding_dst].y;
      }

      fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30);
    }

    draw_graph();

    p_render_frame();
  }  

  p_cleanup();
  return 0;
}