Guest User

Path Find

a guest
Oct 1st, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.20 KB | None | 0 0
  1. //Structs.h
  2. struct Point
  3. {
  4.     int x;
  5.     int y;
  6. };
  7. struct Color
  8. {
  9.     float r;
  10.     float g;
  11.     float b;
  12. };
  13.  
  14.  
  15.  
  16. //Grid.h
  17. #pragma once
  18. #include "Headers.h"
  19.  
  20. class Grid
  21. {
  22. private:
  23.     Color color;
  24.     Point point;
  25.     bool obstacle, traversed, start, end;
  26.     Grid* parent;
  27.  
  28.  
  29. public:
  30.     //constructors
  31.     Grid();
  32.     Grid(float, float);
  33.    
  34.     //getters
  35.     int getX();
  36.     int getY();
  37.     Grid* getParent();
  38.     Color getColor();
  39.     bool isObstacle();
  40.     bool isStartPoint();
  41.     bool isEndPoint();
  42.     bool isTraversed();
  43.  
  44.  
  45.     //setters
  46.     void setX();
  47.     void setY();
  48.     void setStartPoint();
  49.     void setEndPoint();
  50.     void setObstacle();
  51.     void setTraversed();
  52.     void setPath();
  53. };
  54.  
  55. //Grid.cpp
  56. #include "Grid.h"
  57.  
  58. Grid::Grid()
  59. {
  60.     color.r = 0, color.g = 0, color.b = 0;
  61.     point.x = 0, point.y = 0;
  62.     obstacle = false;
  63.     start = false;
  64.     end = false;
  65.     traversed = false;
  66.     parent = NULL;
  67. }
  68.  
  69. Grid::Grid(float x, float y)
  70. {
  71.     //CO-ORD
  72.     point.x = x;
  73.     point.y = y;
  74.  
  75.     //OBSTACLE
  76.     this->obstacle = false;
  77.  
  78.     //COLOR
  79.     this->color.r = this->color.g = this->color.b = 1;
  80.  
  81.     //PARENT
  82.     parent = NULL;
  83. }
  84.  
  85. int Grid::getX() { return point.x; }
  86. int Grid::getY() { return point.y; }
  87.  
  88. Grid* Grid::getParent() { return parent; }
  89.  
  90. Color Grid::getColor() { return this->color; }
  91.  
  92. bool Grid::isObstacle() { return obstacle; }
  93.  
  94. bool Grid::isStartPoint() { return start; }
  95.  
  96. bool Grid::isEndPoint() { return end; }
  97.  
  98. bool Grid::isTraversed() { return traversed; }
  99.  
  100. void Grid::setStartPoint()
  101. {
  102.     this->color.r = 0;
  103.     this->color.g = 0;
  104.     this->color.b = 1;
  105.     parent = NULL;
  106.     start = true;
  107. }
  108.  
  109. void Grid::setEndPoint()
  110. {
  111.     this->color.r = 1;
  112.     this->color.g = 0;
  113.     this->color.b = 0;
  114.     end = true;
  115. }
  116.  
  117. void Grid::setObstacle()
  118. {
  119.     this->color.r = 0;
  120.     this->color.g = 0;
  121.     this->color.b = 0;
  122.     obstacle = true;
  123. }
  124.  
  125. void Grid::setTraversed()
  126. {
  127.     this->color.r = 0;
  128.     this->color.g = 1;
  129.     this->color.b = 0;
  130.     traversed = true;
  131. }
  132.  
  133.  
  134. //main.cpp
  135. #include "Grid.h"
  136. //#include "Headers.h"
  137.  
  138. void display();
  139. void init();
  140. void reshape(int, int);
  141. void mouseFunc(int, int, int, int);
  142. void keyboard(unsigned char, int, int);
  143.  
  144. //returns screen coordinates
  145. Point getScreenCoord(int, int);
  146. //returns the coordinates in grid
  147. Point getGridCoord(int, int);
  148. //draw the grid system
  149. void drawGrid();
  150. //init grid
  151. void initGrid();
  152.  
  153.  
  154. const int XMAX = 600;
  155. const int YMAX = 600;
  156. const int GRID_SIZE = 20;
  157.  
  158. Grid* grid[GRID_SIZE * 2][GRID_SIZE * 2];
  159. Grid* start, *end;
  160. int start_x, start_y;
  161. bool startInit = false;
  162. bool endInit = false;
  163. bool pathFound = false;
  164. bool firstRun = true;
  165.  
  166. int findPath(Grid* current,int x, int y)
  167. {
  168.     if (!pathFound)
  169.     {
  170.         if (current->isObstacle() || current->isTraversed() || current->isStartPoint())
  171.         {
  172.             //do nothing
  173.             std::cout << "obstacle, traversed or start found \n";
  174.         }
  175.         else if (current->isEndPoint())
  176.         {
  177.             pathFound = true;
  178.             std::cout << "end found \n";
  179.         }
  180.         else
  181.         {
  182.             std::cout << "probable path found \n";
  183.             //if (firstRun)
  184.             //{
  185.             //  int x = start_x;
  186.             //  int y = start_x;
  187.             //  firstRun = false;
  188.             //}
  189.             if (x >= 0 && x < (GRID_SIZE*2)-1 && y >= 0 && y < (GRID_SIZE*2)-1)
  190.             {
  191.                 current->setTraversed();
  192.  
  193.                 findPath(grid[x + 1][y], x, y); //right
  194.                 findPath(grid[x - 1][y], x, y); //left
  195.                 findPath(grid[x][y + 1], x, y); //top
  196.                 findPath(grid[x][y - 1], x, y); //bottom
  197.             }
  198.         }
  199.         //glutPostRedisplay();
  200.         //Sleep(100);
  201.     }
  202.     else
  203.     {
  204.         //glutPostRedisplay();
  205.         //Sleep(100);
  206.         return 0;
  207.     }
  208.    
  209. }
  210.  
  211. int main(int argc, char** argv)
  212. {
  213.     //MessageBox(NULL, L"NO VALID PATH FOUND", L"ERROR!", MB_OK | MB_ICONEXCLAMATION);
  214.     glutInit(&argc, argv);
  215.     glutInitDisplayMode(GLUT_RGB);
  216.     glutInitWindowPosition(100,100);
  217.     glutInitWindowSize(XMAX, YMAX);
  218.     glutCreateWindow("path demo");
  219.  
  220.     initGrid();
  221.  
  222.     glutDisplayFunc(display);
  223.     glutReshapeFunc(reshape);
  224.     glutMouseFunc(mouseFunc);
  225.     glutKeyboardFunc(keyboard);
  226.     init();
  227.     glutMainLoop();
  228. }
  229.  
  230. void display()
  231. {
  232.     glClear(GL_COLOR_BUFFER_BIT);
  233.     glLoadIdentity();
  234.     glPointSize(14);            //POINT SIZE
  235.  
  236. #pragma region logic
  237.     //findPath(start,start_x,start_y);
  238. #pragma endregion
  239.  
  240.     drawGrid();
  241.     glFlush();
  242.     /*
  243.     //print coordinate system
  244.     for (int i = 0; i < (GRID_SIZE * 2)-1; i++)
  245.     {
  246.         for (int j = 0; j < (GRID_SIZE * 2)-1; j++)
  247.         {
  248.             std::cout << "(" << grid[i][j]->getX() << "," << grid[i][j]->getY() << ") ";
  249.         }
  250.         std::cout << "\n";
  251.     }
  252.     */
  253. }
  254.  
  255. void init()
  256. {
  257.     glClearColor(0.0, 0.0, 0.0, 1.0);
  258. }
  259.  
  260. void reshape(int w, int h)
  261. {
  262.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  263.     glMatrixMode(GL_PROJECTION);
  264.     glLoadIdentity();
  265.     gluOrtho2D(-GRID_SIZE, GRID_SIZE, -GRID_SIZE, GRID_SIZE);
  266.     //gluOrtho2D(0, -GRID_SIZE *2, 0, -GRID_SIZE * 2);
  267.     glMatrixMode(GL_MODELVIEW);
  268. }
  269.  
  270. void mouseFunc(int button, int state, int mouse_x, int mouse_y)
  271. {
  272.     //Compute the normalized device space coordinate of the mouse in range [-1, 1]:
  273.     double ndc_x = (double)mouse_x / XMAX * 2.0 - 1.0;
  274.     double ndc_y = 1.0 - (double)mouse_y / YMAX * 2.0;
  275.  
  276.     //Map the result to the range to the grid range:
  277.     int x = (int)(ndc_x * GRID_SIZE + GRID_SIZE + 0.5) - GRID_SIZE;
  278.     int y = (int)(ndc_y * GRID_SIZE + GRID_SIZE + 0.5) - GRID_SIZE;
  279.     if (state == 0)
  280.     {
  281.         std::cout << "button:" << button << "\nstate:" << state << "\n(x,y):(" << x << "," << y << ")\n";
  282.         std::cout << "ON SCREEN (" << getScreenCoord(x, y).x << ", " << getScreenCoord(x, y).y << ")\n-----------\n";
  283.     }
  284.  
  285.     //setting params
  286.     x = getScreenCoord(x, y).x;
  287.     y = getScreenCoord(x, y).y;
  288.     if (button == 0 && state == 0)
  289.     {
  290.         if (!startInit)
  291.         {
  292.             grid[x][y]->setStartPoint();
  293.             start_x = x;
  294.             start_y = y;
  295.             startInit = true;
  296.             start = grid[x][y];
  297.         }
  298.         else if (!endInit)
  299.         {
  300.             grid[x][y]->setEndPoint();
  301.             endInit = true;
  302.             end = grid[x][y];
  303.         }
  304.     }
  305.     else if (button == 2 && state == 0)
  306.     {
  307.         std::cout << " start:" << grid[x][y]->isStartPoint() << "\n";
  308.         std::cout << " end:" << grid[x][y]->isEndPoint() << "\n";
  309.  
  310.         if (grid[x][y]->isStartPoint() || grid[x][y]->isEndPoint()){}
  311.         else { grid[x][y]->setObstacle(); }
  312.     }
  313. }
  314.  
  315. void keyboard(unsigned char key, int x, int y)
  316. {
  317.     findPath(start, start_x, start_y);
  318. }
  319.  
  320. Point getScreenCoord(int x, int y)
  321. {
  322.     Point point;
  323.  
  324.     point.x = x + GRID_SIZE - 1;
  325.     point.y = y - GRID_SIZE + 1;
  326.  
  327.     if (point.x < 0) point.x *= -1;
  328.     if (point.y < 0) point.y *= -1;
  329.  
  330.     return point;
  331. }
  332.  
  333. Point getGridCoord(int x, int y)
  334. {
  335.     Point point;
  336.  
  337.  
  338.     return point;
  339. }
  340.  
  341. void drawGrid()
  342. {
  343.     float grid_x = GRID_SIZE - 1;
  344.     float grid_y = GRID_SIZE - 1;
  345.  
  346.     for (int i = 0; i < (GRID_SIZE * 2) - 1; i++)
  347.     {
  348.         for (int j = 0; j < (GRID_SIZE * 2) - 1; j++)
  349.         {
  350.             glBegin(GL_POINTS);
  351.  
  352.             glColor3f(grid[i][j]->getColor().r, grid[i][j]->getColor().g, grid[i][j]->getColor().b);
  353.             glVertex2f(grid[i][j]->getX(), grid[i][j]->getY());
  354.  
  355.             glEnd();
  356.         }
  357.     }
  358. }
  359.  
  360. void initGrid()
  361. {
  362.     float grid_x = GRID_SIZE - 1;
  363.     float grid_y = GRID_SIZE - 1;
  364.     int i, j;
  365.  
  366.     //initializing the grid
  367.     /* old code
  368.     for (float y = grid_y, i = 0; y > -GRID_SIZE; y--, i++)
  369.     {
  370.         for (float x = -grid_x, j = 0; x < GRID_SIZE; x++, j++)
  371.         {
  372.             grid[(int)i][(int)j] = new Grid(x, y);
  373.         }
  374.     }
  375.     */
  376.     for (float x = -grid_x, i = 0; x < GRID_SIZE; x++, i++)
  377.     {
  378.         for (float y = grid_y, j = 0; y > -GRID_SIZE; y--, j++)
  379.         {
  380.             grid[(int)i][(int)j] = new Grid(x, y);
  381.         }
  382.     }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment