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.

Define $g_{[a,b]}(x)$ as: \begin{equation*} g_{[0,1]}(x)=\begin{cases} \{x\} &\text{if} \ [x] &\text{is even} \\ 1-\{x\} &\text{if} \ [x] &\text{is odd} \end{cases} \end{equation*}
$$g_{[a,b]}(x)= a + (b-a)g_{[0,1]}(\frac{x-a}{b-a})$$
where $[x]$ and {x} denote the integral and fractional part of $x$.
This is a simple linear bounce function between $0$ and $1$, and trough rescaling and translation, between $a$ and $b$. To add bounce to an arbitrary $f$, simply define: $$f_{[a,b]}(x) = g_{[a,b]}(f(x))$$
Then $f_{[a,b]}$ will bounce $f$ between $a$ and $b$. So to add bounce between $a$ and $b$ to a function $f$, compose the simple linear bouncer $g_{[a,b]}$ with $f$.