我正在绘制一条封闭路径,我想要的是将每个段的长度对齐到路径段上或旁边。手动操作是一项繁琐的工作,但它可以像这样工作:
\documentclass[tikz]{standalone}
\usetikzlibrary{spath3}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\path[spath/save=test] (0mm,0mm)
-| ++(10mm,20mm)node[sloped,pos=0.25]{10mm}node[sloped,pos=0.75]{20mm}
-| ++(15mm,10mm)node[sloped,pos=0.25]{15mm}node[sloped,pos=0.75]{10mm}
-| ++(-20mm,15mm)node[sloped,pos=0.25]{20mm}node[sloped,pos=0.75]{15mm}
-- ++(-5mm,0mm)node[sloped,pos=0.5]{5mm}
;
\draw[spath/use=test,orange,thick] [spath/transform={test}{xscale=-1}, spath/use={test, reverse, move, weld}] -- cycle;
\end{tikzpicture}
\end{document}
现在如何实现自动化呢?
还有一个附加问题:如何将其应用于变换后的路径?
答案1
问题的第一部分可以用库show path construction
中包含的修饰来回答decorations.pathreplacing
。它可能可以推广到曲线段。但是,我不知道如何将其与一起使用spath3
。
\documentclass[tikz]{standalone}
\usetikzlibrary{spath3}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[measure straight/.style={
decoration={show path construction,
lineto code={
\path let \p1=($(\tikzinputsegmentfirst)-(\tikzinputsegmentlast)$),
\n1={scalar(sqrt((\x1)*(\x1)+(\y1)*(\y1))/1mm)}
in
(\tikzinputsegmentfirst) --
node[midway,sloped]{\pgfmathparse{\n1}%
\pgfmathprintnumber\pgfmathresult mm}
(\tikzinputsegmentlast);
}},decorate
}]
\draw[postaction=measure straight] (0mm,0mm)
-| ++(10mm,20mm)
-| ++(15mm,10mm)
-| ++(-20mm,15mm)
-- ++(-5mm,0mm)
;
\end{tikzpicture}
\end{document}
感谢大家的评论!这是一个带有 的版本closepath
,正如建议的那样,它避免了“尺寸太大”的问题,如下所述。此外,它还可以处理曲线。可能markings
也可以用于直线段。
\documentclass[tikz]{standalone}
\usetikzlibrary{spath3}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fpu}
\begin{document}
\begin{tikzpicture}[measure segments/.style={
decoration={show path construction,
lineto code={
\path let \p1=($(\tikzinputsegmentfirst)-(\tikzinputsegmentlast)$)
in
(\tikzinputsegmentfirst) --
node[midway,sloped]{%
\pgfkeys{/pgf/fpu}%
\pgfmathparse{scalar(sqrt((\x1)*(\x1)+(\y1)*(\y1))/1mm)}%
\pgfmathprintnumber\pgfmathresult mm}
(\tikzinputsegmentlast);
},
curveto code={
\path[decorate,{decoration={markings,
mark=at position 0.5 with {%
\node[transform shape]{\pgfkeys{/pgf/fpu}%
\pgfmathparse{scalar(\pgfdecoratedpathlength/1mm)}%
\pgfmathprintnumber\pgfmathresult mm}
;}
}}]
(\tikzinputsegmentfirst) .. controls
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
.. (\tikzinputsegmentlast);
},
closepath code={
\path let \p1=($(\tikzinputsegmentfirst)-(\tikzinputsegmentlast)$)
in
(\tikzinputsegmentfirst)
node[midway,sloped]{%
\pgfkeys{/pgf/fpu}%
\pgfmathparse{scalar(sqrt((\x1)*(\x1)+(\y1)*(\y1))/1mm)}%
\pgfmathprintnumber\pgfmathresult mm}
(\tikzinputsegmentlast);
}},decorate
}]
\draw[postaction=measure segments] (0mm,0mm)
-| ++(10mm,20mm)
-| ++(15mm,10mm)
-| ++(-20mm,15mm)
-- ++(-5mm,0mm)
-| ++(-20mm,150mm)
to[out=90,in=90] ++ (40mm,0)
to[out=-90,in=180] ++ (40mm,40mm)
;
\end{tikzpicture}
\end{document}