Advertisement
Guest User

A* algo

a guest
Nov 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var pixels = []; //an array to hold all the objects(the grid)
  2.  
  3. var obstacles = []; //array to hold the obstacles
  4.  
  5. function pixel(X, Y, obs) {
  6.     this.X_co_ordinate = X;
  7.     this.Y_co_ordinate = Y;
  8.     this.state = obs; //availale states OPN, UND, OBS, DIS, NULL
  9.     this.g = 0;
  10.     this.h = 0;
  11.     this.f = 0;
  12.     this.last = null;
  13. } //every block in the grid is a pixel
  14.  
  15. function populate(height, width, obs_val = "UND") {
  16.    
  17.     pixels[0] = new pixel(0, 10, obs_val);
  18.    
  19.     for (h = height-1, i = 0; h >= 0; h--) {
  20.         for (w = 0; w < width; w++, i++) {
  21.             var temp_obs = new pixel(w, h, obs_val);
  22.             //temp_obs.last = pixels[0];
  23.             pixels[i] = temp_obs; //saving temp_pixel object to pixels array
  24.         }
  25.     }
  26.        
  27. } //populating the grid AKA pixels with pixel objects or blocks
  28.  
  29. function GetCOordinates(i) {
  30.    
  31.     let x = i % box_in_x;
  32.     let y = box_in_y - ((i - x) / box_in_x) - 1;
  33.    
  34.     return [x,y];
  35. } //get x and y value of the object when passed the index of the object in the pixel array
  36.  
  37. function getIndex(x, y) {
  38.     if(x<box_in_x && x>=0 && y<box_in_y && y>=0){
  39.         return (box_in_x * (box_in_y - y - 1)) + (parseInt(x));
  40.     }else{
  41.         return -1;
  42.     }
  43. } // get index of the object in the array when given the x and u coordinate
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. /////// pathfinding begins here
  54.  
  55. function getG(current, start){
  56.     if(current.g != 0){
  57.         return current.g;
  58.     }else if(current.last != null && current != start){
  59.         current.g = getG(current.last, start) + 1;
  60.     }else if(current == start){
  61.         current.g = 0;
  62.     }else if(current.last == null){
  63.         throw new Error("ERROR: last of current is not set");
  64.     }
  65.     else{
  66.         throw new Error("ERROR: no conditions matched in getG");
  67.     }
  68.    
  69.     return current.g;
  70. }
  71.  
  72. function getH(current, end){
  73.     current.h = Math.abs(current.X_co_ordinate - end.X_co_ordinate) + Math.abs(current.Y_co_ordinate - end.Y_co_ordinate);
  74.     return current.h;
  75. }
  76.  
  77. function getF(start, current, end){
  78.     return getG(current, start) + getH(current, end);
  79. }
  80.  
  81. function getNeighbours(current, start){
  82.     let neighbours = [];
  83.     let tpixelsI = [], tpixels;
  84.    
  85.     tpixelsI[0] = getIndex(current.X_co_ordinate+1, current.Y_co_ordinate);
  86.     tpixelsI[1] = getIndex(current.X_co_ordinate-1, current.Y_co_ordinate);
  87.     tpixelsI[2] = getIndex(current.X_co_ordinate, current.Y_co_ordinate+1);
  88.     tpixelsI[3] = getIndex(current.X_co_ordinate, current.Y_co_ordinate-1);
  89.    
  90.    
  91.     for(let i=0; i<tpixelsI.length; i++){
  92.         if(tpixelsI[i] && tpixelsI[i] >= 0){
  93.             //console.log(tpixelsI[i] + " _ " + i);
  94.             let neighbour = pixels[tpixelsI[i]];
  95.             neighbours.push(neighbour);
  96.             if(neighbour.last == null && neighbour != start){
  97.                 console.log("done for");
  98.                 console.log(neighbour);
  99.                 neighbour.last = current;
  100.             }
  101.            
  102.         }
  103.     }
  104.    
  105.     return neighbours;
  106. }
  107.  
  108.  
  109. function lowFinArray(openset, start, end){
  110.     let current_low = openset[0];
  111.    
  112.     for(let i=0; i<openset.length; i++ ){
  113.         if(getF(start, openset[i], end) < getF(start, current_low, end)){
  114.             current_low = openset[i];
  115.         }
  116.     }
  117.    
  118.     return current_low;
  119. }
  120.  
  121. function getPath(current, start){
  122.     let path = [];
  123.     while(current != start){
  124.         path.push(current);
  125.         current = current.last;
  126.     }
  127.    
  128.     return path
  129. }
  130.  
  131. function pathfind(pixels, start, end){
  132.     let closedSet = [];
  133.     let openSet = [start];
  134.     let current = start;
  135.    
  136.     while(openSet.length > 0){
  137.         //console.log("openset at first");
  138.         //console.log(JSON.parse(JSON.stringify(openSet)));
  139.         //console.log("current");
  140.         current = lowFinArray(openSet, start, end);
  141.         //console.log(current);
  142.         openSet.splice(openSet.indexOf(current), 1);
  143.         //console.log("openset after slice");
  144.         //console.log(JSON.parse(JSON.stringify(openSet)));
  145.        
  146.         //console.log("end"); console.log(end);
  147.         if(current === end){
  148.             getPath(current, start);
  149.         }
  150.        
  151.         let neighbours = getNeighbours(current, start);
  152.         //console.log("neighbours");
  153.         //console.log(neighbours);
  154.        
  155.         for(let i=0; i<neighbours.length; i++){
  156.             console.log("i " + i);
  157.             //console.log("openset"); console.log(openSet);
  158.             let neighbour = neighbours[i];
  159.            
  160.             if(neighbour === undefined){
  161.                 continue;
  162.             }else if(closedSet.includes(neighbour)){
  163.                 continue;
  164.             }else if(!openSet.includes(neighbour)){
  165.                 openSet.push(neighbour);
  166.                 continue;
  167.             }
  168.            
  169.             let tGscore = getG(current, start) + getH(neighbour, current);
  170.            
  171.             if(tGscore > getG(neighbour, start)){
  172.                 continue;
  173.             }
  174.            
  175.             neighbour.last = current;
  176.             neighbour.g = tGscore;
  177.             neighbour.h = getH(neighbour, end);
  178.             neighbour.f = getF(neighbour, start, end);
  179.            
  180.             closedSet.push(current);
  181.         }
  182.     }
  183. }
  184.  
  185.  
  186. // define the height, width and bot size in centimeter
  187.     total_width = 100;
  188.     total_height = 100;
  189.     bot_size = 20;
  190.     total_box = (total_height / bot_size) * (total_width / bot_size);
  191.     box_in_x = total_width / bot_size;
  192.     box_in_y = total_height / bot_size;
  193.  
  194.     //populating the pixels array
  195.     populate(total_width / bot_size, total_height / bot_size, "UND");
  196.     pathfind(pixels, pixels[0], pixels[pixels.length - 1]); //calling the pathfinding algo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement