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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. PImage img_nodes;
  18. void makeGraphFromImage(Graph g, PImage map, int tilesX, int tilesY, boolean allowDiagonals) {
  19. img_nodes.loadPixels();
  20. for (int i = 0; i < img_nodes.pixels.length; i++) {
  21. if (map.pixels[i] != -1) {
  22. img_nodes.pixels[i] = int(color(0));
  23. } else {
  24. img_nodes.pixels[i] = int(color(255));
  25. }
  26. }
  27. img_nodes.updatePixels();
  28. int dx = (img_nodes.width / tilesX) +1;
  29. int dy = (img_nodes.height / tilesY) +1;
  30. int sx = dx / 2, sy = dy / 2;
  31. // use deltaX to avoid horizontal wrap around edges
  32. int deltaX = tilesX + 1; // must be > tilesX
  33. float hCost = dx, vCost = dy, dCost = sqrt(dx*dx + dy*dy);
  34. float cost = 0;
  35. int px, py, nodeID, col;
  36. GraphNode aNode;
  37. py = sy;
  38. for (int y = 0; y < tilesY; y++) {
  39. nodeID = deltaX * y + deltaX;
  40. px = sx;
  41. for (int x = 0; x < tilesX; x++) {
  42. // Calculate the cost
  43. col = img_nodes.get(px, py) & 0xFF;
  44. cost = 1;
  45. // If col is not black then create the node and edges
  46. if (col != 0) {
  47. aNode = new GraphNode(nodeID, px, py);
  48. g.addNode(aNode);
  49. if (x > 0) {
  50. g.addEdge(nodeID, nodeID - 1, hCost * cost);
  51. if (allowDiagonals) {
  52. g.addEdge(nodeID, nodeID - deltaX - 1, dCost * cost);
  53. g.addEdge(nodeID, nodeID + deltaX - 1, dCost * cost);
  54. }
  55. }
  56. if (x < tilesX -1) {
  57. g.addEdge(nodeID, nodeID + 1, hCost * cost);
  58. if (allowDiagonals) {
  59. g.addEdge(nodeID, nodeID - deltaX + 1, dCost * cost);
  60. g.addEdge(nodeID, nodeID + deltaX + 1, dCost * cost);
  61. }
  62. }
  63. if (y > 0)
  64. g.addEdge(nodeID, nodeID - deltaX, vCost * cost);
  65. if (y < tilesY - 1)
  66. g.addEdge(nodeID, nodeID + deltaX, vCost * cost);
  67. }
  68. px += dx;
  69. nodeID++;
  70. }
  71. py += dy;
  72. }
  73. }
  74. IGraphSearch makePathFinder(Graph graph, int pathFinder) {
  75. IGraphSearch pf = null;
  76. float f = 1.0f;
  77. switch(pathFinder) {
  78. case 0:
  79. pf = new GraphSearch_DFS(gs);
  80. break;
  81. case 1:
  82. pf = new GraphSearch_BFS(gs);
  83. break;
  84. case 2:
  85. pf = new GraphSearch_Dijkstra(gs);
  86. break;
  87. case 3:
  88. pf = new GraphSearch_Astar(gs, new AshCrowFlight(f));
  89. break;
  90. case 4:
  91. pf = new GraphSearch_Astar(gs, new AshManhattan(f));
  92. break;
  93. }
  94. return pf;
  95. }
  96. void drawNodes(){
  97. pg_map.pushStyle();
  98. pg_map.noStroke();
  99. pg_map.fill(255,0,255,72);
  100. for(GraphNode node : gNodes)
  101. pg_map.ellipse(node.xf(), node.yf(), nodeSize, nodeSize);
  102. pg_map.popStyle();
  103. }
  104. void drawArrow(GraphNode fromNode, GraphNode toNode, float nodeRad, float arrowSize){
  105. float fx, fy, tx, ty;
  106. float ax, ay, sx, sy, ex, ey;
  107. float awidthx, awidthy;
  108. fx = fromNode.xf();
  109. fy = fromNode.yf();
  110. tx = toNode.xf();
  111. ty = toNode.yf();
  112. float deltaX = tx - fx;
  113. float deltaY = (ty - fy);
  114. float d = sqrt(deltaX * deltaX + deltaY * deltaY);
  115. sx = fx + (nodeRad * deltaX / d);
  116. sy = fy + (nodeRad * deltaY / d);
  117. ex = tx - (nodeRad * deltaX / d);
  118. ey = ty - (nodeRad * deltaY / d);
  119. ax = tx - (nodeRad + arrowSize) * deltaX / d;
  120. ay = ty - (nodeRad + arrowSize) * deltaY / d;
  121. awidthx = - (ey - ay);
  122. awidthy = ex - ax;
  123. pg_map.noFill();
  124. pg_map.strokeWeight(4.0f);
  125. pg_map.stroke(160, 128);
  126. pg_map.line(sx,sy,ax,ay);
  127. pg_map.noStroke();
  128. pg_map.fill(48, 128);
  129. pg_map.beginShape(TRIANGLES);
  130. pg_map.vertex(ex, ey);
  131. pg_map.vertex(ax - awidthx, ay - awidthy);
  132. pg_map.vertex(ax + awidthx, ay + awidthy);
  133. pg_map.endShape();
  134. }