Skip to main content
added 370 characters in body
Source Link
Wulle
  • 1.3k
  • 1
  • 9
  • 19

I'm trying to automate drawing filled plots along (mostly straight) paths. A simple example of what I'm trying to achieve:

\documentclass[border=2cm]{standalone}  
\usepackage{tikz}

\usetikzlibrary{backgrounds}

\begin{document}
    \begin{tikzpicture}[gridded, 
            every path/.style={thick, red ,fill, fill opacity=.5},
            every plot/.style={smooth}]
            
        \begin{scope}
            \draw (0,1) -- plot[domain=0:5, shift={(0,1)}, rotate={atan(3/4)}] (\x,0.05*\x^2) -- (4,4) -- cycle;
            \draw[black] (0,1) -- (4,4);
        \end{scope}

        \begin{scope}[xshift=5cm]
            \draw (0,3) -- plot[domain=0:sqrt(17), shift={(0,3)}, rotate={atan(-1/4)}] (\x,{0.5*sin(3*\x r)}) -- (4,2) -- cycle;
            \draw[black] (0,3) -- (4,2);
        \end{scope}
    \end{tikzpicture}
\end{document}

Expected Result

But how can I automate the process (maybe even for curved paths?) How can I

  • Shift the plot to the last coordinate (I tried it with \pgfextractxy, but was not able to get it to work)
  • Rotate it (Works quite ok with the atan(), but what about vertical paths?)
  • Set the domain to the length of the path (Tried it with the let-option and the veclen command, but I got the "dimension too large"-error)
  • Have it implemented, so that I can repeat it many times (e.g. \draw (A) -- plot[on path] (\x, <function>) -- (B))

Any ideas are appreciated!

Edit

As it seems that my intentions were not very clear: At the moment I would be happy with a solution for straight paths only. (The option for curved paths would be nice to have, though) So my goal is to have a simple method plotting a function between two given points on the canvas without having to recalculate my rotation, shift and domain every time.

I'm trying to automate drawing filled plots along (mostly straight) paths. A simple example of what I'm trying to achieve:

\documentclass[border=2cm]{standalone}  
\usepackage{tikz}

\usetikzlibrary{backgrounds}

\begin{document}
    \begin{tikzpicture}[gridded, 
            every path/.style={thick, red ,fill, fill opacity=.5},
            every plot/.style={smooth}]
            
        \begin{scope}
            \draw (0,1) -- plot[domain=0:5, shift={(0,1)}, rotate={atan(3/4)}] (\x,0.05*\x^2) -- (4,4) -- cycle;
            \draw[black] (0,1) -- (4,4);
        \end{scope}

        \begin{scope}[xshift=5cm]
            \draw (0,3) -- plot[domain=0:sqrt(17), shift={(0,3)}, rotate={atan(-1/4)}] (\x,{0.5*sin(3*\x r)}) -- (4,2) -- cycle;
            \draw[black] (0,3) -- (4,2);
        \end{scope}
    \end{tikzpicture}
\end{document}

Expected Result

But how can I automate the process (maybe even for curved paths?) How can I

  • Shift the plot to the last coordinate (I tried it with \pgfextractxy, but was not able to get it to work)
  • Rotate it (Works quite ok with the atan(), but what about vertical paths?)
  • Set the domain to the length of the path (Tried it with the let-option and the veclen command, but I got the "dimension too large"-error)
  • Have it implemented, so that I can repeat it many times (e.g. \draw (A) -- plot[on path] (\x, <function>) -- (B))

Any ideas are appreciated!

I'm trying to automate drawing filled plots along (mostly straight) paths. A simple example of what I'm trying to achieve:

\documentclass[border=2cm]{standalone}  
\usepackage{tikz}

\usetikzlibrary{backgrounds}

\begin{document}
    \begin{tikzpicture}[gridded, 
            every path/.style={thick, red ,fill, fill opacity=.5},
            every plot/.style={smooth}]
            
        \begin{scope}
            \draw (0,1) -- plot[domain=0:5, shift={(0,1)}, rotate={atan(3/4)}] (\x,0.05*\x^2) -- (4,4) -- cycle;
            \draw[black] (0,1) -- (4,4);
        \end{scope}

        \begin{scope}[xshift=5cm]
            \draw (0,3) -- plot[domain=0:sqrt(17), shift={(0,3)}, rotate={atan(-1/4)}] (\x,{0.5*sin(3*\x r)}) -- (4,2) -- cycle;
            \draw[black] (0,3) -- (4,2);
        \end{scope}
    \end{tikzpicture}
\end{document}

Expected Result

But how can I automate the process (maybe even for curved paths?) How can I

  • Shift the plot to the last coordinate (I tried it with \pgfextractxy, but was not able to get it to work)
  • Rotate it (Works quite ok with the atan(), but what about vertical paths?)
  • Set the domain to the length of the path (Tried it with the let-option and the veclen command, but I got the "dimension too large"-error)
  • Have it implemented, so that I can repeat it many times (e.g. \draw (A) -- plot[on path] (\x, <function>) -- (B))

Any ideas are appreciated!

Edit

As it seems that my intentions were not very clear: At the moment I would be happy with a solution for straight paths only. (The option for curved paths would be nice to have, though) So my goal is to have a simple method plotting a function between two given points on the canvas without having to recalculate my rotation, shift and domain every time.

Source Link
Wulle
  • 1.3k
  • 1
  • 9
  • 19

Plot along path in TikZ

I'm trying to automate drawing filled plots along (mostly straight) paths. A simple example of what I'm trying to achieve:

\documentclass[border=2cm]{standalone}  
\usepackage{tikz}

\usetikzlibrary{backgrounds}

\begin{document}
    \begin{tikzpicture}[gridded, 
            every path/.style={thick, red ,fill, fill opacity=.5},
            every plot/.style={smooth}]
            
        \begin{scope}
            \draw (0,1) -- plot[domain=0:5, shift={(0,1)}, rotate={atan(3/4)}] (\x,0.05*\x^2) -- (4,4) -- cycle;
            \draw[black] (0,1) -- (4,4);
        \end{scope}

        \begin{scope}[xshift=5cm]
            \draw (0,3) -- plot[domain=0:sqrt(17), shift={(0,3)}, rotate={atan(-1/4)}] (\x,{0.5*sin(3*\x r)}) -- (4,2) -- cycle;
            \draw[black] (0,3) -- (4,2);
        \end{scope}
    \end{tikzpicture}
\end{document}

Expected Result

But how can I automate the process (maybe even for curved paths?) How can I

  • Shift the plot to the last coordinate (I tried it with \pgfextractxy, but was not able to get it to work)
  • Rotate it (Works quite ok with the atan(), but what about vertical paths?)
  • Set the domain to the length of the path (Tried it with the let-option and the veclen command, but I got the "dimension too large"-error)
  • Have it implemented, so that I can repeat it many times (e.g. \draw (A) -- plot[on path] (\x, <function>) -- (B))

Any ideas are appreciated!