> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.FhVIJtLdLOT/rev.15
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * Lerp Dots (v3.80)
 * by GokPotter (2013/Feb)
 * mod GoToLoop
 *
 * http://forum.processing.org/topic/gradual-movement-within-a-for-loop
 * http://forum.processing.org/topic/drawing-a-network-of-nodes-and-links
 *
 * http://studio.processingtogether.com/sp/pad/export/ro.9Kzw4QVZGKeFZ/latest
 */

final static byte  FPS = 040, BIND_DIST = 020;
final static float DOT_THICK = 4, LINE_THICK = 1.5;
final static color BG = 0350, DOT_COLOR = #FFA000, LINE_COLOR = #0000FF;
final static short NUM_STEPS = 0500, DOT_AMOUNT = 01000;
final static boolean IS_SMOOTH = false;

final static PVector[] dotList    = new PVector[DOT_AMOUNT];
final static PVector[] oldDotList = new PVector[DOT_AMOUNT];
final static PVector[] currentPos = new PVector[DOT_AMOUNT];

final static String ENGINE = JAVA2D;  // Use JAVA2D or P2D

void setup() {
  size(800, 600, ENGINE);
  frameRate(FPS);

  if (IS_SMOOTH)    smooth();
  else            noSmooth();

  createDotList();
  newDotList();
}

void createDotList() {
  for (int i = 0; i != DOT_AMOUNT; i++) {
    dotList[i]    = new PVector( random(width), random(height) );
    currentPos[i] = new PVector();
  }
}

void newDotList() {
  arrayCopy(dotList, oldDotList);

  for (int i = 0; i != DOT_AMOUNT;
    dotList[i++] = new PVector( random(width), random(height) ));
}

void connectPoints(int i) {
  for (int z = i+1; z != DOT_AMOUNT; z++)
    if (currentPos[i].dist(currentPos[z]) < BIND_DIST)
      line(currentPos[i].x, currentPos[i].y, currentPos[z].x, currentPos[z].y);
}

void draw() {
  background(BG);

  final float amt = (float) frameCount/NUM_STEPS;

  for (int i = 0; i != DOT_AMOUNT;) {
    final float pointX = lerp(oldDotList[i].x, dotList[i].x, amt);
    final float pointY = lerp(oldDotList[i].y, dotList[i].y, amt);
    currentPos[i].set(pointX, pointY, 0);

    strokeWeight(DOT_THICK);
    stroke(DOT_COLOR);
    point(pointX, pointY);

    strokeWeight(LINE_THICK);
    stroke(LINE_COLOR);
    connectPoints(i++);
  }

  /*
  frame.setTitle( "Lerp Dots         Frame: #" + frameCount 
   + "      FPS: " + round(frameRate) );
   */

  if (frameCount == NUM_STEPS) {
    frameCount = 0;
    newDotList();
  }
}

void keyPressed() {
  if (key == ' ')       frameCount = NUM_STEPS-1;
  else mousePressed();
}

void mousePressed() {
  if (looping)   noLoop();
  else             loop();
}