# Extract x, y coordinate of an arbitrary point on curve in TikZ

We have very nice Extract x, y coordinate of an arbitrary point in TikZ

\documentclass{article}
%\url{http://tex.stackexchange.com/q/33703/86}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,1);
\gettikzxy{(A)}{\ax}{\ay}
\fill[red] (\ax,\ay) circle (5pt);

\tikzset{-dot-/.style={decoration={
markings,
mark=at position #1 with {\fill circle (2pt);}},postaction={decorate}}} %%% in this line added a ;

\begin{tikzpicture}
\draw[-dot-=.5] (0,0) to [bend left] (2,4);
\draw[-dot-=.8] (0,0) to [bend right] (2,4);
\end{tikzpicture}

\end{tikzpicture}
\end{document}


Is it possible to extract x, y coordinate of a point located on the curve. These points can be used for doing some other tasks.

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother

%
\begin{document}
%
\begin{tikzpicture}
\draw (0,0) to [bend left=20]  coordinate[pos=0.7] (A)(2,4);
\draw (0,0) to [bend right=20]  coordinate[pos=0.2] (B)(2,4);
\draw[thick,red] (A) -- (B);
\gettikzxy{(A)}{\ax}{\ay}
\gettikzxy{(B)}{\bx}{\by}
\fill[blue] (\ax, \ay) circle (2pt);
\fill[blue] (\bx, \by) circle (2pt);
\draw[thick,green] (A) -- (\bx,\ay) -- (B);
\draw[thick,yellow] (A) -- (\ax,\by) -- (B);
%
\end{tikzpicture}
%
\end{document}


Thank you Andrew Stacey..

Now i got a better MWE and using

\tikzset{pontoncurve/.style={decoration={
markings,
mark=at position #1 with {\coordinate (B);}},postaction={decorate}}}


i get the coordinate. But i do not know to change B in \coordinate (B). Should i use \newcommand like \gettikzxy?

\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}

\documentclass{article}
%\url{http://tex.stackexchange.com/q/33703/86}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,1);
\gettikzxy{(A)}{\ax}{\ay}
%\fill[red] (\ax,\ay) circle (5pt);

\tikzset{-dot-/.style={decoration={
markings,
mark=at position #1 with {\fill circle (2pt);}},postaction={decorate}}} %%% in this line added a ;

\tikzset{pontoncurve/.style={decoration={
markings,
mark=at position #1 with {\coordinate (B);}},postaction={decorate}}}

\begin{tikzpicture}
\draw[pontoncurve=.5] (0,0) to [bend left] (2,4);
\gettikzxy{(B)}{\bx}{\by}
\draw[red] (\bx,\by) -- ++(5,1);

\draw[-dot-=.8] (0,0) to [bend right] (2,4);
\end{tikzpicture}

\end{tikzpicture}
\end{document}

-
One way would be to create a vertical line (at the x coordinate of where you want to extract the point) with draw=none but name it, and then use the intersections library to find the intersection of this vertical line with the given curve. An example is given in Intersections in PGFplots, and TikZ: Intersection of two lines –  Peter Grill May 18 '12 at 8:01
If you want to get the coordinate of the marked point, simply add a coordinate to the \fill command you are using in the marking. More generally, exactly how you extract the coordinates will depend somewhat on how you specify the point. How do you want to do that? –  Loop Space May 18 '12 at 8:14
Regarding your update, in the sample code you don't actually need to get the x and y coordinates of the point: by using coordinate (name) then you have a handle, name, by which you can refer to the point so you can write \draw[red] (B) -- ++(5,1); without needing \gettikzxy at all. Do you want to do anything more complicated with the coordinates than just refer to them? If not, you don't need anything more complicated than the above. –  Loop Space May 21 '12 at 7:19

You don't need the library decorations.markings and you don't need the macro gettikzxy in your examples; perhaps with other examples, it's useful to work like this.

First \pgfgetlastxy{\ax}{\ay} is enough after

 \coordinate (A) at (2,1);
\pgfgetlastxy{\ax}{\ay}


  \draw[-dot-=.5] (0,0) to [bend left]  (2,4);


you have

   \draw (0,0) to [bend left]  coordinate[pos=.5] (B)(2,4);


Now if you want to get the coordinates, you have several ways like

 \path (B);\pgfgetlastxy{\bx}{\by}


A complete code

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (2,1);
\pgfgetlastxy{\ax}{\ay}
\fill[red] (\ax,\ay) circle (5pt);
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) to [bend left]  coordinate[pos=.5] (B)(2,4);
\draw (0,0) to [bend right] coordinate[pos=.8] (C)(2,4);
\path (B);\pgfgetlastxy{\bx}{\by}
\path (C);\pgfgetlastxy{\cx}{\cy}
\draw[red,thick] (\bx,\by)--(\cx,\cy) ;
\end{tikzpicture}

\end{document}


Perhaps you need to add another example to get a better answer.

Remark

You can get the coordinates like this with the curveto operation and now with the CVS version, it's possible with the arc operation.

-