I’m coding a djiktsra search algorithm however I wanted to improve the last bit my programs algorithm and again run into some if statement issues, not sure if this is a bug or not.
https://editor.p5js.org/paulgoux/sketches/eiY8BkMUm
The issue occurs arround line 2285, in the code commented out.
The jist of the issue is, the pathfinder is made to travel from node to node and find the next nearest node, whilst it does this it has to fill in the path to mark it as having been travelled. All this is fine until I get to the end, at this point the logic statements I’m using do not work.
Each node is aware of its children and the steps required to get to them, and each child is aware of its parents and its distance to them. Therefore I would have though you verify that the finish point lies on a path, retrieve the targets parents, and work from there. Pick the parent which is currently the pathfinder, from there look at the pathfinders children and only mark the ones which have matching parents with the finishing point. Alas this does not work.
The function is called this.parentNode, and called at this.nextNode.
var step;
for(var i=0;i<this.child.length;i++){
var a = this.child[i];
if(a[1] ===target){
step = a[0];
}}
var a;
var b;
var c;
for(var i=0;i<2;i++){
var x = target.node_info[i];
if(a){
b = x;
}
else if(!a){
a = x;
}}
var d1 = dist(a[1].x,a[1].y,target.x,target.y);
var d2 = dist(b[1].x,b[1].y,target.x,target.y);
var array = [];
// for(var i=0;i<this.child.length;i++){
// var k = this.child[i];
// var d = k[1].node_info[0];
// var c = k[1].node_info[1];
// if(d[1]!==a[1]&&c!==b[1]&&c[1]!==a[1]){
// array.push(k)
// }}
//
// if(array.length>0){
// for(var i=0;i<array.length;i++){
// var k = array[i];
// var d = k[1].node_info[0];
// var c = k[1].node_info[1];
// // if(){
// k[1].pcol2 = color(0,0,0,150);
// k[1].depth = this.depth;
// k[1].visited2 = true;
// }}
return target;
if(a[0]<step&a[1]===this){
for(var i=0;i<a[1].child.length;i++){
var k = a[1].child[i];
// node info contains parent information, distance at index 0, and reference at index 1.
var d = k[1].node_info[0];
var c = k[1].node_info[1];
//stack array is the reference to the stack the function is currently using.
if((d[1]===a[1]&&d[0]<=step-1||c[1]===a[1]&&c[0]<=step-1)){
stack_array.push(k[1]);
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth;
k[1].visited2 = true;
}}
return target;
}
else if(b[1]===this){
for(var i=0;i<b[1].child.length;i++){
var k = b[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if(d[1]===b[1]&&d[0]<=step-1||c[1]===b[1]&&c[0]<=step-1){
stack_array.push(k[1]);
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth;
k[1].visited2 = true;
}}
return target;
}