我想修改贝塞尔曲线tikz
,例如
\draw (0,0) .. controls +(87:2.3) and +(50:-0.7) .. (1,1);
这样,中间的部分就用点来绘制(而不是完整的实线),而起点和终点仍然是实线。我可以用下面的 MWE(见下文)从原则上解决这个问题,但参数pre length=
和post length=
必须在绝对坐标中定义。我如何更改它以指定相对位置(例如从路径长度的 0.3 到 0.8)?
具体来说,有一个\pgfdecoratedpathlength
变量(TikZ 中曲线的长度),但我不知道如何在dotted part of curve
不出错的情况下将其包含在我的定义中。或者有其他更好的方法吗?
梅威瑟:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\begin{document}
\begin{tikzpicture}[scale=1] % <- except of line/dot width drawing should be independent of scaling
\tikzset{
dotted part of curve/.style args={between #1 and #2 with color #3}{
#3,
decorate,
decoration={
markings,
mark=between positions 0 and 1 step 3*\pgflinewidth with{\fill[radius=\pgflinewidth,#3] (0,0) circle;},
pre length=#1,
post length=#2,
pre=curveto,
post=curveto,
%post=moveto, % <-- alternate end: no drawing
}
},
dotted part of curve/.default={between 0.5cm and 0.3cm with color red},
% dotted part of curve/.default={between 0.3 and 0.8 with color red}, % <-- desired: relative positions
}
\draw (0,0) .. controls +(87:2.3) and +(50:-0.7) .. (1,1);
\draw[dotted part of curve] (0,0) .. controls +(87:2.3) and +(50:-0.7) .. (1,1);
\end{tikzpicture}
\end{document}
答案1
您可以使用\pgfdecoratedinputsegmentlength
:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
dotted part of curve/.style args={between #1 and #2 with color #3}{
postaction={decorate,draw,#3,
decoration={
markings,
mark=between positions 0 and 1 step 3*\pgflinewidth
with{\fill[radius=\pgflinewidth,#3] (0,0) circle;},
pre=curveto, pre length=#1*\pgfdecoratedinputsegmentlength,
post=curveto, post length=(1-#2)*\pgfdecoratedinputsegmentlength
}}},
dotted part of curve/.default={between 0.3 and 0.8 with color red}
}
\begin{document}
\begin{tikzpicture}[scale=1]
\draw
[dotted part of curve]
(0,0) .. controls +(87:2.3) and +(50:-0.7) .. (1,1);
\end{tikzpicture}
\end{document}
结果:
如果post=curveto
替换为post=moveto
结果变为
如果背景中不应该有黑线,您可以在上面的代码中\path
使用:\draw
\path% <-
[dotted part of curve]
(0,0) .. controls +(87:2.3) and +(50:-0.7) .. (1,1);
或者
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
dotted part of curve/.style args={between #1 and #2 with color #3}{
decorate,
#3,
decoration={
markings,
mark=between positions 0 and 1 step 3*\pgflinewidth
with{\fill[radius=\pgflinewidth,#3] (0,0) circle;},
pre=curveto, pre length=#1*\pgfdecoratedinputsegmentlength,
post=curveto, post length=(1-#2)*\pgfdecoratedinputsegmentlength
}},
dotted part of curve/.default={between 0.3 and 0.8 with color red}
}
\begin{document}
\begin{tikzpicture}[scale=1]
\draw
[dotted part of curve]
(0,0) .. controls +(87:2.3) and +(50:-0.7) .. (1,1);
\end{tikzpicture}
\end{document}
答案2
对于单条贝塞尔曲线,基本层\pgfpathcurvebetweentime
命令可能很有用,而且肯定比装饰更有效。不幸的是,默认情况下没有 TikZ 界面,但下面显示了一种略显粗糙(但可用)的方法:
\documentclass[tikz,border=5]{standalone}
\tikzset{part curve/.style args={%
from #1 to #2 curve #3 .. controls #4 and #5 .. #6}{insert path={
#3 coordinate (@1) #4 coordinate (@2)
#5 coordinate (@3) #6 coordinate (@4)
\pgfextra{\pgfpathcurvebetweentime{#1}{#2}%
{\pgfpointanchor{@1}{center}}{\pgfpointanchor{@2}{center}}%
{\pgfpointanchor{@3}{center}}{\pgfpointanchor{@4}{center}}}
}}}
\begin{document}
\begin{tikzpicture}
\path
(0,0) coordinate (P1)
+(87:2.3) coordinate (P2)
(1,1) coordinate (P4)
+(50:-0.7) coordinate (P3);
\draw [help lines] (P1) .. controls (P2) and (P3) .. (P4);
\draw [red, thick,
part curve={from 0.0 to 0.3 curve (P1) .. controls (P2) and (P3) .. (P4)}];
\draw [green!50!black, thick, dotted,
part curve={from 0.3 to 0.8 curve (P1) .. controls (P2) and (P3) .. (P4)}];
\draw [blue, thick, dashed,
part curve={from 0.8 to 1.0 curve (P1) .. controls (P2) and (P3) .. (P4)}];
\end{tikzpicture}
\end{document}