这个问题是这个问题的后续
这个想法是如何绘制一个矢量,即 50% 实线和 50% 虚线。percusse 建议使用这个解决方案
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[mydashed/.style={dashed,dash phase=3pt}]
\draw[very thin] (0,-1) -- (0,1);
\draw[ultra thick] (-2,0) -- (0,0);
\draw[ultra thick,mydashed,->] (0,0) -- (2,0);
\end{tikzpicture}
\end{document}
但他也提到“要一次性绘制组合矢量,可以使用装饰“。我已阅读手册,但找不到答案如何做到这一点。有人可以告诉我怎么做吗?
虚线部分必须从白色部分开始。
答案1
你可以从下一个代码开始。使用show path construction
装饰是从马克·维布罗回答复杂的 tikz 矢量图(转弯产生的近似误差距离)
我希望你能改进装饰方式line width
和arrow
设置方式。
\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,arrows.meta}
\tikzset{%
half dashed/.style={
decoration={show path construction,
lineto code={
\draw[#1] (\tikzinputsegmentfirst) --($(\tikzinputsegmentfirst)!.5!(\tikzinputsegmentlast)$);,
\draw[dashed, dash phase=3pt,->,#1] ($(\tikzinputsegmentfirst)!.5!(\tikzinputsegmentlast)$)--(\tikzinputsegmentlast);,
}
},
decorate
},
}
\begin{document}
\begin{tikzpicture}[>=LaTeX]
\draw (0,0) rectangle (2,2);
\draw (2,0) rectangle (4,2);
\draw [half dashed={very thin}] (0,1)--(4,1);
\draw [half dashed={ultra thick}] (0,1.5)--(4,1.5);
\draw [half dashed={line width=1mm}] (0,0.5)--(4,0.5);
\end{tikzpicture}
\end{document}
答案2
在https://tex.stackexchange.com/users/3235/percusse根据我的建议我提出了这个低级解决方案:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{decorations}
\makeatletter
\pgfdeclaredecoration{halffull}{initial}{%
\state{initial}[width=0.5*\pgfdecoratedpathlength,next state=final]{%
\pgfsetlinewidth{\pgflinewidth}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{0.5*\pgfdecoratedpathlength}{0}}
\pgfusepathqstroke
\pgfsetlinewidth{\tikzscope@linewidth}}
\state{final}{%
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{0.5*\pgfdecoratedpathlength}{0}}}}
\makeatother
\begin{tikzpicture}
\draw[ultra thick,dashed,dash phase=3pt,decorate,decoration=halffull,->] (-2,0) -- (2,0);
\draw[very thin] (0,-1) -- (0,1);
\end{tikzpicture}
\end{document}