Compare data with recorded data

I would like just to compare a value of a data that I have at the actual frame, with the former value I had at the previous frame.

And do something like this
if (formerData > actualData+100)
doSomething.

Thank you.
Happy new year.

All you need to do is set up a member variable for your sketch or class (outside the functions) that tracks the formerData. Then at the end of your doSomething, remember to update your formerData with the values in your actualData.

1 Like

Tank for your answer.
I did this example below. Is this the good manner to compare actual and former value?

/**
 * Reflection 
 * by Simon Greenwold. 
 * 
 * Vary the specular reflection component of a material
 * with the horizontal position of the mouse. 
 */
float formerData;
float actualData; 


void setup() {
  frameRate (5);
  size(640, 360, P3D);
  noStroke();
  colorMode(RGB, 1);
  fill(0.4);
}

void draw() {
 
  print (formerData);
  background(0);
  translate(width / 2, height / 2);
  // Set the specular color of lights that follow
  lightSpecular(1, 1, 1);
  directionalLight(0.8, 0.8, 0.8, 0, 0, -1);
  actualData = mouseX / float(width);
  doSomething(); 
  sphere(120);

}

void doSomething() {
   specular(actualData,actualData,actualData);
   if (actualData>formerData+0.01){
   background(100);
    }
   formerData= actualData;
    print (" "); println (actualData);
  }
1 Like

Yup. I mean, i didn’t dig into the rest of your code because i assume you have it doing what you want. But as far as the formerData vs actualData comparison goes, you got it. You compare the two, do something based on the comparison, and then update the formerData for the next time around.

One suggestion would be to initialize your formerData, either when you declare it, or in setup(). Depending on what your variable is doing, initializing it to 0.0f or some safe value could prevent problems in the comparison.

if (actualData > formerData + 0.01f)
{
   ...doing things
}

In this situation, formerData is a float, but if there’s no float value actually stored there the first time through, how does it know if it’s greater than, less than, or equal to anything else?

It doesn’t look like it would cause issues in this situation, and i’m not sure if Java/Processing handles uninitialized variables better than other languages like C++. Someone who knows better than i do could probably tell you. Otherwise, it’s good practice, and the rest of your implementation is spot on.

1 Like