xxxxxxxxxx
NodeSystem ns; // the System
int num = 250; // number of nodes
float x = 0;
float y = 0;
PVector acceleration;
PVector location;
PVector velocity;
//static points
float ConnectedPoints;
int numPoints = 50;
PVector[] points = new PVector[numPoints];
void setup(){
size(1000,1000);
// the starting distance for the calculation of the lines
int distance =150; //line distance betweeen 2 points
// create the NodeSystem with distance
ns = new NodeSystem(distance);// number of connections from points
smooth();
// initalise all the nodes
// if you put the init into the draw it calcs every loop new nodes
ns.init(num/3);// number of lines, overwrites number of integers
//static points
for (int i = 0; i < numPoints; i++) {
points[i] = new PVector( random(width), random(height) );
}
}// end of setup
//
void draw(){
// write a rect in the size of the sketch for smooth background clear
//noStroke();
//fill(0);
rect(0,0,width+ 100,height+ 100);
// run the node system
ns.run();
//static points
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
strokeWeight(random(100));
frameRate(5);
if ( j != i ) {
float dst = dist( points[i].x, points[i].y, points[j].x, points[j].y );
if ( dst < 255 ) {
stroke(200, 255 - dst);
}
}
}
point( points[i].x, points[i].y );
}
}
// the node class holds the only the dynamic points
// the lines get caculated in the NodeSystem
class Node{
PVector pos; // the node position
PVector vel; // the velocity of the node
float diam; // the diameter
int cons = 1; // the connection he has
// the constructor
Node(PVector pos,float diam){
this.pos = pos;
this.diam = diam;
// start with own velocity
vel = new PVector(random(-3,3),random(-5,5));//movement of balls on points along x,y axis
}
// draw the node
void show(){
fill(245);
ellipse(pos.x, pos.y, diam, diam); //alters scale of nodes
}
// update the position
void update() {
// Motion 101: Locations changes by velocity.
pos.add(vel);
}
// check Edges makes them come in from the other side
void checkEdges() {
if (pos.x > width) {
pos.x = 0;
} else if (pos.x < 0) {
pos.x = width;
} // X
if (pos.y > height) {
pos.y = 0;
} else if (pos.y < 0) {
pos.y = height;
}// Y
}// end checkEdges
}
class NodeSystem{
ArrayList <Node> nodes; // a list of nodes
float distance; // initial distance
// constructor
NodeSystem(float dis){
this.distance = dis;
}
// this initalizes the nodes
void init(int num){
nodes = new ArrayList();
// loop thru num
for(int i = 0; i < num; i++){
// make a random point
float x = random(2, width - 2);
float y = random(2, height - 2);
float diam =1.25;// with diameter
PVector pos = new PVector(x,y);// position into PVector
Node n = new Node(pos,diam);
nodes.add(n); // add the new node to the list
}
}
// run the nodesystem
void run(){
display();
}
// calculate the connections and draw the lines
void calcConnections(Node n){
int num = 1; //ball size
for(int i = 0; i < nodes.size(); i ++){
PVector v1 = n.pos; // position of the refrence position
PVector v2 = nodes.get(i).pos; // every other node
float d = PVector.dist(v1, v2);// calc the distance
// now if the node already has some connections
// make the diastance he can check higher
if((d < distance + n.cons* 3) &&(d > 1)){
stroke(255);
line(v1.x , v1.y,v2.x, v2.y); // draw the line
num++; // increment num
strokeWeight(3); //weight of connection lines
}
// set the connections of the node to the num
n.cons = num; //changes ball size
}
}
// display the nodes and draw the connections
void display(){
Node n = null;// keep it clear
for(int i = 0; i < nodes.size(); i++){
n = nodes.get(i);
// call the functions of node
n.checkEdges();
calcConnections(n);
n.diam = n.cons/.3; // set the size of ball
n.show();// display
n.update(); // and update position
}
} // end display
}