How to make a function "bounce" from two horizontal lines? 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.
 A: 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$.
A: If the bounding lines are $y = a$ and $y = b$, then what you are probably looking for is the function $$y = \frac{b - a}{\pi}\Big(\arccos(\cos \pi x)\Big) + m.$$
You can modify the angle of reflection by changing $\pi x$ to $\frac{\pi x}{c}$ for a nonzero $c$ where $c$ is the slope of the line from the origin.
Check the graph from Desmos through this link here.

Update:
I can't find a solution right now to get all functions, even the endpoints.
This only works for polynomials, roots, and compositions of both.
Let $f$ and $g_n$ be the original function and the function defined for the interval before the $n$th interval. Then, the function must be $$f_n(x) = \left(-1\right)^{p+1} g(x) +n(b - a)$$ where $$n=(p-(1/2)+(1/2)(-1)^{p})(-1)^{p}$$ and $p$ is an integer starting from $1$.
Try to replace $f(x)$ in this Desmos graph and see if it works under certain conditions.

Let's test a function, $g(x) = 3(x - \sqrt{x})^2$ with boundaries $y = 0$ and $y = 2$ for $p$ up to $9$.

