Simulation von Bürgern
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathFinding.pde 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import pathfinder.*;
  2. Graph gs;
  3. GraphNode[] gNodes;
  4. GraphEdge[] gEdges;
  5. float nodeSize;
  6. void initPathFinding() {
  7. //generate a public path finding graph and get nodemap
  8. img_nodes = createImage(img_houses.width, img_houses.height, ARGB);
  9. nodeSize = 5.0f;
  10. gs = new Graph();
  11. makeGraphFromImage(gs, img_houses, int(img_houses.width * f_nodeResolution), int(img_houses.height * f_nodeResolution), true);
  12. gNodes = gs.getNodeArray();
  13. gs.compact();
  14. gNodes = gs.getNodeArray();
  15. gEdges = gs.getAllEdgeArray();
  16. }
  17. //void mousePressed() {
  18. // startNode = gs.getNodeAt(mouseX, mouseY, 0, 10.0f);
  19. // if (startNode != null)
  20. // selectMode = true;
  21. //}
  22. //void mouseDragged() {
  23. // if (selectMode)
  24. // endNode = gs.getNodeAt(mouseX, mouseY, 0, 10.0f);
  25. //}
  26. //void mouseReleased() {
  27. // if (selectMode && endNode!= null && startNode != null && startNode != endNode) {
  28. // start = startNode.id();
  29. // end = endNode.id();
  30. // usePathFinder(pathFinder);
  31. // }
  32. // selectMode = false;
  33. // startNode = endNode = null;
  34. //}
  35. PImage img_nodes;
  36. void makeGraphFromImage(Graph g, PImage map, int tilesX, int tilesY, boolean allowDiagonals) {
  37. img_nodes.loadPixels();
  38. for (int i = 0; i < img_nodes.pixels.length; i++) {
  39. if (map.pixels[i] != -1) {
  40. img_nodes.pixels[i] = int(color(0));
  41. } else {
  42. img_nodes.pixels[i] = int(color(255));
  43. }
  44. }
  45. img_nodes.updatePixels();
  46. int dx = (img_nodes.width / tilesX) +1;
  47. int dy = (img_nodes.height / tilesY) +1;
  48. int sx = dx / 2, sy = dy / 2;
  49. // use deltaX to avoid horizontal wrap around edges
  50. int deltaX = tilesX + 1; // must be > tilesX
  51. float hCost = dx, vCost = dy, dCost = sqrt(dx*dx + dy*dy);
  52. float cost = 0;
  53. int px, py, nodeID, col;
  54. GraphNode aNode;
  55. py = sy;
  56. for (int y = 0; y < tilesY; y++) {
  57. nodeID = deltaX * y + deltaX;
  58. px = sx;
  59. for (int x = 0; x < tilesX; x++) {
  60. // Calculate the cost
  61. col = img_nodes.get(px, py) & 0xFF;
  62. cost = 1;
  63. // If col is not black then create the node and edges
  64. if (col != 0) {
  65. aNode = new GraphNode(nodeID, px, py);
  66. g.addNode(aNode);
  67. if (x > 0) {
  68. g.addEdge(nodeID, nodeID - 1, hCost * cost);
  69. if (allowDiagonals) {
  70. g.addEdge(nodeID, nodeID - deltaX - 1, dCost * cost);
  71. g.addEdge(nodeID, nodeID + deltaX - 1, dCost * cost);
  72. }
  73. }
  74. if (x < tilesX -1) {
  75. g.addEdge(nodeID, nodeID + 1, hCost * cost);
  76. if (allowDiagonals) {
  77. g.addEdge(nodeID, nodeID - deltaX + 1, dCost * cost);
  78. g.addEdge(nodeID, nodeID + deltaX + 1, dCost * cost);
  79. }
  80. }
  81. if (y > 0)
  82. g.addEdge(nodeID, nodeID - deltaX, vCost * cost);
  83. if (y < tilesY - 1)
  84. g.addEdge(nodeID, nodeID + deltaX, vCost * cost);
  85. }
  86. px += dx;
  87. nodeID++;
  88. }
  89. py += dy;
  90. }
  91. }
  92. IGraphSearch makePathFinder(Graph graph, int pathFinder) {
  93. IGraphSearch pf = null;
  94. float f = 1.0f;
  95. switch(pathFinder) {
  96. case 0:
  97. pf = new GraphSearch_DFS(gs);
  98. break;
  99. case 1:
  100. pf = new GraphSearch_BFS(gs);
  101. break;
  102. case 2:
  103. pf = new GraphSearch_Dijkstra(gs);
  104. break;
  105. case 3:
  106. pf = new GraphSearch_Astar(gs, new AshCrowFlight(f));
  107. break;
  108. case 4:
  109. pf = new GraphSearch_Astar(gs, new AshManhattan(f));
  110. break;
  111. }
  112. return pf;
  113. }
  114. void drawNodes(){
  115. pg_map.pushStyle();
  116. pg_map.noStroke();
  117. pg_map.fill(255,0,255,72);
  118. for(GraphNode node : gNodes)
  119. pg_map.ellipse(node.xf(), node.yf(), nodeSize, nodeSize);
  120. pg_map.popStyle();
  121. }
  122. void drawArrow(GraphNode fromNode, GraphNode toNode, float nodeRad, float arrowSize){
  123. float fx, fy, tx, ty;
  124. float ax, ay, sx, sy, ex, ey;
  125. float awidthx, awidthy;
  126. fx = fromNode.xf();
  127. fy = fromNode.yf();
  128. tx = toNode.xf();
  129. ty = toNode.yf();
  130. float deltaX = tx - fx;
  131. float deltaY = (ty - fy);
  132. float d = sqrt(deltaX * deltaX + deltaY * deltaY);
  133. sx = fx + (nodeRad * deltaX / d);
  134. sy = fy + (nodeRad * deltaY / d);
  135. ex = tx - (nodeRad * deltaX / d);
  136. ey = ty - (nodeRad * deltaY / d);
  137. ax = tx - (nodeRad + arrowSize) * deltaX / d;
  138. ay = ty - (nodeRad + arrowSize) * deltaY / d;
  139. awidthx = - (ey - ay);
  140. awidthy = ex - ax;
  141. pg_map.noFill();
  142. pg_map.strokeWeight(4.0f);
  143. pg_map.stroke(160, 128);
  144. pg_map.line(sx,sy,ax,ay);
  145. pg_map.noStroke();
  146. pg_map.fill(48, 128);
  147. pg_map.beginShape(TRIANGLES);
  148. pg_map.vertex(ex, ey);
  149. pg_map.vertex(ax - awidthx, ay - awidthy);
  150. pg_map.vertex(ax + awidthx, ay + awidthy);
  151. pg_map.endShape();
  152. }