如何多次迭代同一个草图 -(处理) [英] How to iterate the same sketch multiple times -(processing)
问题描述
我在 Processing 3.5.4 中编写了一个程序.其基本结构如下:
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?
推荐答案
您可以通过几个简单的步骤完成此操作,其中一些可能需要进行一些重构:
You can do this with a couple simple steps, some of which may require some refactoring:
- 确定定义模拟结束的条件.
- 设置一个仅在模拟完成后运行的方法.我们将其称为
Reset()
以简化操作. - 在
Reset()
中,确保重新初始化所有在创建时初始化的全局变量.将它们设置回初始值. - 在
Reset()
中,运行setup()
. - 让自然顺其自然,您的应用程序已被诱骗重新开始.
- Determine the conditions which define the end of the simulation.
- Set un a method which will run only once the simulation is done. We'll call it
Reset()
to make things easier. - In
Reset()
, make sure that you reinitialize any global variables which is initialized at their creation. Set them back to their initial value. - In
Reset()
, runsetup()
. - 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.
玩得开心!
这篇关于如何多次迭代同一个草图 -(处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!