Now, all that's left is to define three buffers, the pointers next, curr and prev, and the initial value that triggers the wave!
As mentioned earlier, the bitwise shift operations act as an extremely efficient way to multiply or divide values by powers of two. For this reason, every multiplication or division by a power of two can and has been replaced by a bitwise shift.
Furthermore, the wave naturally moves pixel by pixel between frames. This is too slow! For quicker simulations, I have provided a bit of code that skips the generation of an image for every 8 iterations.
Here is the full code! Parts of the code that needed a bit of explanation are commented!
Ok now you've got frames. But how do you make an animation out of them?
Using ffmpeg of course!
You can easily turn all those frames into a gif image (or any other video format you want) by using the command ffmpeg -i ./frames/frame%03d.pgm waves.gif!
You can play with the values around to generate different waves starting at different places, you can have multiple waves at the same time, see how they interact with each other, play with the amplitudes, change the speed c2, and even modify the boudary conditions so the reflected wave does not become inverted! The point of this tutorial is to get you into the way of thinking of problem solving. And I hope I could at least encourage you to become a good problem solver. I hope =)
Feel free to email me for any questions, explanations, or just to say hi!