How to iterate the same sketch multiple times -(processing)

独自空忆成欢 提交于 2021-01-29 11:47:01

问题


I wrote a program in Processing 3.5.4. It's basic structure is as follows:

int SOMEINITIALSTUFF;
Class[] classArrays = new Class[];

void setup() {
  Sets up the simulation to run;
  size(1200, 700);
}

void draw() {
  background(255, 200, 200);
  Runs Simulation;
  Collects data;
}

This runs fine. What I would like to do is run this program multiple times to gather some statistics.

I can't figure out how to do this. I want to essentially put this whole code into a loop, and collect the data it creates for each iteration, possibly running it thousands of times. I've tried just that, but it breaks the program. Any suggestions?


回答1:


You can do this with a couple simple steps, some of which may require some refactoring:

  1. Determine the conditions which define the end of the simulation.
  2. Set un a method which will run only once the simulation is done. We'll call it Reset() to make things easier.
  3. In Reset(), make sure that you reinitialize any global variables which is initialized at their creation. Set them back to their initial value.
  4. In Reset(), run setup().
  5. Let nature follows it's course, your application has been tricked into beginning anew.

Of course, you may want to organize your code so the information you gather won't be erased, whether by saving it to a different file/appending it to a file at every time you run a new simulation, or by preserving it in a global variable which won't be reset. I don't have enough details to elaborate on this front, but I think you'll understand the idea I'm advancing.

Have fun!



来源:https://stackoverflow.com/questions/60750890/how-to-iterate-the-same-sketch-multiple-times-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!