是否有可能在 TikZ 中计算一般曲线的长度?
这个问题的答案将解决TikZ 虚线和闭合曲线如下:设lc
是曲线的长度,是lp
虚线图案的期望长度。我们要做的就是将虚线图案重新缩放为长度lc/round(lc/lp)
。
答案1
改良版:
您可以使用装饰和\pgfdecoratedpathlength
:
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\makeatletter
\tikzset{%
measureme/.style={
decoration={
markings,
mark=at position 1 with {\node[below,black,anchor=west] {My length is \pgfdecoratedpathlength};},
},
postaction=decorate
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[line width=2pt]
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) -- (B);
\begin{scope}[yshift=-1cm]
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) arc[start angle=180,end angle=0,radius=10pt];
\end{scope}
\begin{scope}[yshift=-2.5cm]
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) .. controls(0.25,2) and (0.75,-2) .. (B);
\end{scope}
\begin{scope}[yshift=-5cm]
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) .. controls(0.25,7) and (0.75,-7) .. (B);
\end{scope}
\end{tikzpicture}
\end{document}
初始版本:
这是一个使用装饰和钥匙的选项distance from start
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\makeatletter
\tikzset{%
measureme/.style={
decoration={
markings,
mark=at position -.01pt with {%
\pgfkeysgetvalue{/pgf/decoration/mark info/distance from start}\@totallength
\global\let\@totallength\@totallength
},
mark=at position 0 with {\node[black] {\@totallength};},
},
postaction=decorate
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) -- (B);
\begin{scope}[xshift=3cm]
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) .. controls(0.25,2) and (0.75,-2) .. (B);
\end{scope}
\begin{scope}[xshift=6cm]
\coordinate (A);
\coordinate (B) at (20pt,0);
\draw[cyan,measureme] (A) .. controls(0.25,7) and (0.75,-7) .. (B);
\end{scope}
\end{tikzpicture}
\end{document}