如何定义动线?

如何定义动线?

在下面的代码中,我画了两条平行线。我想知道如何定义一条从蓝线到红线的移动线,从而产生动画。

在此处输入图片描述 代码如下,

\documentclass{standalone}
\usepackage{tikz, pgfplots}
\usetikzlibrary{arrows}

\let\Oldvec\vec \renewcommand{\vec}[1]{\ensuremath{\mathbf{#1}}}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw [->] (-4,0) -- (4,0); 
\draw [->] (0,-4) -- (0,4);
 \tiny\node[blue,label={0:$\vec{x}$}] at (4,0) {};
 \tiny\node[blue,label={0:$\mathcal{F}$}] at (0,4) {};
 %%%%%%%%%%%%%%%%%%%%%%%%
  \tiny\node[blue,label={0:$\mathcal{F}(\vec{x})$}] at (-3,3) {};
 \tiny\node[blue,label={0:$\left\langle \vec{x},\vec{y} \right\rangle $}] at (1,2) {};
  \tiny\node[blue,label={0:$\left\langle \vec{x},\vec{y} \right\rangle - \mathcal{F}(\vec{x})$}] at (1.5,0.7) {};
   %%%%%%%%%%%%%%%%%%%%%%%%

 \draw [<->] (1.3,0.7) -- (1.3,1.3);

\begin{axis}[
    anchor=origin,  % Align the origins
    x=1cm, y=1cm,   % Set the same unit vectors
    hide axis
]
\addplot[smooth] coordinates {
(-3,4)
(-2,-1.5)
(-1,3)
(1,0.5)
(4,4.5)
}; 

\addplot[ blue] coordinates {(-4,-4) (4,4)};
\addplot[ red] coordinates {(-4,-4.6) (4,3.4)};
\end{axis}


\end{tikzpicture}
\end{document}

答案1

使用animate包:

\documentclass{standalone}
\usepackage{animate}
\usepackage{tikz, pgfplots}
\usetikzlibrary{arrows}

\let\Oldvec\vec \renewcommand{\vec}[1]{\ensuremath{\mathbf{#1}}}
\begin{document}

\begin{animateinline}[controls]{8}
  \multiframe{21}{rYa=-4+-0.03,rYb=4+-0.03}{
    \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
    \draw [->] (-4,0) -- (4,0);
    \draw [->] (0,-4) -- (0,4);
    \tiny\node[blue,label={0:$\vec{x}$}] at (4,0) {};
    \tiny\node[blue,label={0:$\mathcal{F}$}] at (0,4) {};
    %%%%%%%%%%%%%%%%%%%%%%%%
    \tiny\node[blue,label={0:$\mathcal{F}(\vec{x})$}] at (-3,3) {};
    \tiny\node[blue,label={0:$\left\langle \vec{x},\vec{y} \right\rangle $}] at (1,2) {};
    \tiny\node[blue,label={0:$\left\langle \vec{x},\vec{y} \right\rangle - \mathcal{F}(\vec{x})$}] at (1.5,0.7) {};
    %%%%%%%%%%%%%%%%%%%%%%%%

    \draw [<->] (1.3,0.7) -- (1.3,1.3);

    \begin{axis}[
        anchor=origin,  % Align the origins
        x=1cm, y=1cm,   % Set the same unit vectors
        hide axis
    ]
    \addplot[smooth] coordinates {
    (-3,4)
    (-2,-1.5)
    (-1,3)
    (1,0.5)
    (4,4.5)
    };

    \addplot[ blue, dashed] coordinates {(-4,-4) (4,4)};
    \addplot[ blue] coordinates {(-4,\rYa) (4,\rYb)};
    \addplot[ red] coordinates {(-4,-4.6) (4,3.4)};
    \end{axis}
    \end{tikzpicture}
  }
\end{animateinline}
\end{document}

相关内容