-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcGrid.hh
108 lines (95 loc) · 2.14 KB
/
cGrid.hh
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
#pragma once
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "cImage.hh"
/**
* @brief class to hold and modify the grid
*
*/
class cGrid
{
public:
int m_width; // width of the grid
int m_height; // height of the grid
int *m_grid; // the grid
int m_bMinX; // bounding box min x
int m_bMinY; // bounding box min y
int m_bMaxX; // bounding box max x
int m_bMaxY; // bounding box max y
cGrid(int t_w, int t_h);
~cGrid();
/**
* @brief convert x,y to index
*
* @param t_x
* @param t_y
* @return int
*/
inline int xy2i(int t_x, int t_y)
{
return t_y * m_width + t_x;
}
/**
* @brief set the value v at x,y
*
* @param t_x
* @param t_y
* @param t_v
*/
inline void set(int t_x, int t_y, int t_v)
{
if (t_x < 0 || t_x > m_width - 1 || t_y < 0 || t_y > m_height - 1)
return;
m_grid[xy2i(t_x, t_y)] = t_v;
}
/**
* @brief get the value at x,y
*
* @param t_x
* @param t_y
* @return int
*/
inline int get(int t_x, int t_y)
{
if (t_x < 0 || t_x > m_width - 1 || t_y < 0 || t_y > m_height - 1)
return 0;
return m_grid[xy2i(t_x, t_y)];
}
/**
* @brief resize the current grid
*
* @param t_x new width
* @param t_y new height
*/
inline void resize(int t_x, int t_y)
{
int diffX = t_x - m_width;
int diffY = t_y - m_height;
int *newgrid = new int[t_x * t_y];
for (int i = 0; i < t_x * t_y; i++)
{
newgrid[i] = 0;
}
for (int i = 0; i < m_width; i++)
{
int ii = i + diffX / 2;
for (int j = 0; j < m_height; j++)
{
int jj = j + diffY / 2;
newgrid[jj * t_x + ii] = m_grid[j * m_width + i];
}
}
delete[] m_grid;
m_grid = newgrid;
m_width = t_x;
m_height = t_y;
}
int getWidth();
int getHeight();
};