-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.h
154 lines (136 loc) · 3.07 KB
/
grid.h
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
#pragma once
#include <vector> //TODO static allocation
#include <iostream>
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include "cell.h"
class Grid
{
public:
int rows;
int cols;
std::vector<std::vector<Cell>> grid;
Grid(int, int);
void configureCells();
friend std::ostream& operator<<(std::ostream& os, const Cell& c); // TODO what is friend?
void to_png(int);
virtual std::string contentsOf(Cell*);
};
Grid::Grid(int r, int c): rows{r}, cols{c}
{
// TOOD std::size_t i{0U} ? Why?
for (int i = 0; i < rows; i++)
{
std::vector<Cell> newRow;
grid.push_back(newRow);
for (int j = 0; j < cols; j++)
{
grid[i].push_back(Cell(i,j));
}
}
}
void Grid::configureCells()
{
for (int i = 0; i < rows; i++) // TODO improve indices names
{
for (int j = 0; j < cols; j++)
{
if (i > 0)
{
grid[i][j].north = &grid[i - 1][j];
}
if (i < rows - 1)
{
grid[i][j].south = &grid[i + 1][j];
}
if (j > 0)
{
grid[i][j].west = &grid[i][j - 1];
}
if (j < cols - 1)
{
grid[i][j].east = &grid[i][j + 1];
}
}
}
}
std::string Grid::contentsOf(Cell* c)
{
return " ";
}
// TODO cleaner characters (page 46)
std::ostream& operator<<(std::ostream& os, Grid& g)
{
// Top line
os << "+";
for (auto i = 0; i < g.cols; i++)
{
os << "---+";
}
os << std::endl;
for (auto i = 0; i < g.rows; i++)
{
std::string top = "|";
std::string bottom = "+";
for (auto j = 0; j < g.cols; j++)
{
Cell cell = g.grid[i][j];
Cell* pcell = &(g.grid[i][j]);
//std::cout << pcell << std::endl;
std::string body = " " + g.contentsOf(pcell) + " ";
std::string east_boundary = (cell.isLinked(cell.east) ? " " : "|");
top += body + east_boundary;
std::string south_boundary = (cell.isLinked(cell.south) ? " " : "---");
bottom += south_boundary + "+";
}
os << top << std::endl;
os << bottom << std::endl;
}
return os;
}
void Grid::to_png(int cell_size = 10)
{
int img_width = cell_size * cols;
int img_height = cell_size * rows;
cv::Scalar background(255, 255, 255);
cv::Scalar wall(0, 0, 0);
cv::Mat image = cv::Mat(img_height + 1, img_width + 1, CV_8UC3, background);
for (auto i = 0; i < rows; i++)
{
for (auto j = 0; j < cols; j++)
{
auto cell = grid[i][j];
int x1 = cell.col * cell_size;
int y1 = cell.row * cell_size;
int x2 = (cell.col + 1) * cell_size;
int y2 = (cell.row + 1) * cell_size;
if (cell.north == nullptr)
{
auto pt1 = cv::Point(x1, y1);
auto pt2 = cv::Point(x2, y1);
cv::line(image, pt1, pt2, wall);
}
if (cell.west == nullptr)
{
auto pt1 = cv::Point(x1, y1);
auto pt2 = cv::Point(x1, y2);
cv::line(image, pt1, pt2, wall);
}
if (!cell.isLinked(cell.east))
{
auto pt1 = cv::Point(x2, y1);
auto pt2 = cv::Point(x2, y2);
cv::line(image, pt1, pt2, wall);
}
if (!cell.isLinked(cell.south))
{
auto pt1 = cv::Point(x1, y2);
auto pt2 = cv::Point(x2, y2);
cv::line(image, pt1, pt2, wall);
}
}
}
std::cout << cv::imwrite("maze.png", image) << std::endl;
}