Random walker through points

here is a version that stops.

We return -1 (not an allowed index!) to indicate failure when dist > 86 or so

Then we evaluate idx and abort update in Walker class

ArrayList<Walker> walkers;
ArrayList<Anchor> anchors;

PVector pos;

int walkerCount = 1;
int anchorCount = 500;

float x = 0;
float y = 0;

void setup() {
  size(800, 800);
  background(51);
  // frameRate(6);

  walkers = new ArrayList<Walker>();
  anchors = new ArrayList<Anchor>();

  for (int i = 0; i < walkerCount; i++) {
    PVector origin = new PVector(random(width), 0);
    walkers.add(new Walker(origin, color(random(256), random(256), random(256))));
  }

  for (int i = 0; i < anchorCount; i++) {
    PVector position = new PVector(random(width), random(height));
    anchors.add(new Anchor(position));
  }
}//setup()

void draw() {
  //  background(51);

  for (int i = 0; i < walkers.size(); i++) {  // for (Walker w : walkers) {  !!!!!!!!!!!!!!!!!!!!!!!
    Walker w = walkers.get(i);  // (Walker) walkers.get(i);
    w.update(anchors);
    w.show();
  }

  for (int i = 0; i < anchors.size(); i++) {
    Anchor a = anchors.get(i); // (Anchor) anchors.get(i);
    a.show();
  }
}//draw()

// ===========================================================================

class Walker {
  PVector pos;
  PVector previousPos;
  color col;

  Walker(PVector position, color c) {
    col = c;
    pos = position.copy(); //  new PVector(position.x, position.y);
    previousPos = pos.copy();
  }

  void show() {
    strokeWeight(4);
    stroke(col);
    //point(pos.x, pos.y);
    line(previousPos.x, previousPos.y, 
      pos.x, pos.y);
  }

  void update(ArrayList<Anchor> anchors) {

    if (anchors.size()>0) {
      int idx = getNearest(anchors);

      if (idx<0) 
        return;  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      println(""+idx);
      Anchor a = anchors.get(idx);
      PVector move = a.pos;
      previousPos.set(pos);

      /*
      float targetX = move.x;
       float dx = targetX - pos.x;
       pos.x += dx ; //  * easing;  
       
       float targetY = move.y;
       float dy = targetY - pos.y;
       pos.y += dy ; // * easing; 
       */

      // pos.add(move);
      pos=move.copy(); 
      anchors.remove(idx);
    }
  }

  int getNearest(ArrayList<Anchor> anchors) {
    float record = 10000;
    int idx = 0;
    for (int i = 0; i < anchors.size(); i++) {
      Anchor anchor = anchors.get(i);
      float dist = pos.dist(anchor.pos);
      if (dist < record) {
        record = dist;  // YOU HAD  dist = record;   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        idx = i;
      }
    }
    if (record<86) 
      return idx;    // give index of nearest target
    else 
    return -1;
  }
}

// ===========================================================================

class Anchor {
  PVector pos;
  color col;

  Anchor(PVector position) {
    col = color(0);
    pos = new PVector(position.x, position.y);
  }

  void show() {
    strokeWeight(4);
    stroke(col);
    point(pos.x, pos.y);
  }

  void update() {
  }
}
//
2 Likes