2

How can I plot this curve in Tikz

\begin{gather*}
x(t)=
\begin{cases}
  2(t-n+1),   & t\in [n-1,n-1/2] \\
  2-2(t-n+1), & t\in [n-1/2, n]
\end{cases}
\\
y (t)=
\begin{cases}
2(\sqrt{2}t-n+1),   & \sqrt{2}t\in [n-1,n-1/2] \\
2-2(\sqrt{2}t-n+1), & \sqrt{2}t\in [n-1/2, n]
\end{cases}
\end{gather*}
with $n\in \mathbb{N}$

picture of example output

2
  • 3
    Welcome! Can you show us what you have so far? You are more likely to get useful help that way.
    – cfr
    Commented Nov 26, 2015 at 23:40
  • You will need a \foreach loop for \n (assuming you want more than one value) Otherwise you just have to supply the function and domain for each of the cases. The main question is what sort of axis do you want? Commented Nov 27, 2015 at 16:35

1 Answer 1

5

First thing first : you can probably write your function in a simpler way like this : enter image description here

This is a billiard trajectory in a unit square with slope 1:sqrt(2).

Method I You can define this function using math library and plot it as usual function. The problem is that you need a big number of samples (so the compilation is slow) to obtain something usable (here I use 700!).

\documentclass[border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\tikzmath{
  function x(\t){
    int \n; \n=int(\t);
    if (\t - \n) < .5 then{
      return 2*(\t-\n);
    }
    else {
      return 2*(1+\n-\t);
    };
  };
  function y(\t){
    return x(1.41421356237*\t);
  };
}
\begin{tikzpicture}[scale=3]
  \draw[thick] plot[domain=0:5,variable=\u,samples=700] ({x(\u)},{y(\u)});
\end{tikzpicture}
\end{document}

enter image description here

Method II you can calculate the positions of all bounces and draw straight lines in between.

\documentclass[varwidth,border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
  \begin{tikzpicture}[scale=3]
      \tikzmath{
        % initial value, max value, next bounce value, speed
        \x = 0; \maxx=1; \bx=\maxx; \dx = 1;
        \y = 0; \maxy=1; \by=\maxy; \dy = 1.41421356237;
        for \i in {0,...,35}{
          % save values
          \sx = \x; \sy = \y;
          % time to next bounce
          \tx = (\bx-\x)/\dx;
          \ty = (\by-\y)/\dy;
          if \tx < \ty then { % if bounce on x before y
            \x = \bx;
            \bx = \maxx-\x;
            \dx = -\dx;
            \y = \y + \tx*\dy;
          } else { % if bounce on y before x
            \y = \by;
            \by = \maxy-\y;
            \dy = -\dy;
            \x = \x + \ty*\dx;
          };
          {\draw (\sx,\sy) -- (\x,\y);};
        }; % end for
      };
  \end{tikzpicture}
\end{document}

enter image description here

This method is faster and you can do a lot of bounces. For example if you do for \i in {0,...,300} than you obtain

enter image description here

You can customize this billiard trajectory:

  • By modifying \maxx and \maxy you can replace the unit square with arbitrary rectangle.
  • By modifying \dx and \dy you can obtain arbitrary slope (initial velocity).
  • By modifying \x and \y you can obtain arbitrary initial position.

Exercice : You can adapt the code to obtain this : enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .