> show canvas only <


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

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



/** 
 * Moving Dots (v2.27)
 * by Josem.93 (2013/Aug)
 * mod GoToLoop
 * 
 * http://forum.processing.org/topic/interaction-among-objects-in-an-array
 * http://forum.processing.org/topic/gradual-movement-within-a-for-loop
 * 
 * http://studio.processingtogether.com/sp/pad/export/ro.9s0026dE7B8v-/latest
 */

final static int NUM = 30, FPS = 60;
final static Dot[] dots = new Dot[NUM];

boolean isPaused;

void setup() {
  size(800, 600);
  frameRate(FPS);
  noSmooth();
  stroke(Dot.COLOUR);
  fill(Dot.COLOUR);

  for (int i = 0; i != NUM; dots[i++] = new Dot());
}

void draw() {
  background(-1);
  for (int i = 0; i != NUM; connectPoints(i++))   dots[i].script();
}

void mousePressed() {
  if (isPaused = !isPaused)   noLoop();
  else                        loop();
}

void keyTyped() {
  mousePressed();
}

final static void connectPoints(int i) {
  for (int z = i; ++z != NUM;)
    if ( dots[i].isNear(dots[z]) )   dots[i].drawLine(dots[z]);
}

class Dot {
  final static short DIM = 5, MIN_DIST = 30, MAX_SPD = 5;
  final static color COLOUR = 0100;

  float x, y;
  float spx = random(-MAX_SPD, MAX_SPD);
  float spy = random(-MAX_SPD, MAX_SPD);

  Dot() {
    x = width>>1;
    y = height>>1;
  }

  void script() {
    move();
    display();
  }

  void move() {
    if ((x += spx) > width  | x < 0)  spx *= -1;
    if ((y += spy) > height | y < 0)  spy *= -1;
  }

  void display() {
    ellipse(x, y, DIM, DIM);
  }

  boolean isNear(Dot other) {
    //return dist(x, y, other.x, other.y) < MIN_DIST;
    return sq(other.x - x) + sq(other.y - y) < MIN_DIST*MIN_DIST;
    //return abs(other.x - x) < MIN_DIST && abs(other.y - y) < MIN_DIST;
  }

  void drawLine(Dot other) {
    line(x, y, other.x, other.y);
  }
}