/
MazePainter.cpp
49 lines (40 loc) · 1.57 KB
/
MazePainter.cpp
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
#include "MazePainter.h"
void MazePainter::paint(MazeModel *pModel, QPainter &painter) {
if (!pModel->hasMaze())
return;
QRect rSize = painter.viewport();
int nMazeWidth = pModel->width();
int nMazeHeight = pModel->height();
float nCellSize = std::min((float)rSize.width()/nMazeWidth,
(float)rSize.height()/nMazeHeight);
//draw the big left and top walls of the maze
QLineF wall;
wall.setP1(QPointF(0, 0));
wall.setP2(QPointF(nCellSize, 0));
for (int col = 0; col < nMazeWidth; col++) {
if (pModel->cellWalls(col, 0) & MazeModel::UP)
painter.drawLine(wall);
wall.translate(nCellSize, 0);
}
wall.setP1(QPointF(0, 0));
wall.setP2(QPointF(0, nCellSize));
for (int row = 0; row < nMazeHeight; row++) {
if (pModel->cellWalls(0, row) & MazeModel::LEFT)
painter.drawLine(wall);
wall.translate(0, nCellSize);
}
//draw the rest of the maze's walls
for (int row = 0; row < nMazeHeight; row++) {
QLineF wallDown(0, (row+1)*nCellSize, nCellSize, (row+1)*nCellSize);
QLineF wallRight(nCellSize, row*nCellSize, nCellSize, (row+1)*nCellSize);
for (int col = 0; col < nMazeWidth; col++) {
int walls = pModel->cellWalls(col, row);
if (walls & MazeModel::RIGHT)
painter.drawLine(wallRight);
if (walls & MazeModel::DOWN)
painter.drawLine(wallDown);
wallDown.translate(nCellSize, 0);
wallRight.translate(nCellSize, 0);
}
}
}