[SOLVED] Uneven Data Entry by sketch

Hello all,

In my sketch, I have a bunch of balls bouncing around. I am saving the X, Y values of each ball, and the XSpeed, and YSpeed value of each ball every time draw() runs.

I use the following code :

PrintWriter hist1;
PrintWriter hist2;

hist1 = createWriter("spatial_density.txt");
hist2 = createWriter("Velocity_distribution.txt");
void draw(){
  
  background(255);

  for (int i = 0; i < elec.length; i++){
    elec[i].update(elec);
    elec[i].move();
    elec[i].bounce();
    hist1.println(nf(elec[i].getX(),0,2) +","+ nf(elec[i].getY(),0,2) );
    hist2.println(nf(elec[i].getDX(),0,2) +","+ nf(elec[i].getDY(),0,2) );
   
  }
  hist1.println("------------ F " + j);
  hist2.println("-------------F " + j);
  j+=1;

At first I thought I would end the program after j reaches a threshold value, however even that produces uneven data in the text files: i.e., for example, spatial_density file will have, say, data up to j = 800, and the velocity_distribution, only 600.

I also used the time module to exit the program after a certain time has elapsed, but that had the same results.

My canvas is 700*600 big and the balls are 2 pixels wide.

I would want the data files to be of even order ie : x,y and xpeed, yspeed value of all balls upto j = same value.

What seems to be the problem?

1 Like

start with the reference
https://processing.org/reference/createWriter_.html
and basically test first with 2 files and the same content ( and test it real slow )

PrintWriter output1,output2;

int j,posx,posy;

void setup() {     // Create a new file in the sketch directory
  output1 = createWriter("data/pos1.txt");
  output2 = createWriter("data/pos2.txt");
  frameRate(1);
}

void draw() {
  output1.println("x "+posx+" y " +posy);   // Write the coordinate to the file
  output2.println("x "+posx+" y " +posy);   // Write the coordinate to the file
  println("x "+posx+" y " +posy);   // Write the coordinate to the console
  posx++;
  posy++;
  j++;
  if ( j >100 ) finish();
}

void finish() {
  output1.flush(); // Writes the remaining data to the file
  output1.close(); // Finishes the file
  output2.flush(); // Writes the remaining data to the file
  output2.close(); // Finishes the file
  exit(); // Stops the program
}

that works nice here: ( processing 3.5.3 // OS: Raspbian )
so my question is how you do the stop?

2 Likes

Hello @kll,

Thank you for your reply. It seems that I was not “writing the remaining data” to the file before the program closed, and the file.

The “finish” program is just what I needed.