Skip to content

Commit e413692

Browse files
committed
removed rounding error. Added code that highlights finish
1 parent 2ce5add commit e413692

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

gui.h

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,44 @@
1616

1717
using namespace cv;
1818

19+
void drawGreenSquare(Mat *img, int x, int y)
20+
{
21+
//draw square
22+
Mat greenSquare(MAZE_WIDTH*PX_PER_UNIT, MAZE_HEIGHT*PX_PER_UNIT, CV_8UC3, Scalar(0,0,0));
23+
rectangle(greenSquare, Point(x*PX_PER_UNIT+PX_PER_UNIT/6, MAZE_HEIGHT_PX-(y*PX_PER_UNIT+PX_PER_UNIT/6)), Point(x*PX_PER_UNIT+PX_PER_UNIT-PX_PER_UNIT/6,MAZE_HEIGHT_PX-(y*PX_PER_UNIT+PX_PER_UNIT-PX_PER_UNIT/6)), CV_RGB(131,255,89), CV_FILLED);
24+
//merge square to frame
25+
*img += greenSquare;
26+
}
27+
28+
void highlightFinish(Mat *img, struct baseMapNode maze[][MAZE_HEIGHT])
29+
{
30+
int found = false;
31+
for(int i=0; i<MAZE_HEIGHT && !found; i++)
32+
{
33+
for(int j=0; j<MAZE_WIDTH && !found; j++)
34+
{
35+
if(maze[i][j].right != NULL)
36+
{
37+
if(maze[i][j].right->top != NULL)
38+
{
39+
if(maze[i][j].right->top->left != NULL)
40+
{
41+
if(maze[i][j].right->top->left->bottom == &(maze[i][j]))
42+
{
43+
found = true;
44+
drawGreenSquare(img, i, j);
45+
drawGreenSquare(img, i, j+1);
46+
drawGreenSquare(img, i+1, j);
47+
drawGreenSquare(img, i+1, j+1);
48+
}
49+
}
50+
}
51+
52+
}
53+
}
54+
}
55+
}
56+
1957
struct callbackWrapper
2058
{
2159
struct baseMapNode (*startNode)[MAZE_HEIGHT];
@@ -145,6 +183,9 @@ void redrawMaze(Mat *img, struct baseMapNode startNode[][MAZE_HEIGHT], struct mo
145183
}
146184
}
147185

186+
//highlight finish
187+
highlightFinish(img, startNode);
188+
148189
//draw mouse
149190
Point tmp[3] = {Point(mouse->posData->x*PX_PER_UNIT-PX_PER_UNIT/2,MAZE_HEIGHT_PX-mouse->posData->y*PX_PER_UNIT+PX_PER_UNIT/2),
150191
Point(mouse->posData->x*PX_PER_UNIT-PX_PER_UNIT/4,MAZE_HEIGHT_PX-mouse->posData->y*PX_PER_UNIT+PX_PER_UNIT/8),
@@ -259,7 +300,7 @@ void mouseCallBackFunc(int event, int x, int y, int flags, void* ptr)
259300
//
260301
if(x%PX_PER_UNIT>=PX_PER_UNIT/2)
261302
{
262-
if((int)(x/PX_PER_UNIT) != MAZE_WIDTH)
303+
if((int)(x/PX_PER_UNIT) != MAZE_WIDTH -1)
263304
{
264305
data->startNode[(int)(x/PX_PER_UNIT)+1][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].wallLeft = true;
265306
data->startNode[(int)(x/PX_PER_UNIT)+1][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].left = NULL;
@@ -301,7 +342,7 @@ void mouseCallBackFunc(int event, int x, int y, int flags, void* ptr)
301342
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].wallTop = true;
302343
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].top = NULL;
303344

304-
if((MAZE_HEIGHT_PX-y)/PX_PER_UNIT != MAZE_HEIGHT)
345+
if(((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1 != MAZE_HEIGHT)
305346
{
306347
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1].wallBottom = true;
307348
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1].bottom = NULL;
@@ -321,7 +362,7 @@ void mouseCallBackFunc(int event, int x, int y, int flags, void* ptr)
321362
//
322363
if(x%PX_PER_UNIT>=PX_PER_UNIT/2)
323364
{
324-
if((int)(x/PX_PER_UNIT) != MAZE_WIDTH)
365+
if((int)(x/PX_PER_UNIT) != MAZE_WIDTH -1)
325366
{
326367
data->startNode[(int)(x/PX_PER_UNIT)+1][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].wallLeft = false;
327368
data->startNode[(int)(x/PX_PER_UNIT)+1][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].left = &(data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)]);
@@ -361,7 +402,7 @@ void mouseCallBackFunc(int event, int x, int y, int flags, void* ptr)
361402
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].wallTop = false;
362403
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].top = &(data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1]);
363404

364-
if((MAZE_HEIGHT_PX-y)/PX_PER_UNIT != MAZE_HEIGHT)
405+
if(((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1 != MAZE_HEIGHT)
365406
{
366407
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1].wallBottom = false;
367408
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)+1].bottom = &(data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)]);

mmSim.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ int main()
109109
std::ostringstream buff ("");
110110

111111
do{
112-
buff << telldir(local) << ": " << dircontents->d_name;
112+
buff << telldir(local) << ": " << dircontents->d_name; //setup output string
113113
cvPrint(&guiText,buff.str());
114114
cout << buff.str() <<endl;
115-
buff.str("");
115+
buff.str("");//clear string buffer for next loop
116116
buff.clear();
117117
dircontents = readdir(local);
118118
}while(dircontents != NULL);
@@ -189,7 +189,7 @@ int main()
189189
{
190190
studentAi(&mouse);
191191
redrawMaze(&image, start, &mouse);
192-
if(waitKey(500) == 'q')
192+
if(waitKey(90) == 'q')
193193
{
194194
break;
195195
}

0 commit comments

Comments
 (0)