Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Structs.h
- struct Point
- {
- int x;
- int y;
- };
- struct Color
- {
- float r;
- float g;
- float b;
- };
- //Grid.h
- #pragma once
- #include "Headers.h"
- class Grid
- {
- private:
- Color color;
- Point point;
- bool obstacle, traversed, start, end;
- Grid* parent;
- public:
- //constructors
- Grid();
- Grid(float, float);
- //getters
- int getX();
- int getY();
- Grid* getParent();
- Color getColor();
- bool isObstacle();
- bool isStartPoint();
- bool isEndPoint();
- bool isTraversed();
- //setters
- void setX();
- void setY();
- void setStartPoint();
- void setEndPoint();
- void setObstacle();
- void setTraversed();
- void setPath();
- };
- //Grid.cpp
- #include "Grid.h"
- Grid::Grid()
- {
- color.r = 0, color.g = 0, color.b = 0;
- point.x = 0, point.y = 0;
- obstacle = false;
- start = false;
- end = false;
- traversed = false;
- parent = NULL;
- }
- Grid::Grid(float x, float y)
- {
- //CO-ORD
- point.x = x;
- point.y = y;
- //OBSTACLE
- this->obstacle = false;
- //COLOR
- this->color.r = this->color.g = this->color.b = 1;
- //PARENT
- parent = NULL;
- }
- int Grid::getX() { return point.x; }
- int Grid::getY() { return point.y; }
- Grid* Grid::getParent() { return parent; }
- Color Grid::getColor() { return this->color; }
- bool Grid::isObstacle() { return obstacle; }
- bool Grid::isStartPoint() { return start; }
- bool Grid::isEndPoint() { return end; }
- bool Grid::isTraversed() { return traversed; }
- void Grid::setStartPoint()
- {
- this->color.r = 0;
- this->color.g = 0;
- this->color.b = 1;
- parent = NULL;
- start = true;
- }
- void Grid::setEndPoint()
- {
- this->color.r = 1;
- this->color.g = 0;
- this->color.b = 0;
- end = true;
- }
- void Grid::setObstacle()
- {
- this->color.r = 0;
- this->color.g = 0;
- this->color.b = 0;
- obstacle = true;
- }
- void Grid::setTraversed()
- {
- this->color.r = 0;
- this->color.g = 1;
- this->color.b = 0;
- traversed = true;
- }
- //main.cpp
- #include "Grid.h"
- //#include "Headers.h"
- void display();
- void init();
- void reshape(int, int);
- void mouseFunc(int, int, int, int);
- void keyboard(unsigned char, int, int);
- //returns screen coordinates
- Point getScreenCoord(int, int);
- //returns the coordinates in grid
- Point getGridCoord(int, int);
- //draw the grid system
- void drawGrid();
- //init grid
- void initGrid();
- const int XMAX = 600;
- const int YMAX = 600;
- const int GRID_SIZE = 20;
- Grid* grid[GRID_SIZE * 2][GRID_SIZE * 2];
- Grid* start, *end;
- int start_x, start_y;
- bool startInit = false;
- bool endInit = false;
- bool pathFound = false;
- bool firstRun = true;
- int findPath(Grid* current,int x, int y)
- {
- if (!pathFound)
- {
- if (current->isObstacle() || current->isTraversed() || current->isStartPoint())
- {
- //do nothing
- std::cout << "obstacle, traversed or start found \n";
- }
- else if (current->isEndPoint())
- {
- pathFound = true;
- std::cout << "end found \n";
- }
- else
- {
- std::cout << "probable path found \n";
- //if (firstRun)
- //{
- // int x = start_x;
- // int y = start_x;
- // firstRun = false;
- //}
- if (x >= 0 && x < (GRID_SIZE*2)-1 && y >= 0 && y < (GRID_SIZE*2)-1)
- {
- current->setTraversed();
- findPath(grid[x + 1][y], x, y); //right
- findPath(grid[x - 1][y], x, y); //left
- findPath(grid[x][y + 1], x, y); //top
- findPath(grid[x][y - 1], x, y); //bottom
- }
- }
- //glutPostRedisplay();
- //Sleep(100);
- }
- else
- {
- //glutPostRedisplay();
- //Sleep(100);
- return 0;
- }
- }
- int main(int argc, char** argv)
- {
- //MessageBox(NULL, L"NO VALID PATH FOUND", L"ERROR!", MB_OK | MB_ICONEXCLAMATION);
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(XMAX, YMAX);
- glutCreateWindow("path demo");
- initGrid();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMouseFunc(mouseFunc);
- glutKeyboardFunc(keyboard);
- init();
- glutMainLoop();
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glPointSize(14); //POINT SIZE
- #pragma region logic
- //findPath(start,start_x,start_y);
- #pragma endregion
- drawGrid();
- glFlush();
- /*
- //print coordinate system
- for (int i = 0; i < (GRID_SIZE * 2)-1; i++)
- {
- for (int j = 0; j < (GRID_SIZE * 2)-1; j++)
- {
- std::cout << "(" << grid[i][j]->getX() << "," << grid[i][j]->getY() << ") ";
- }
- std::cout << "\n";
- }
- */
- }
- void init()
- {
- glClearColor(0.0, 0.0, 0.0, 1.0);
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-GRID_SIZE, GRID_SIZE, -GRID_SIZE, GRID_SIZE);
- //gluOrtho2D(0, -GRID_SIZE *2, 0, -GRID_SIZE * 2);
- glMatrixMode(GL_MODELVIEW);
- }
- void mouseFunc(int button, int state, int mouse_x, int mouse_y)
- {
- //Compute the normalized device space coordinate of the mouse in range [-1, 1]:
- double ndc_x = (double)mouse_x / XMAX * 2.0 - 1.0;
- double ndc_y = 1.0 - (double)mouse_y / YMAX * 2.0;
- //Map the result to the range to the grid range:
- int x = (int)(ndc_x * GRID_SIZE + GRID_SIZE + 0.5) - GRID_SIZE;
- int y = (int)(ndc_y * GRID_SIZE + GRID_SIZE + 0.5) - GRID_SIZE;
- if (state == 0)
- {
- std::cout << "button:" << button << "\nstate:" << state << "\n(x,y):(" << x << "," << y << ")\n";
- std::cout << "ON SCREEN (" << getScreenCoord(x, y).x << ", " << getScreenCoord(x, y).y << ")\n-----------\n";
- }
- //setting params
- x = getScreenCoord(x, y).x;
- y = getScreenCoord(x, y).y;
- if (button == 0 && state == 0)
- {
- if (!startInit)
- {
- grid[x][y]->setStartPoint();
- start_x = x;
- start_y = y;
- startInit = true;
- start = grid[x][y];
- }
- else if (!endInit)
- {
- grid[x][y]->setEndPoint();
- endInit = true;
- end = grid[x][y];
- }
- }
- else if (button == 2 && state == 0)
- {
- std::cout << " start:" << grid[x][y]->isStartPoint() << "\n";
- std::cout << " end:" << grid[x][y]->isEndPoint() << "\n";
- if (grid[x][y]->isStartPoint() || grid[x][y]->isEndPoint()){}
- else { grid[x][y]->setObstacle(); }
- }
- }
- void keyboard(unsigned char key, int x, int y)
- {
- findPath(start, start_x, start_y);
- }
- Point getScreenCoord(int x, int y)
- {
- Point point;
- point.x = x + GRID_SIZE - 1;
- point.y = y - GRID_SIZE + 1;
- if (point.x < 0) point.x *= -1;
- if (point.y < 0) point.y *= -1;
- return point;
- }
- Point getGridCoord(int x, int y)
- {
- Point point;
- return point;
- }
- void drawGrid()
- {
- float grid_x = GRID_SIZE - 1;
- float grid_y = GRID_SIZE - 1;
- for (int i = 0; i < (GRID_SIZE * 2) - 1; i++)
- {
- for (int j = 0; j < (GRID_SIZE * 2) - 1; j++)
- {
- glBegin(GL_POINTS);
- glColor3f(grid[i][j]->getColor().r, grid[i][j]->getColor().g, grid[i][j]->getColor().b);
- glVertex2f(grid[i][j]->getX(), grid[i][j]->getY());
- glEnd();
- }
- }
- }
- void initGrid()
- {
- float grid_x = GRID_SIZE - 1;
- float grid_y = GRID_SIZE - 1;
- int i, j;
- //initializing the grid
- /* old code
- for (float y = grid_y, i = 0; y > -GRID_SIZE; y--, i++)
- {
- for (float x = -grid_x, j = 0; x < GRID_SIZE; x++, j++)
- {
- grid[(int)i][(int)j] = new Grid(x, y);
- }
- }
- */
- for (float x = -grid_x, i = 0; x < GRID_SIZE; x++, i++)
- {
- for (float y = grid_y, j = 0; y > -GRID_SIZE; y--, j++)
- {
- grid[(int)i][(int)j] = new Grid(x, y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment