Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var pixels = []; //an array to hold all the objects(the grid)
- var obstacles = []; //array to hold the obstacles
- function pixel(X, Y, obs) {
- this.X_co_ordinate = X;
- this.Y_co_ordinate = Y;
- this.state = obs; //availale states OPN, UND, OBS, DIS, NULL
- this.g = 0;
- this.h = 0;
- this.f = 0;
- this.last = null;
- } //every block in the grid is a pixel
- function populate(height, width, obs_val = "UND") {
- pixels[0] = new pixel(0, 10, obs_val);
- for (h = height-1, i = 0; h >= 0; h--) {
- for (w = 0; w < width; w++, i++) {
- var temp_obs = new pixel(w, h, obs_val);
- //temp_obs.last = pixels[0];
- pixels[i] = temp_obs; //saving temp_pixel object to pixels array
- }
- }
- } //populating the grid AKA pixels with pixel objects or blocks
- function GetCOordinates(i) {
- let x = i % box_in_x;
- let y = box_in_y - ((i - x) / box_in_x) - 1;
- return [x,y];
- } //get x and y value of the object when passed the index of the object in the pixel array
- function getIndex(x, y) {
- if(x<box_in_x && x>=0 && y<box_in_y && y>=0){
- return (box_in_x * (box_in_y - y - 1)) + (parseInt(x));
- }else{
- return -1;
- }
- } // get index of the object in the array when given the x and u coordinate
- /////// pathfinding begins here
- function getG(current, start){
- if(current.g != 0){
- return current.g;
- }else if(current.last != null && current != start){
- current.g = getG(current.last, start) + 1;
- }else if(current == start){
- current.g = 0;
- }else if(current.last == null){
- throw new Error("ERROR: last of current is not set");
- }
- else{
- throw new Error("ERROR: no conditions matched in getG");
- }
- return current.g;
- }
- function getH(current, end){
- current.h = Math.abs(current.X_co_ordinate - end.X_co_ordinate) + Math.abs(current.Y_co_ordinate - end.Y_co_ordinate);
- return current.h;
- }
- function getF(start, current, end){
- return getG(current, start) + getH(current, end);
- }
- function getNeighbours(current, start){
- let neighbours = [];
- let tpixelsI = [], tpixels;
- tpixelsI[0] = getIndex(current.X_co_ordinate+1, current.Y_co_ordinate);
- tpixelsI[1] = getIndex(current.X_co_ordinate-1, current.Y_co_ordinate);
- tpixelsI[2] = getIndex(current.X_co_ordinate, current.Y_co_ordinate+1);
- tpixelsI[3] = getIndex(current.X_co_ordinate, current.Y_co_ordinate-1);
- for(let i=0; i<tpixelsI.length; i++){
- if(tpixelsI[i] && tpixelsI[i] >= 0){
- //console.log(tpixelsI[i] + " _ " + i);
- let neighbour = pixels[tpixelsI[i]];
- neighbours.push(neighbour);
- if(neighbour.last == null && neighbour != start){
- console.log("done for");
- console.log(neighbour);
- neighbour.last = current;
- }
- }
- }
- return neighbours;
- }
- function lowFinArray(openset, start, end){
- let current_low = openset[0];
- for(let i=0; i<openset.length; i++ ){
- if(getF(start, openset[i], end) < getF(start, current_low, end)){
- current_low = openset[i];
- }
- }
- return current_low;
- }
- function getPath(current, start){
- let path = [];
- while(current != start){
- path.push(current);
- current = current.last;
- }
- return path
- }
- function pathfind(pixels, start, end){
- let closedSet = [];
- let openSet = [start];
- let current = start;
- while(openSet.length > 0){
- //console.log("openset at first");
- //console.log(JSON.parse(JSON.stringify(openSet)));
- //console.log("current");
- current = lowFinArray(openSet, start, end);
- //console.log(current);
- openSet.splice(openSet.indexOf(current), 1);
- //console.log("openset after slice");
- //console.log(JSON.parse(JSON.stringify(openSet)));
- //console.log("end"); console.log(end);
- if(current === end){
- getPath(current, start);
- }
- let neighbours = getNeighbours(current, start);
- //console.log("neighbours");
- //console.log(neighbours);
- for(let i=0; i<neighbours.length; i++){
- console.log("i " + i);
- //console.log("openset"); console.log(openSet);
- let neighbour = neighbours[i];
- if(neighbour === undefined){
- continue;
- }else if(closedSet.includes(neighbour)){
- continue;
- }else if(!openSet.includes(neighbour)){
- openSet.push(neighbour);
- continue;
- }
- let tGscore = getG(current, start) + getH(neighbour, current);
- if(tGscore > getG(neighbour, start)){
- continue;
- }
- neighbour.last = current;
- neighbour.g = tGscore;
- neighbour.h = getH(neighbour, end);
- neighbour.f = getF(neighbour, start, end);
- closedSet.push(current);
- }
- }
- }
- // define the height, width and bot size in centimeter
- total_width = 100;
- total_height = 100;
- bot_size = 20;
- total_box = (total_height / bot_size) * (total_width / bot_size);
- box_in_x = total_width / bot_size;
- box_in_y = total_height / bot_size;
- //populating the pixels array
- populate(total_width / bot_size, total_height / bot_size, "UND");
- pathfind(pixels, pixels[0], pixels[pixels.length - 1]); //calling the pathfinding algo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement