Distance between nodes

Hi!

I'm trying to create a network which resembles neurons and how they transmit information. It's very early stage at the moment, I have a network of some sorts but I don't know how to get a minimum distance between the nodes so they don't appear too close to each other. Trying to find answers online, but teaching myself to process is like trying to learn a foreign language on your own. Would be so grateful for help! :)

Here is my code:

Node n1;
Node n2;

int numNodes = 80;
PVector [] nodes = new PVector[numNodes];

void setup() {
  size(1200, 800);
   background(0);
  n1 = new Node();
  n2 = new Node();

  for (int i = 0; i < numNodes; i++) {
    nodes[i] = new PVector(random(-100, 1300), random(-100, 900));
  }
}

void draw() {
 n1.display();
 n2.display();

}

class Node {

  float x;
  float y;

  Node() {
    x = 15;
    y = 18;
  }

  void display() {
    background(0);

     for (int i = 0; i < numNodes; i++) {
      for (int j = 0; j < numNodes; j++) {
        if (j != i) {
          float dst = dist(nodes[i].x, nodes[i].y, nodes[j].x, nodes[j].y);
          if (dst < 350) {
            stroke(255, 30);
            strokeWeight(4);
            line(nodes[i].x, nodes[i].y, nodes[j].x, nodes[j].y);
            fill(255, 10);
            noStroke();
            ellipse(nodes[i].x, nodes[i].y, random(x, y), random(x, y));
        }
      }
    } 
  } 
}  

}
Tagged:

Answers

  • Edit your post and format your code. Click on the gear icon in the corner of your post, select your code and hit ctrl+o. Ensure there is an empty line above and below your code.

    Kf

  • Do you want the nodes distribute a bit evenly on the screen? What about diving the screen in grids and then loading the nodes within each grid randomly? You problem could still persist. You could still have two nodes close to each other in the case of adjacent grid cells. Then you can do one step further by introducing margins on each grid. Think like an inner square on each grid where the node is drawn inside this square.

    Another way to do it is that you create random node's positions. Before you add a node to your sketch, you check the distance to the existing list of nodes. If the distance is too close to any node, you discard those position values and generate a new set, and recheck the new position set to see if it is acceptable to your requirements.

    Kf

  • Ok, my code is easier to read now, thanks for that tip!

    Yes, I want the nodes more evenly distributed! And also so that it's not so much a shape, but extends off the screen. Like a net. Could you perhaps show me in code how to write that? I have absolutely no idea (I'm veeery new at this)

    Thanks!

  • Answer ✓

    This is implementing the second option. replace your setup of your previous code with this next setup(). The interDistance variable controls the minimum allowable distance btw nodes. i made it to be 5% of the screen's width.

    Kf

    void setup() {
      size(1200, 800);
      background(0);
      n1 = new Node();
      n2 = new Node();
    
      float interDistance=0.05*width;
    
      for (int i = 0; i < numNodes; i++) {
        boolean goodDista;
        do {
          goodDista=true;
          float ix=random(-100, 1300);
          float iy=random(-100, 900);
          for (int j=0; j<i && goodDista==true; j++) {
            //println("fff "+i+"   "+j);
            if (dist(nodes[j].x, nodes[j].y, ix, iy)<interDistance)
              goodDista=false;
          }
          if (goodDista==true) {
            nodes[i] = new PVector(ix, iy );
          } else
            println("Stuck @ i="+i);
        } while (goodDista==false);
      }
    
    }
    
  • You could also try to set minimum distances on a model of your nodes such as a mesh data structure:

  • Thank you! Will try!

Sign In or Register to comment.