提取TikZ中曲线上任意点的x,y坐标

提取TikZ中曲线上任意点的x,y坐标

我们有非常好的 提取 TikZ 中任意点的 x,y 坐标

\documentclass{article}
%\url{https://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}

是否可以提取曲线上某个点的 x、y 坐标?这些点可用于执行其他任务。

新答案:感谢大家

\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}  

在此处输入图片描述


谢谢安德鲁·斯泰西......

现在我获得了更好的 MWE 并使用:

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

我得到了坐标。但我不知道该如何更改\coordinate (B)我应该使用\newcommandlike吗\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{https://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}

答案1

您的示例中不需要库decorations.markings,也不需要宏gettikzxy;也许对于其他示例来说,像这样工作很有用。

\pgfgetlastxy{\ax}{\ay}足够后

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

代替

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

你有

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

现在如果你想获取坐标,你可以通过以下几种方式获取

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

完整的代码

\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}  

也许您需要添加另一个示例才能得到更好的答案。

评论

您可以使用曲线操作获得这样的坐标,现在使用 CVS 版本,可以使用弧操作。

相关内容