summaryrefslogtreecommitdiff
path: root/lib/geometry.c
blob: 2c1987ba38a5e0a7a54fd363639abd550b36c3de (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

#include "fracture.h"

int edge_to_verts(unsigned int width, bool periodic, unsigned int edge,
                  bool index) {
  assert(edge < pow(width, 2));

  int x = edge / width + 1;
  int y = edge % width + 1;

  if (periodic) {
    return (((index ^ (x % 2)) + 2 * ((y + (index ^ (!(x % 2)))) / 2) - 1) %
                width +
            (x - index) * width) /
           2;
  } else {
    return ((index ^ (x % 2)) + 2 * ((y + (index ^ (!(x % 2)))) / 2) +
            (x - index) * (width + 1) - 1) /
           2;
  }
}

int dual_edge_to_verts(unsigned int width, bool periodic, unsigned int edge,
                       bool index) {
  assert(edge < pow(width, 2));

  int x = edge / width + 1;
  int y = edge % width + 1;

  if (periodic) {
    return (((index ^ (!(x % 2))) + 2 * ((y + (index ^ (x % 2))) / 2) - 1) %
                width +
            (x - index) * width) /
           2;
  } else {
    return ((index ^ (!(x % 2))) + 2 * ((y + (index ^ (x % 2))) / 2) +
            (x - index) * (width + 1) - 1) /
           2;
  }
}

double dual_vert_to_coord(unsigned int width, bool periodic, unsigned int vert,
                          bool index) {
  if (periodic) {
    if (index)
      return (2 * vert) % width + (2 * vert / width) % 2;
    else
      return 2 * vert / width;
  } else {
    if (index)
      return (2 * vert) % (width + 1);
    else
      return (2 * vert) / (width + 1);
  }
}