Right angle pathfinding agents. Click the above sketch to start a new agent. Link to editor and source files.

var pressed = 0;
var myFont;

var space = 40;

var coor = [];
var dirx = [];
var diry = [];
var ccolor = [];

function setup() {
  createCanvas(800,800);
  background(0);
  for (var x = 0; x < 2800; x++) {
    coor[x] = []; // create nested array
    dirx[x] = [];
    diry[x] = [];
    ccolor[x] = [];
    for (var y = 0; y < 1500; y++) {
      coor[x][y] = 0; // create nested array
      dirx[x][y] = 0;
      diry[x][y] = 0;
      ccolor[x][y] = 0;
    }
  }
  
  var xp = Math.round(random(width)/space) * space + space;
  var yp = Math.round(random(height)/space) * space + space;
  coor[xp][yp] = 1;
  dirx[xp][yp] = 0;
  diry[xp][yp] = 0;
  ccolor[xp][yp] = color(random(255),random(255),random(255));
  
}

function draw() {

  for (var x = 0; x < 2600; x++) {
    for (var y = 0; y < 1400; y++) {
      if(coor[x][y] > 0){
        if(coor[x][y] < 3){
          if(red(ccolor[x][y]) != 0){
            fill(ccolor[x][y]);
            noStroke();
            ellipse(x-space,y-space,space-2,space-2);
          }
        }else{
          coor[x][y] = 4;
        }
      }
      if(coor[x][y] == 1) {
        
        if((x%space == 00) && (y%space == 0)){
          
          dirx[x][y] = 0;
          diry[x][y] = 0;
          for(var i=0; i < 4; i++){
            var newx = Math.round(random(-1,1));
            var newy = Math.round(random(-1,1));
            if((newx !=0) && (newy !=0)){
              var ch = Math.random(0,1);
              if(ch > 0.5){
                newx = 0;
              }else{
                newy = 0;
              }
            }
      
            var xp = x + newx;
            var yp = y + newy;
            var xc = x + newx * space;
            var yc = y + newy * space;
            if(coor[xc][yc] < 1 && coor[xc][yc] > -1 && (xc < 2600) && (yc < 1400)&& (xc > 0) && (yc > 0)){
              coor[xc][yc] = -1;
              coor[xp][yp] = 1;
              dirx[xp][yp] = newx;
              diry[xp][yp] = newy;
              var newr = red(ccolor[x][y]) + random(-2,2);
              var newg = green(ccolor[x][y]) + random(-2,2);
              var newb = blue(ccolor[x][y]) + random(-2,2);
              ccolor[xp][yp] = color(newr,newg,newb);
            }
          }
          coor[x][y] = 2;
          ccolor[x][y] = color(0,0,0);
        }else{
          var xp = x + dirx[x][y];
          var yp = y + diry[x][y];
          coor[xp][yp] = 1;
          if(dirx[x][y] > 0 || diry[x][y] > 0){
            coor[xp][yp] = 3;
          }
          var newr = red(ccolor[x][y]) + random(-2,2);
          var newg = green(ccolor[x][y]) + random(-2,2);
          var newb = blue(ccolor[x][y]) + random(-2,2);
          ccolor[xp][yp] = color(newr,newg,newb);
          dirx[xp][yp] = dirx[x][y];
          diry[xp][yp] = diry[x][y];
          coor[x][y] = 2;
          ccolor[x][y] = color(0,0,0);
        }
      }
      if(coor[x][y] == 4){
        coor[x][y] = 1;
      }
    }
  }
}


function mouseClicked() {
  pressed++;
  var xp = Math.round(mouseX/space) * space + space;
  var yp = Math.round(mouseY/space) * space + space;
  if(coor[xp][yp] == 0){
  coor[xp][yp] = 1;
  dirx[xp][yp] = 0;
  diry[xp][yp] = 0;
  ccolor[xp][yp] = color(random(255),random(255),random(255));
  }
}