Update screen from recursive function
in
Programming Questions
•
1 year ago
Hi all, I have what is perhaps a strange question: I'm enumerating combinations using a recursive function. I'd like to get one combination, do something with it, then the next, do something, etc. The problem is that draw() updates the window only when done. Is there a way to draw and update the screen outside the draw() function? I'm thinking there might be a Java method that skips the draw()...?
Here is a shorter version of my code to demonstrate:
Here is a shorter version of my code to demonstrate:
- String input = "0123";
int len = 3;
void setup() {
size(300, 300);
noLoop();
}
void draw() {
background(255);
line(0,0, width,height);
recursiveCombinations(input, len, new String());
}
// the recursive function which generates all combinations in one go
void recursiveCombinations(String input, int depth, String output) {
if (depth == 0) {
// not sure why -'0' works to cast to an int... but it does!
int r = (output.charAt(0) - '0') * (255/input.length()); // scale to 0-255 based on input size
int g = (output.charAt(1) - '0') * (255/input.length());
int b = (output.charAt(2) - '0') * (255/input.length());
println(r + "\t" + g + "\t" + b);
// should write the text to the screen and wait 2 seconds before continuing...
fill(0);
text(r + ", " + g + ", " + b, 20, height/2);
delay(2000);
}
else {
for (int i=0; i<input.length(); i++) {
output += input.charAt(i);
recursiveCombinations(input, depth-1, output);
output = output.substring(0, output.length() - 1);
}
}
}
1