I want to be able to transform a function $f$ in such a way so that it will "bounce" infinitely from two horizontal lines($y=a$ and $y=b$), as if $f$ dictates the path of a light beam, and the horizontal lines represents walls that the light can reflect off of. I would like the transformed function to be $g$, in terms of $f$. hopefully that makes sense.
For example, if $f(x)=x$, $a=0$, and $b=1$, then $g$ will form infinitely many v-shaped patterns between 0 and 1.
im thinking it might have something to do with mod or absolute value, but i have been unsuccessful so far.
i also looked into this: How to make ANY function to repeat periodically?, but it wasnt exactly what i was looking for.
EDIT: The light beam analogy might be a little misleading as it implies straight lines, but i want a solution that works for any function f, not just linear.