Draw Partially Dashed Vector Using Decoration

tikz-pgf

This question is sequel to this question

Dash pattern that starts with space

enter image description here

The idea is how to draw a vector that is, say 50% full line and 50% dashed line. percusse suggested this solution

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

but he also mentioned that "to draw the combined vector in one go, one can use a decoration". I have read manual and find no answer how to do that. Can someone show me how to do that?

Dashed part must start with white part.

Best Answer

You can start with next code. Using show path construction decoration was stolen from Mark Wibrow's answer to Complicated tikz vector diagram (Approximate Miss Distance Produced by Turns)

I hope you'll improve how line width and arrow are set in decoration.

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

enter image description here