1

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.

10
  • How. Can. I. Plot. On. A. Curved. Path. ?. Commented May 22, 2019 at 8:32
  • I‘m sorry, but I don‘t understand, what you want to tell me with your comment. @JouleV Commented May 22, 2019 at 8:48
  • How can I plot a e.g. x ^2 curve on a curve? Commented May 22, 2019 at 8:49
  • 1
    Unfortunately I don‘t know the english equivalent but there is a Wikipedia article in German: de.m.wikipedia.org/wiki/Krummlinige_Koordinaten . You can think of the y axis moving along the path in a way that it always stands orthogonal to the tangent (x axis) Commented May 22, 2019 at 8:54
  • But ploting along curved paths is not necessary! I would also be happy with straight paths! Commented May 22, 2019 at 8:55

1 Answer 1

3

This plots along straight lines.

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}[%gridded, 
            every path/.style={thick, red ,fill, fill opacity=.5},
            every plot/.style={smooth},
            plot along line/.style args={from #1 to #2}{insert
            path={
            let \p1=($#2-#1$),\n1={veclen(\x1,\y1)/1cm},\n2={atan2(\y1,\x1)}
            in   [shift={#1},rotate=\n2,domain=0:\n1]
            }}]

        \begin{scope}
            \draw {[plot along line=from {(0,1)} to {(4,4)}] 
            plot (\x,0.05*\x^2) }-- (4,4) -- cycle;
            \draw[black] (0,1) -- (4,4);
        \end{scope}

        \begin{scope}[xshift=5cm]
            \draw[plot along line=from {(0,3)} to {(4,2)}] plot (\x,{0.5*sin(3*\x r)})
            coordinate (end) --cycle;
            \draw[black] (0,3) -- (end);
        \end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

You can also add styles that plot along curved coordinate systems, using \usepgfmodule{nonlineartransformations} \usepgflibrary{curvilinear}.

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{calc}
\usepgfmodule{nonlineartransformations} 
\usepgflibrary{curvilinear}
\begin{document}
\begin{tikzpicture}
  \draw [help lines] (0,0) grid (3,2);
  {
    \pgfsetcurvilinearbeziercurve
      {\pgfpoint{0mm}{20mm}}
      {\pgfpoint{11mm}{20mm}}
      {\pgfpoint{20mm}{11mm}}
      {\pgfpoint{20mm}{0mm}}
\makeatletter     
\pgftransformnonlinear{\pgfpointcurvilinearbezierorthogonal\pgf@x\pgf@y}%
\makeatother
\draw (0,-30pt) grid [step=10pt] (80pt,30pt); 
\draw[blue,thick] plot[domain=0:2.8,samples=51] ({\x},{sin(180*\x)});
}
  \draw[red, very thick]
    (0mm,20mm) .. controls (11mm,20mm) and (20mm,11mm) .. (20mm,0mm);
\end{tikzpicture}
\end{document}

enter image description here

This example is more or less taken from the pgfmanual except that I added a plot of a function. However, I do not know what precisely you expect to be done? How do you specify the coordinate system?

1
  • This is exactly what I was searching for. I think the second example is perfect, as I wanted to plot a function along a half circle. Thank you! Commented May 23, 2019 at 5:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.