Compress an Array to a smaller one for drawing graphs

Without wanting to hijack the neighboring thread on the topic of Array.add(), this topic brings me right to my current brain teaser:

I have a data field that shows the rolling average graph for a certain period of time. A user has asked me not to make the graph a rolling average, but rather to show the complete activity. Problem: the duration of which is of course unknown. At first, I thought I could solve this with array.add(), but after studying the neighboring thread, I now want to do it this way:

Let‘s assume:
-it‘s about elevation
-to keep it simple: at the beginning - each second a new value
-the width of the graph is 240 pixel

At the beginning, I fill the graph from right to left. After 240 seconds, the graph is complete. I use an array with 240 elements for this.

Then a new value is to be added at the right edge. Now the previous 240 values at the left must be reduced to 239 and the new value becomes the [0] value. So I have to average/edit 240 values in such a way that the original 240-wide graph is compressed to a width of 239.

And I have to do this over and over again so that the elevation graph always show the entire activity. However, for the new value[0] at the right edge, an increasingly larger average elevation value must be formed over time, because each pixel of the graph represents a larger time span over time. However, this is easy to accomplish and not the problem.

My problem is: How can I turn 240 values into 239?
I'm racking my brains, but I can't find a solution for how to implement this.

Or is there a completely other way to handle this?

  • Original algo, with fix:

    Linear interpolation:

    EDIT: maybe they don't look that different with this data but I'm pretty sure the standard linear interpolation algo is better than the first one that i randomly cooked up

  • Once again - have many thanks for your efforts! Very appreciated! ThumbsupThumbsupThumbsup

    Will insert the new code tomorrow!

  • Now the linear algorithm works perfectly—thank you! I would never have been able to solve this problem on my own! And by halving the data after filling a predetermined array (suggestion in another thread), I should now be able to draw a graph for activities of any length. Thumbsup

    (…and indeed, the original title of the thread „Dynamic Array sizes #2“ now has nothing to do with the content of the thread. Thrrefore I have changed the thread title)