sKit  0.0.9
grids.cpp
Go to the documentation of this file.
1 #include "grids.hpp"
2 
3 #include <format>
4 #include <stdexcept>
5 
6 namespace sKit {
7 namespace structure {
8 auto gridFillDiagonal(int numCols, int numRows) -> std::vector<glm::ivec2> {
9  if (numCols < 2) {
10  throw std::runtime_error(
11  std::format("{}: Grids must have atleast 2 columns!", __func__)
12  );
13  }
14  if (numRows < 2) {
15  throw std::runtime_error(
16  std::format("{}: Grids must have atleast 2 rows!", __func__)
17  );
18  }
19 
20  std::vector<glm::ivec2> path = {{0, 0}, {1, 0}, {0, 1}};
21  glm::ivec2 pos(0, 1);
22  glm::ivec2 dir = SW;
23  glm::ivec2 prevDir = SW;
24  const glm::ivec2 start(0, 0);
25  const glm::ivec2 end(numCols - 1, numRows - 1);
26  const glm::ivec2 bottomLeft(start.x, end.y);
27  const glm::ivec2 topRight(end.x, start.y);
28  bool atPathEnd = false;
29  while (!atPathEnd) {
30  if (pos == end) {
31  atPathEnd = true;
32  break;
33  }
34  if (pos == bottomLeft) {
35  dir = prevDir == SW ? E : NE;
36  pos += dir;
37  path.push_back(pos);
38  prevDir = dir;
39  continue;
40  }
41  if (pos == topRight) {
42  dir = prevDir == NE ? S : SW;
43  pos += dir;
44  path.push_back(pos);
45  prevDir = dir;
46  continue;
47  }
48  glm::ivec2 nextPos = pos + dir;
49  bool outOfBounds = nextPos.x < 0 || nextPos.x >= numCols || nextPos.y < 0 ||
50  nextPos.y >= numRows;
51  if (outOfBounds) {
52  dir = prevDir == SW ? S : pos.y == 0 ? E : S;
53  if (prevDir == SW) {
54  if (pos.x == 0) {
55  dir = S;
56  } else {
57  dir = E;
58  }
59  } else if (prevDir == NE) {
60  if (pos.y == 0) {
61  dir = E;
62  } else {
63  dir = S;
64  }
65  }
66 
67  pos += dir;
68  path.push_back(pos);
69  prevDir = dir;
70  continue;
71  }
72  if (prevDir == S) {
73  dir = pos.x == 0 ? NE : SW;
74  pos += dir;
75  path.push_back(pos);
76  prevDir = dir;
77  continue;
78  }
79  if (prevDir == E) {
80  dir = pos.y == 0 ? SW : NE;
81  pos += dir;
82  path.push_back(pos);
83  prevDir = dir;
84  continue;
85  }
86  pos += dir;
87  path.push_back(pos);
88  prevDir = dir;
89  }
90 
91  return path;
92 }
93 
94 } // namespace structure
95 } // namespace sKit
grids.hpp
sKit::structure::NE
constexpr glm::ivec2 NE(1, -1)
Unit int vector North-East.
sKit::structure::S
constexpr glm::ivec2 S(0, 1)
Unit int vector South.
sKit::structure::gridFillDiagonal
auto gridFillDiagonal(int numCols, int numRows) -> std::vector< glm::ivec2 >
Creates a path following the coordinates of every cell in a grid that has a diagonal tendency.
Definition: grids.cpp:8
sKit::structure::E
constexpr glm::ivec2 E(1, 0)
Unit int vector East.
sKit
Definition: camera.hpp:8
sKit::structure::SW
constexpr glm::ivec2 SW(-1, 1)
Unit int vector South-West.