I am trying to optimize a part of my code that creates 500 PGraphics. Is there a way to creates them all at the same time ?
final int child_count = 5;
final int min_size = 5;
final int max_size = 300;
final int position_variation = 35;
final int size_variation = 20;
final int rotation_variation = 10;
final int alpha_variation = 30;
PImage img;
void setup() {
img = loadImage("imge.png");
}
void draw() {
Shape[] survivors = {}; // Assume that there are 100 objects in this variable
Shape[] children = {};
for(Shape shape: survivors) {
for(int child=0; child < child_count; child++) {
float x = randomVar(shape.x, position_variation, 0, img.width);
float y = randomVar(shape.y, position_variation, 0, img.height);
float sizeX = randomVar(shape.sizeX, size_variation, min_size, max_size);
float sizeY = randomVar(shape.sizeY, size_variation, min_size, max_size);
float rotation = randomVar(shape.rotation, rotation_variation, -180, 180);
float alpha = randomVar(alpha(shape.c), alpha_variation, 0, 255);
children = (Shape[]) append(children, new Shape(x, y, sizeX, sizeY, rotation, alpha));
}
}
}
float randomVar(float original, float range, float min, float max) {
return constrain(original + random(-range, range+1), min, max);
}
I tried to use a thread() that fills up children and a while to wait for it to be full, but I didn’t came up to anything working, I am not an expert at all in threads…
How can I create a lot of PGraphics at the same time and put them inside an array ?
The draw method is called ~60 times a second and is used to render the frames. It is not the place to create the shapes. You might trty the following although its difficult to test as we don’t have the Shape class code
final int child_count = 5;
final int min_size = 5;
final int max_size = 300;
final int position_variation = 35;
final int size_variation = 20;
final int rotation_variation = 10;
final int alpha_variation = 30;
PImage img;
Shape[] survivors = {}; // Assume that there are 100 objects in this variable
Shape[] children = {};
void setup() {
img = loadImage("imge.png");
for (Shape shape : survivors) {
for (int child=0; child < child_count; child++) {
float x = randomVar(shape.x, position_variation, 0, img.width);
float y = randomVar(shape.y, position_variation, 0, img.height);
float sizeX = randomVar(shape.sizeX, size_variation, min_size, max_size);
float sizeY = randomVar(shape.sizeY, size_variation, min_size, max_size);
float rotation = randomVar(shape.rotation, rotation_variation, -180, 180);
float alpha = randomVar(alpha(shape.c), alpha_variation, 0, 255);
children = (Shape[]) append(children, new Shape(x, y, sizeX, sizeY, rotation, alpha));
}
}
}
void draw() {
}
float randomVar(float original, float range, float min, float max) {
return constrain(original + random(-range, range+1), min, max);
}
The fact that it is in the draw method is not a problem, all I want is optimize the two for loops. The code I posted above is a lot shorter than my whole code.
Sorry if I don’t know how to say it clearly… "-_-
This is only a small part of my code, the whole program takes about 10 seconds to calculate everything, and then starts over.
I made a github repository if you want to understand what my program does