Loading...
Logo
Processing Forum
The code consists on some moving points. I'd like to draw lines from point to point when the distance that separates them is less than a certain number, but i don't know how can i make the point coordinates interact among them. HELP!



Copy code
  1. Dots [] dots = new Dots [20];

  2. void setup(){
  3.   size(800,600);
  4.   smooth();
  5.   
  6.     for(int i = 0; i < dots.length; i++){
  7.     dots[i] = new Dots();
  8.   }
  9. }

  10.   
  11. void draw(){
  12.   
  13.   background(255);
  14.   
  15.   for(int i = 0; i < dots.length; i++){
  16.     
  17.     dots[i].move();
  18.     dots[i].display();
  19.     
  20.     if (dots[i].intersect(dots[i])){
  21.       dots[i].drawLines(dots[i]);
  22.     
  23.     }
  24.   }
  25. }

  26. class Dots{
  27.   
  28.   float x,y;             // position
  29.   float xspeed,yspeed;
  30.   float d;               //diameter
  31.   color c;


  32.   Dots(){
  33.   
  34.     x = width/2;
  35.     y = height/2;
  36.     d = 5;
  37.     c = color (200);
  38.     xspeed = random(-5,5);
  39.     yspeed = random(-5,5);
  40.   
  41.   }
  42.   
  43.   void display(){
  44.     noStroke();
  45.     fill(c);
  46.     ellipse(x,y,d,d);
  47.   }
  48.   
  49.   void move(){
  50.     x += xspeed;
  51.     y += yspeed;
  52.     
  53.     if(x > width || x < 0){
  54.       xspeed *= -1;
  55.     }
  56.     if(y > height || y < 0){
  57.       yspeed *= -1;
  58.   } 
  59. }
  60.   
  61.   void drawLines(Dots b){          
  62.      stroke(c);
  63.      line(x,y,b.x,b.y);
  64.   }
  65.      
  66.   boolean intersect(Dots b){
  67.     float distance = abs(dist (x,y,b.x,b.y));  
  68.     if(distance < 30){
  69.       return true;
  70.     } else {
  71.       return false; 
  72.     }  
  73.   }
  74. }

Replies(3)

Each Dot has to check against every1 else whether they're near; so a line() can be drawn linking them together.
Your code above only redundantly check a Dot against itself!
So you need an extra inner loop for each iteration of the outer 1.

Here's an online example:
http://studio.processingtogether.com/sp/pad/export/ro.9Kzw4QVZGKeFZ/latest

Some posts dealing w/ it:
http://forum.processing.org/topic/gradual-movement-within-a-for-loop
http://forum.processing.org/topic/drawing-a-network-of-nodes-and-links

And below's your code fixed:
Copy code
    /** 
     * Moving Dots (v2.25)
     * 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.9Kzw4QVZGKeFZ/latest
     */
    
    final static int NUM = 30, FPS = 60;
    final static Dot[] dots = new Dot[NUM];
    
    boolean isPaused;
    
    void setup() {
      size(800, 600);
      frameRate(FPS);
      smooth();
      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+1; z != NUM; z++)
        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);
      }
    }
    
And a more advanced version using Comparable interface and sort() method from Arrays class:
Copy code
    /**
     * Vector Dot Lines (v3.57)
     * by GokPotter (2013/Feb)
     * modders v.k., Thomas.Diewald, GoToLoop
     *
     * works faster when compiled in P1.5.1!
     *
     * http://forum.processing.org/topic/gradual-movement-within-a-for-loop
     * http://forum.processing.org/topic/drawing-a-network-of-nodes-and-links
     * http://forum.processing.org/topic/interaction-among-objects-in-an-array
     *
     * http://studio.processingtogether.com/sp/pad/export/ro.9Kzw4QVZGKeFZ/latest
     */
    
    import java.util.Arrays;
    
    final static color BG = -1, FG = 0;
    final static short DOT_DIST  = 50, DOT_OPAC = 60;
    final static float DOT_SPEED = 3, FPS = 60, BOLD = 1;
    
    final static short NUM_DOTS  = 5000;
    final static Point[] points = new Point[NUM_DOTS];
    
    final static String GFX = P2D;  // Use P2D or JAVA2D
    
    static int dotConnects;
    
    void setup() {
      size(1024, 800, GFX);
      frameRate(FPS);
      noSmooth();
      strokeWeight(BOLD);
      stroke(FG, DOT_OPAC);
    
      for ( int i = 0; i != NUM_DOTS; 
      points[i++] = new Point(random(.5, DOT_SPEED)) );
    }
    
    void draw() {
      background(BG);
    
      Arrays.sort(points);
    
      final float minDist = map(mouseX, 0, width, 1, DOT_DIST);
      dotConnects = 0;
    
      for (int i = 0; i != NUM_DOTS;) {
        final Point p = points[i++];
    
        p.update();
        p.drawPoint();
    
        nearestNeighbors(p, i, minDist);
      }
    
      frame.setTitle(" Nearest neighbor"
        + " | fps "         + nf(frameRate, 0, 2)
        + " | minDist "     + nf(minDist, 0, 2)
        + " | numPoints "   + NUM_DOTS
        + " | numConnects " + dotConnects);
    }
    
    static final void nearestNeighbors(Point p1, int i, float minDist) {
      final float minDistSq = minDist * minDist;
      final float x = p1.x, y = p1.y;
    
      for (int j = i; j != NUM_DOTS;) {
        final Point p2 = points[j++];
    
        final float dx = p2.x - x;
        if (dx > minDist)   return;
    
        final float dy = abs(p2.y - y);
        if (dy > minDist)   continue;
    
        if (dx*dx + dy*dy < minDistSq) {
          p1.drawLine(p2);
          dotConnects++;
        }
      }
    }
    
    final class Point extends PVector implements Comparable<Point> {
      final PVector dir = new PVector();
      final float spd;
    
      Point(float speed) {
        spd = speed;
        init();
      }
    
      void init() {
        final float a = random(TWO_PI);
        dir.set(spd*cos(a), spd*sin(a), 0);
        set(random(width), random(height), 0);
      }
    
      void update() {
        add(dir);
        if ( isOffScreen() )   init();
      }
    
      boolean isOffScreen() {
        return x < 0 | x > width | y < 0 | y > height;
      }
    
      void drawPoint() {
        point(x, y);
      }
    
      void drawLine(Point other) {
        line(x, y, other.x, other.y);
      }
    
      int compareTo(Point other) {
        return (int) Math.signum(x - other.x);
      }
    }
    
Fixed and working. Thanks for the extra information, it will keep me busy for some time