summaryrefslogtreecommitdiff
path: root/lib/src/graph.cpp
blob: e5f374e561dbbadf185c543f9a0e9089c7bfd1eb (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

#include "graph.hpp"

graph::graph(unsigned int L) {
  double dx = 1.0 / L;
  unsigned int nv = 2 * pow(L / 2, 2);
  unsigned int ne = pow(L, 2);

  vertices.resize(nv);
  edges.reserve(ne);

  dual_vertices.resize(nv);
  dual_edges.reserve(ne);

  for (unsigned int i = 0; i < nv; i++) {
    vertices[i].r.x = dx * ((1 + i / (L / 2)) % 2 + 2 * (i % (L / 2)));
    vertices[i].r.y = dx * (i / (L / 2));

    dual_vertices[i].r.x = dx * ((i / (L / 2)) % 2 + 2 * (i % (L / 2)));
    dual_vertices[i].r.y = dx * (i / (L / 2));
  }

  for (unsigned int x = 0; x < L; x++) {
    for (unsigned int y = 0; y < L; y++) {
      unsigned int v1 = (L * x) / 2 + ((y + x % 2) / 2) % (L / 2);
      unsigned int v2 = ((L * (x + 1)) / 2 + ((y + (x + 1) % 2) / 2) % (L / 2)) % nv;

      edges.push_back({v1, v2});

      unsigned int dv1 = (L * x) / 2 + ((y + (x + 1) % 2) / 2) % (L / 2);
      unsigned int dv2 = ((L * (x + 1)) / 2 + ((y + x % 2) / 2) % (L / 2)) % nv;

      dual_edges.push_back({dv1, dv2});
    }
  }
}