Skipping a certain number of frames on the timeline

I have a math problem that is difficult to describe, but I'll try anyway.

in the timeline, I have several frames from which I want to skip a certain number of frames, which should be evenly spaced across the timeline.

for example, I have 10 frames and I want to skip 5, then the solution is easy: we skip every second frame. 10/5 = 2

if (frame%2 == 0)
    skip();

      

but what if the above division results in a floating number?

for example in 44 frames I want to skip 15 times. how can i determine 15 frames to be skipped?

Basically I'm looking for an algorithm that distributes those 15 frames as evenly as possible across 44 frames. it will probably look like something like skips after 2 and then after 3 frames alternately.

thanks!

+2


a source to share


4 answers


You can store an extra floating point value, t, that matches the next frame to skip, and update it every time you skip. Like this:



int frame_num = 44; 
int skips_num = 15; 
double dt =((double)(frame_num))/skips_num;
double t = 0.0;
...
// then, in your loop
if (frame == (int)t) {  
  skip();   
  t += dt; 
}

      

+3


a source


You obviously can't always do this really evenly if the number of frames you want to skip is not a divisor of the total number of frames. So if it's important to always skip at least the number of frames you want, follow the floor under (totalFrames / framesToSkip).

Having said that, you can apply the same trick you suggested.

if (totalFrames % floor(totalFrames/framesToSkip) == 0)
    skip();

      



Note that in the case of 44 and 15 you are missing much more 15. Another possibility is to place instead of the floor, in which case you will not miss 15 frames, except for 14 frames.

Another solution is to make a round to the nearest integer, this is the best approximation, however you sometimes miss more than you want and sometimes less than you want, but on average you have better results.

+2


a source


It seems to me that the blending problem is skipped with non-skips is a bit like the problem of blending steps that go diagonally downward, with steps that go straight when drawing a line with pixels. The linear drawing problem is solved in great detail at http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

+2


a source


I went off to write a solution that improved on Igor, but at the end the only improvement I actually found was to explicitly state what round(t)

should be used instead (int)t

. So here's Igor's solution again (with some variable names changed so it's more obvious and uses a round function).

int frame_num = 44; 
int skips_num = 15; 
double frames_between_skips =((double)(frame_num))/skips_num;
double next_skip = 0.0;
...
// then, in your loop
if (current_frame == round(next_skip)) {  
  skip();   
  next_skip += frames_between_skips; 
}

      

Using round(t)

will make it fairest and will try to discard the frame (int) that is closest to the perfect frame (double) while keeping the full precision timer ( next_skip

).

A few notes

  • take care if the frame starts at 0 or 1 and adjusts accordingly,
  • take care that frame_num is not> skips_num (if you increase next_skip

    more than a frame every time the frame falls, otherwise you will stop increasing next_skip

    and there will be no more frames going down)
+1


a source







All Articles