How do I optimize/make my script run better/let processing take more resources

I recently coded a simple point cloud script but it starts to lag really bad when I go above 250 or so points.
Is there any way to optimize my script / let processing take more resources?
keep in mind I am new to Processing, but know java really well.

here is a simplified version of my script without a few features or the settings menu:

float MaxSpeed = 2;
float MaxDBPts = 200; //max distance between points
float SizeFactor = 100;
float MaxSize = 5;
float windowX = 1920;
float windowY = 1080;
int pointAmount = 200;
point[] pList = new point[pointAmount];
void setup() {
  background(0);
  fullScreen();
  frameRate(60);
  stroke(255);
  for (int i = 0; i < pList.length; i++) {
    pList[i] = new point(random(windowX), random(windowY), random(-MaxSpeed, MaxSpeed), random(-MaxSpeed, MaxSpeed));
  }
}

class point {
  private float x, y, mx, my;
  point (float a, float b, float c, float d)
  {
    x = a;
    y = b;
    mx = c;
    my = d;
  }
  float getX() {
    return x;
  }
  float getY() {
    return y;
  }
  void cyclepoint()
  {
    x += mx;
    y += my;
    if (x > windowX) {
      x = 0;
    }
    if (y > windowY) {
      y = 0;
    }
    if (x < 0) {
      x = windowX;
    }
    if (y < 0) {
      y = windowY;
    }
  }
}

void draw() {
  background(0);
  float tempD = 0;
  point TP1;
  point TP2;

  for (int i = 0; i < pList.length; i++) {
    pList[i].cyclepoint();
  }
  for (int o = 0; o < pList.length; o++) {
    for (int t = 0; t < pList.length; t++) {
      TP1=pList[o];
      TP2=pList[t];//temporary point objects used to make tempd and other calculations easier to write and read
      tempD = sqrt(sq(TP1.getX() - TP2.getX()) + sq(TP1.getY() - TP2.getY()));//calculates distance between 2 points
      if (tempD<MaxDBPts&&tempD!=0) {
        if (SizeFactor/tempD<MaxSize) {//determines line thickness
          strokeWeight(SizeFactor/tempD);
        }
        else {
          strokeWeight(MaxSize);
        }
        line(TP1.getX(), TP1.getY(), TP2.getX(), TP2.getY());//draws the line
      }
    }
  }
}
1 Like

You got a nested for loop with o and t

You check ever pair twice

In the 2nd for loop say t=o+1

(In the first length-1)

Also sqrt and sq are very costly; google a better formula

1 Like

switch your renderer too.

fullScreen(P2D);
1 Like