Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Code slows down when using grid

$
0
0

Hi I have a couple of bits of code. One of them places a random series of nodes inside of a box and draws lines between them. New nodes then come in from one side that either add or subtract to the original structure depending on if you press the UP or DOWN key. This code works fine. However, when I then tried to make it work with an ordered grid instead of a random arrangement the sketch then slowed down dramatically when running. I am not really sure why this is as the code is virtually the same and can't figure out why the small change would make such a difference to the running speed. Any help would be much appreciated. Thanks, Michael.

Random Arrangement:

import peasy.*;
PeasyCam cam; //use PeasyCam

ArrayList<Node> nodes;
ArrayList<Node2> nodes2;
ArrayList<Node3> nodes3;

int z = 0;
int move = 0;
int startx = 600;
int starty = 400;

int[] numbers = new int[33];
int num =0;
int dist = 40;
float rand = random(-1, 1);

void setup() {

  lights();
  size(1300, 800, P3D);
  rectMode(CENTER);

  cam = new PeasyCam (this, width/2);
  cam.setMinimumDistance(20);
  cam.setDistance(1000);
  cam.setMaximumDistance(10000);


  nodes = new ArrayList<Node>();
  nodes2 = new ArrayList<Node2>();
  nodes3 = new ArrayList<Node3>();

  for (int i=0; i< 33; i++) {

    num +=int(random(3));
    int number = num;
    print(num);
    numbers [i] = number;
  }
  numbers = sort(numbers);

  println("\nsorted");
  println(numbers);
  fill(0);
}

void draw() {

  background(0);

  if (z < numbers.length -1) {
    z++;
  }

  stroke(100);
  drawboxes();
  generatenodes();
  generatenodes2();
  generatenodes3();
  collidesubtrackers();
  collideadders();

  for (int x = 0; x<nodes3.size(); x++) {
    nodes3.get(x).draw();
  }

  for (int z = 0; z<nodes2.size(); z++) {
    nodes2.get(z).draw();
  }

  for (int t = 0; t<nodes.size(); t++) {
    nodes.get(t).draw();
  }
  for (int i = 0; i<nodes.size()-1; i++) {
    for (int c = i+1; c<nodes.size(); c++) {

      float d = sq(nodes.get(c).loc.x - nodes.get(i).loc.x) + sq(nodes.get(c).loc.y - nodes.get(i).loc.y) + sq(nodes.get(c).loc.z - nodes.get(i).loc.z);

      if ( d < pow(100, 2)  ) {
        stroke(255, map(d, 0, pow(100, 2), 100, 0));
        line(nodes.get(c).loc.x, nodes.get(c).loc.y, nodes.get(c).loc.z, nodes.get(i).loc.x, nodes.get(i).loc.y, nodes.get(i).loc.z);
      }
    }
  }
}

void generatenodes() {

  if (z < numbers.length -1) {

    for (int j=0; j<numbers[z] -1; j++) {
      nodes.add(new Node(new PVector(random(-300, 300), -330 +20*z + random(-5, 5), random(-300, 300) )));
    }
  }
}

void generatenodes2() {


  if (key == CODED) {
    if (keyCode == DOWN) {

      nodes2.add(new Node2(new PVector(600, random(-300, 300), random(-300, 300) )));
    }
  }
}

void generatenodes3() {

  if (key == CODED) {
    if (keyCode == UP) {
      nodes3.add(new Node3(new PVector(600, random(-300, 300), random(-300, 300))));
    }
  }
}
void collidesubtrackers() {

  PVector nodePos;
  for (int i=0; i<nodes.size (); i++) {
    Node drop = (Node) nodes.get(i);
    nodePos = drop.loc;

    PVector node2Pos;
    for (int j=0; j< nodes2.size (); j++) {
      Node2 drop2 = (Node2) nodes2.get(j);
      node2Pos = drop2.loc;

      float distance = nodePos.dist(node2Pos);
      if (distance < dist) {
        nodes.remove(i);
        nodes2.remove(j);
      }
    }
  }

  PVector node2Pos;
  for (int j=0; j< nodes2.size (); j++) {
    Node2 drop2 = (Node2) nodes2.get(j);
    node2Pos = drop2.loc;

    if (node2Pos.x < -600) {
      nodes2.remove(j);
    }
  }
}
void collideadders() {

  PVector nodePos;
  for (int i=0; i<nodes.size (); i++) {
    Node drop = (Node) nodes.get(i);
    nodePos = drop.loc;

    PVector node3Pos;
    for (int j=0; j< nodes3.size (); j++) {
      Node3 drop3 = (Node3) nodes3.get(j);
      node3Pos = drop3.loc;

      float distance = nodePos.dist(node3Pos);
      if (distance < dist) {
        nodes3.remove(j);
        nodes.add(new Node(new PVector(node3Pos.x, node3Pos.y, node3Pos.z)));
      }
    }
  }
  PVector node3Pos;
  for (int j=0; j< nodes3.size (); j++) {
    Node3 drop3 = (Node3) nodes3.get(j);
    node3Pos = drop3.loc;

    if (node3Pos.x < -600) {
      nodes3.remove(j);
    }
  }
}
void drawboxes() {
  pushMatrix();
  noFill();
  translate(0, 0, 0);
  box(600, 600, 600);
  translate(600, 0, 0);
  box(10, 600, 600);
  translate(-1200, 0, 0);
  box(-10, 600, 600);
  popMatrix();
}
class Node {

  PVector loc;
  float radius;

  Node(PVector loc_ ) {

    loc = loc_;
    this.radius = random(1, 5);
  }

  void draw() {

    fill(255);
    noStroke();
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    sphere(radius);
    popMatrix();
  }
}
class Node2 {

  PVector loc;
  float radius;

  Node2(PVector loc_ ) {

    loc = loc_;
    this.radius = random(1, 5);
  }

  void draw() {

    loc.x = loc.x -15;

    fill(0);
    stroke(50);
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    sphere(radius);
    popMatrix();
  }
}
class Node3 {

  PVector loc;
  float radius;


  Node3(PVector loc_) {

    loc = loc_;
    this.radius = random(1, 5);
  }

  void draw() {

    loc.x = loc.x - 15;

    fill(255);
    noStroke();
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    sphere(radius);
    popMatrix();
  }
}

Grid Arrangement:

import peasy.*;
PeasyCam cam; //use PeasyCam

ArrayList<Node> nodes;
ArrayList<Node2> nodes2;
ArrayList<Node3> nodes3;

int z = 0;
int move = 0;
int startx = 600;
int starty = 400;

int[] numbers = new int[33];
int num =0;
int dist = 40;
float rand = random(-1, 1);

void setup() {

  lights();
  size(1300, 800, P3D);
  rectMode(CENTER);

  cam = new PeasyCam (this, width/2);
  cam.setMinimumDistance(20);
  cam.setDistance(1000);
  cam.setMaximumDistance(10000);

  nodes = new ArrayList<Node>();
  nodes2 = new ArrayList<Node2>();
  nodes3 = new ArrayList<Node3>();

}

void draw() {

  background(0);

  stroke(100);
  drawboxes();
  generatenodes();
  generatenodes2();
  generatenodes3();
  collidesubtrackers();
  collideadders();

  for (int x = 0; x<nodes3.size(); x++) {
    nodes3.get(x).draw();
  }

  for (int z = 0; z<nodes2.size(); z++) {
    nodes2.get(z).draw();
  }

  for (int t = 0; t<nodes.size(); t++) {
    nodes.get(t).draw();
  }
  for (int i = 0; i<nodes.size()-1; i++) {
    for (int c = i+1; c<nodes.size(); c++) {

      float d = sq(nodes.get(c).loc.x - nodes.get(i).loc.x) + sq(nodes.get(c).loc.y - nodes.get(i).loc.y) + sq(nodes.get(c).loc.z - nodes.get(i).loc.z);

      if ( d < pow(150, 2)  ) {
        stroke(255, map(d, 0, pow(150, 2), 100, 0));
        line(nodes.get(c).loc.x, nodes.get(c).loc.y, nodes.get(c).loc.z, nodes.get(i).loc.x, nodes.get(i).loc.y, nodes.get(i).loc.z);
      }
    }
  }
}

void generatenodes() {

  for (int i = 0; i < 7; i ++) { //set loop for int i
    for (int j = 0; j < 7; j++) { //set loop for int j
      for (int k = 0; k < 7; k++) { //set loop for int j

        nodes.add(new Node(new PVector(-300+ i *100, -300+ j *100, -300+ k *100)));
      }
    }
  }
}

void generatenodes2() {


  if (key == CODED) {
    if (keyCode == DOWN) {

      nodes2.add(new Node2(new PVector(600, random(-300, 300), random(-300, 300) )));
    }
  }
}

void generatenodes3() {

  if (key == CODED) {
    if (keyCode == UP) {
      nodes3.add(new Node3(new PVector(600, random(-300, 300), random(-300, 300))));
    }
  }
}
void collidesubtrackers() {

  PVector nodePos;
  for (int i=0; i<nodes.size (); i++) {
    Node drop = (Node) nodes.get(i);
    nodePos = drop.loc;

    PVector node2Pos;
    for (int j=0; j< nodes2.size (); j++) {
      Node2 drop2 = (Node2) nodes2.get(j);
      node2Pos = drop2.loc;

      float distance = nodePos.dist(node2Pos);
      if (distance < dist) {
        nodes.remove(i);
        nodes2.remove(j);
      }
    }
  }

  PVector node2Pos;
  for (int j=0; j< nodes2.size (); j++) {
    Node2 drop2 = (Node2) nodes2.get(j);
    node2Pos = drop2.loc;

    if (node2Pos.x < -600) {
      nodes2.remove(j);
    }
  }
}
void collideadders() {

  PVector nodePos;
  for (int i=0; i<nodes.size (); i++) {
    Node drop = (Node) nodes.get(i);
    nodePos = drop.loc;

    PVector node3Pos;
    for (int j=0; j< nodes3.size (); j++) {
      Node3 drop3 = (Node3) nodes3.get(j);
      node3Pos = drop3.loc;

      float distance = nodePos.dist(node3Pos);
      if (distance < dist) {
        nodes3.remove(j);
        nodes.add(new Node(new PVector(node3Pos.x, node3Pos.y, node3Pos.z)));
      }
    }
  }
  PVector node3Pos;
  for (int j=0; j< nodes3.size (); j++) {
    Node3 drop3 = (Node3) nodes3.get(j);
    node3Pos = drop3.loc;

    if (node3Pos.x < -600) {
      nodes3.remove(j);
    }
  }
}
void drawboxes() {
  pushMatrix();
  noFill();
  translate(0, 0, 0);
  box(600, 600, 600);
  translate(600, 0, 0);
  box(10, 600, 600);
  translate(-1200, 0, 0);
  box(-10, 600, 600);
  popMatrix();
}

class Node {

  PVector loc;
  float radius;

  Node(PVector loc_ ) {

    loc = loc_;
    this.radius = random(1, 5);
  }

  void draw() {

    fill(255);
    noStroke();
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    sphere(radius);
    popMatrix();
  }
}

class Node2 {

  PVector loc;
  float radius;

  Node2(PVector loc_ ) {

    loc = loc_;
    this.radius = random(1, 5);
  }

  void draw() {

    loc.x = loc.x -15;

    fill(0);
    stroke(50);
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    sphere(radius);
    popMatrix();
  }
}

class Node3 {

  PVector loc;
  float radius;


  Node3(PVector loc_) {

    loc = loc_;
    this.radius = random(1, 5);
  }

  void draw() {

    loc.x = loc.x - 15;

    fill(255);
    noStroke();
    pushMatrix();
    translate(loc.x, loc.y, loc.z);
    sphere(radius);
    popMatrix();
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles