This is a follow-up question to this one. My goal is to draw a vertical line to any path or a function, but the main requirement is to avoid temporal paths that are used to get the intersection.
And that's my attempt to define a macro that finds the intersection point, but it doesn't work:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}
\makeatletter
\newcommand\topath[1]{
\path[name path=t] (\the\tikz@lastxsaved,\pgfkeysvalueof{/pgfplots/ymin}) -- (\the\tikz@lastxsaved,\pgfkeysvalueof{/pgfplots/ymax});
\coordinate[name intersections={of=#1 and t}] at (intersection-1)
}
\newcommand\currentcoordinate{\the\tikz@lastxsaved,\the\tikz@lastysaved}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
samples=200,
clip=false
]
\def\ymaxv{\pgfkeysvalueof{/pgfplots/ymax}}
\addplot[blue, name path=f, restrict y to domain=-3:5] {x^4-3*x^2+x+2};
% ↓ doesn't work ↓
%\draw[red] (1,0) -- \topath{f};
%\draw[red] (-1.5,0) -- \topath{f};
\end{axis}
\end{tikzpicture}
%
\qquad
%
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
samples=200,
clip=false
]
\addplot[blue, name path=b, smooth, tension=1] coordinates {(0,1) (1,2) (1,0) (0.5,0) (0.5,1) (1.5,0.5) (2,0.7)};
% ↓ doesn't work ↓
%\draw[red] (0.2,0) -- \topath{b};
%\draw[red] (1.5,0) -- \topath{b};
\end{axis}
\end{tikzpicture}
\end{document}
\drawtopath{\draw[red] (0.2,0) -- }{b}
to what you want is possible. Is that okay?