StepByStep_Dijkstra_Maps.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #define MAX_PATH 1048
  2. #define MAPWIDTH 15
  3. #define MAPHEIGHT 15
  4. #include "raylib.h"
  5. #include <math.h>
  6. typedef struct pathnode{
  7. int x;
  8. int y;
  9. }pathnode;
  10. static struct pathnode list[MAX_PATH];
  11. static int map[MAPWIDTH][MAPHEIGHT];
  12. static int mapWidth = MAPWIDTH;
  13. static int mapHeight = MAPHEIGHT;
  14. static float tileWidth ;
  15. static float tileHeight;
  16. static int startx;
  17. static int starty;
  18. static int endx;
  19. static int endy;
  20. static int listlen=0;
  21. static bool startfound=false;
  22. static void newmap(void);
  23. static void animatedijkstramap(void);
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib example.");
  31. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  32. //--------------------------------------------------------------------------------------
  33. newmap();
  34. tileWidth = abs((float)screenWidth/(float)mapWidth);
  35. tileHeight = abs((float)screenHeight/(float)mapHeight);
  36. listlen = 0;
  37. map[startx][starty]=1;
  38. list[listlen].x=startx;
  39. list[listlen].y=starty;
  40. listlen+=1;
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. if(IsKeyPressed(KEY_SPACE)){
  47. if(startfound==false){
  48. animatedijkstramap();
  49. }else{
  50. startfound=false;
  51. newmap();
  52. listlen = 0;
  53. map[startx][starty]=1;
  54. list[listlen].x=startx;
  55. list[listlen].y=starty;
  56. listlen+=1;
  57. }
  58. }
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. for(int y=0;y<mapHeight;y++){
  65. for(int x=0;x<mapWidth;x++){
  66. if(map[x][y]<0){
  67. DrawRectangleGradientEx((Rectangle){x*tileWidth,y*tileHeight,tileWidth,tileHeight},BLACK,GRAY,WHITE,WHITE);
  68. }else if(map[x][y]>0){
  69. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLUE);
  70. DrawText(FormatText("%i",map[x][y]),x*tileWidth,y*tileHeight,30,YELLOW);
  71. }else{
  72. DrawText(FormatText("%i",map[x][y]),x*tileWidth,y*tileHeight,30,BLACK);
  73. }
  74. }
  75. }
  76. DrawRectangle(startx*tileWidth,starty*tileHeight,tileWidth,tileHeight,(Color){0,200,0,200});
  77. DrawRectangle(endx*tileWidth,endy*tileHeight,tileWidth,tileHeight,(Color){200,0,0,100});
  78. if(startfound){
  79. DrawRectangle(0,0,screenWidth,60,(Color){10,10,10,200});
  80. DrawText("Start Position Found!.",0,0,50,(Color){255,255,255,200});
  81. }else{
  82. DrawRectangle(0,0,screenWidth,20,(Color){100,100,100,200});
  83. DrawText("Press space to search.",0,0,20,(Color){255,255,255,255});
  84. }
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }
  94. void animatedijkstramap(){
  95. //
  96. // Flood the map with distances from the start.
  97. //
  98. if(listlen==0)return;
  99. // int failed=0;
  100. // 4 way search! left/up/down/right
  101. int dx[4]={ 0,1,0,-1};
  102. int dy[4]={-1,0,1,0};
  103. // While we have a list to work with
  104. // while(listlen>0){
  105. // Take the first value from the array.
  106. int x1=list[0].x;
  107. int y1=list[0].y;
  108. // shift all up.
  109. for(int i=0;i<listlen;i++){
  110. list[i].x = list[i+1].x;
  111. list[i].y = list[i+1].y;
  112. }
  113. if(x1==endx && y1==endy){
  114. listlen=0;
  115. startfound=true;
  116. return;
  117. }
  118. // Decrease list length
  119. listlen-=1;
  120. //
  121. // Here we check around our current position.
  122. for(int i=0;i<4;i++){
  123. int nx = x1+dx[i];
  124. int ny = y1+dy[i];
  125. if(nx<0 || ny<0 || nx>= mapWidth || ny>= mapHeight)continue;
  126. // If we can get there then put the new distance there and add this position
  127. // to the list.
  128. if(map[nx][ny]==0){
  129. map[nx][ny]=map[x1][y1]+1;
  130. // add to last
  131. //
  132. list[listlen].x = nx;
  133. list[listlen].y = ny;
  134. listlen++;
  135. //
  136. }
  137. }
  138. // Error?
  139. // failed+=1;
  140. // if(failed>160000)return;
  141. // }
  142. }
  143. void newmap(){
  144. //
  145. // Generate random map....
  146. //
  147. // First make sure every map value is 0.
  148. for(int y=0;y<mapHeight;y++){
  149. for(int x=0;x<mapWidth;x++){
  150. map[x][y] = 0;
  151. }}
  152. // Draw some lines on the map.
  153. for(int x=0;x< mapWidth;x+=5){
  154. if(GetRandomValue(0,3)<2){
  155. for(int y=2;y< mapHeight-2;y++){
  156. map[x][y] = -1;
  157. }
  158. }
  159. }
  160. //
  161. //draw some rectangles on the map
  162. for(int i=0;i<mapWidth;i++){
  163. int x1=GetRandomValue(0,mapWidth-5);
  164. int y1=GetRandomValue(0,mapHeight-5);
  165. for(int x2=0;x2<4;x2++){
  166. for(int y2=0;y2<4;y2++){
  167. map[x1+x2][y1+y2] = -1;
  168. }}
  169. }
  170. // Draw some passages on the map.
  171. for(int y=0;y<mapHeight;y+=5){
  172. for(int x=0;x<mapWidth;x++){
  173. map[x][y] = 0;
  174. }}
  175. //
  176. // find a start and end location.
  177. //
  178. bool found=false;
  179. int failed=0;
  180. while(found==false){
  181. found = true;
  182. startx = GetRandomValue(1,mapWidth-1);
  183. starty = GetRandomValue(1,mapHeight-1);
  184. endx = GetRandomValue(1,mapWidth-1);
  185. endy = GetRandomValue(1,mapHeight-1);
  186. // If below any start or end position than try again.
  187. if(map[startx][starty]!=0 || map[endx][endy]!=0)found=false;
  188. failed+=1;
  189. if(failed>500000)return; // If we just can not find any start and end position then exit.
  190. }
  191. }