Walls And Holes  1
array2dtools.h
Go to the documentation of this file.
1 #ifndef ARRAY2DTOOLS_H
2 #define ARRAY2DTOOLS_H
3 
4 #include <QSize>
5 #include <QSet>
6 #include <QPoint>
7 
8 // QPoints are not hashable in Qt by default.
9 inline uint qHash (const QPoint & key)
10 {
11  return qHash (QPair<int,int>(key.x(), key.y()) );
12 }
13 
17 bool isPointInBounds(int x, int y, int width, int height)
18 {
19  return x >= 0 && x < width && y >= 0 && y < height;
20 }
21 
22 bool isPointInBounds(int x, int y, QSize size)
23 {
24  return isPointInBounds(x, y, size.width(), size.height());
25 }
26 
27 
28 bool isPointInBounds(QPoint pt, QSize size)
29 {
30  return isPointInBounds(pt.x(), pt.y(), size.width(), size.height());
31 }
32 
33 
37 QSet<QPoint> getValidNeighbors(int x, int y, int width, int height)
38 {
39  QSet<QPoint> neighbors;
40 
41  for (int dx = -1; dx <= 1; ++dx)
42  for (int dy = -1; dy <= 1; ++dy)
43  if (!(dx == 0 && dy == 0) && isPointInBounds(x+dx, y+dy, width, height))
44  neighbors.insert({x+dx, y+dy});
45 
46  return neighbors;
47 }
48 
49 QSet<QPoint> getValidNeighbors(int x, int y, QSize size)
50 {
51  return getValidNeighbors(x, y, size.width(), size.height());
52 }
53 
54 QSet<QPoint> getValidNeighbors(QPoint pt, QSize size)
55 {
56  return getValidNeighbors(pt.x(), pt.y(), size.width(), size.height());
57 }
58 
59 
60 #endif // ARRAY2DTOOLS_H
uint qHash(const QPoint &key)
Definition: array2dtools.h:9
QSet< QPoint > getValidNeighbors(int x, int y, int width, int height)
Returns the coordinates of all (up to 8) in-bounds neighbors of the point.
Definition: array2dtools.h:37
bool isPointInBounds(int x, int y, int width, int height)
Returns true if the point is in bounds, false otherwise.
Definition: array2dtools.h:17