Plexus effect with connections limitations

In the sketch below the circles are randomly connected by lines based on the mouseY. The problem here is that some of the circles hold to many connections making it visually too dense. I wonder how can I control the maximum number of connections that a circle can hold.

Any ideas?

Thank you in advance.

D

Answers

  • Answer ✓

    I don't see any posted sketch from you. Nevertheless, a possible similar 1 below:

    /**
     * Vector Dot Lines (v3.59)
     * by GokPotter (2013/Feb)
     * modders v.k., Thomas.Diewald, GoToLoop
     *
     * works faster when compiled in P1.5.1!
     *
     * forum.processing.org/topic/gradual-movement-within-a-for-loop
     * forum.processing.org/topic/drawing-a-network-of-nodes-and-links
     * forum.processing.org/topic/interaction-among-objects-in-an-array
     *
     * studio.processingtogether.com/sp/pad/export/ro.9Kzw4QVZGKeFZ/latest
     */
    
    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);
    
      java.util.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) {
          //if (dy < minDist && abs(dx) < minDist) {
          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);
      }
    }
    
  • @GoToLoop I completely forgot to post the sketch. Thank you for your pro-activeness. Very nice the result.

    I am trying to limit the number of connections in the sketch below:

    import processing.pdf.*;
    boolean savePDF = false;
    
    
    Circle[] circles;
    int numCircles = 250; // number of circles
    int maxDistance; // distance
    int circleSize = 3; // circle size
    int strokeWeight = 1; // stroke weight
    int cl = 255; // stroke colour
    
    void setup() {
      size(320, 530);
      smooth();
    
      // create an array and fill it with circles
      circles = new Circle[numCircles];
    
      for (int i=0; i< numCircles; i++) {
        circles[i] = new Circle(random(width), random(height), circleSize);
      }
    }
    
    void draw() {
    
      if ( savePDF ) { // Begin record 
        beginRecord( PDF, "pdf/myartwork-####.pdf" );
      }
    
    
      // clear background
      background(0, 0, 20);
    
      // update and display the circles
      for (int i=0; i< numCircles; i++) {
        //  circles[i].update(); 
        circles[i].display();
      }
    
      // define maximum distance
      maxDistance = mouseY;
    
      // look of the lines
      stroke(cl);
      strokeWeight(strokeWeight);
    
      for (int i=0; i< numCircles; i++) {
    
        // compare circle to other circles
        for (int j=i+1; j< numCircles; j++) {
    
          // draw line if distance is below 'maxDistance'
          if (dist(circles[i].x, circles[i].y, circles[j].x, circles[j].y) < maxDistance) {
            line(circles[i].x, circles[i].y, circles[j].x, circles[j].y);
          }
        }
      }
    
      if ( savePDF ) { // End record
        endRecord(); 
        savePDF = false;
      }
    }
    
    
    class Circle {
      float x, y, dia;
    
      Circle(float x, float y, float dia) {
        this.x =x;
        this.y =y;
        this.dia = dia;
      }
    
      /*
      void update() {
       // code for movement here
       // this is just some random displacement
       x = x + random(-1, 1);
       y = y + random(-1, 1);
       }
       */
    
    
      void display() {
        // code for drawing the circles
        noStroke();
        fill(255, 255, 255);
        ellipse(x, y, dia, dia);
      }
    }
    
    void keyPressed() {
      if ( key == 's' ) {
        savePDF = true;
      }
    }
    
Sign In or Register to comment.