This needs a bit more math to make the pattern balanced/symmetrical …
Idea: Use the decorations
library to get the length of the path and create a custom dash pattern that fits.
More work is needed for any dash pattern (that has more than one on
and one off
part) or any combination of dash patterns. Besides fractions of the path lengths one could even use absolute lengths (say, if only 1cm should be dotted and not 30% of the middle).
Code
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{decorations}
\makeatletter
\def\pgfutil@Repeat#1#2{#2\ifnum#1>0
\expandafter\pgfutil@firstofone\else\expandafter\pgfutil@gobble\fi
{\expandafter\pgfutil@Repeat\expandafter{\the\numexpr#1-1\relax}{#2}}}
\tikzset{
dash between/.code args={#1 and #2}{%
\tikz@addoption{%
\pgfgetpath\currentpath
\pgfprocessround{\currentpath}{\currentpath}%
\pgf@decorate@parsesoftpath{\currentpath}{\currentpath}%
\pgfmathsetlengthmacro\firstpart{(#1)*\pgf@decorate@totalpathlength}%
\pgfmathsetlengthmacro\secondpart{(#2-(#1))*\pgf@decorate@totalpathlength}%
\pgfmathsetlengthmacro\thirdpart{(1-(#2))*\pgf@decorate@totalpathlength}%
\edef\thirdpart{{\thirdpart}{0pt}}%
\edef\firstpart{{\firstpart}{0pt}}%
\pgfmathsetlengthmacro\secondpartlength{\pgfkeysvalueof{/tikz/dash between on}
+(\pgfkeysvalueof{/tikz/dash between off})}%
\pgfmathtruncatemacro\repetitions{\secondpart/\secondpartlength}%
\pgfmathsetlengthmacro\secondexpand{\secondpart/\repetitions-\secondpartlength}%
\edef\secondexpand{\the\dimexpr\pgfkeysvalueof{/tikz/dash between off}+\secondexpand\relax}%
\edef\secondpart{%
\pgfutil@Repeat{\the\numexpr\repetitions-1\relax}%
{{\pgfkeysvalueof{/tikz/dash between on}}{\secondexpand}}%
}%
\edef\tikz@temp{\firstpart\secondpart\thirdpart}%
\expandafter\pgfsetdash\expandafter{\tikz@temp}{+0pt}%
}
}
}
\makeatother
\tikzset{
dash between style/.is choice,
dash between style/dotted/.style ={dash between on=\pgflinewidth,dash between off=2pt},
dash between style/densely dotted/.style={dash between on=\pgflinewidth,dash between off=1pt},
dash between style/loosely dotted/.style={dash between on=\pgflinewidth,dash between off=4pt},
dash between style/dashed/.style ={dash between on=3pt,dash between off=2pt},
dash between style/loosely dashed/.style={dash between on=3pt,dash between off=6pt},
dash between style/densely dashed/.style={dash between on=3pt,dash between off=2pt},
dash between style/no/.style={dash between on=0pt, dash between off=1pt},% dirty
dash between on/.initial=\pgflinewidth,
dash between off/.initial=2pt,
%
middle dotted line/.style={
thick,
dash between=.35 and .65}}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0, 3)
coordinate (B) at (5, 3)
coordinate (C) at (0, 2)
coordinate (D) at (5, 0);
\draw[middle dotted line] (A) -- (B);
\draw[middle dotted line] (C) -- (D);
\draw[
ultra thick,
dash between=1/3 and 2/3,
dash between style=dotted,
] (0,-1) .. controls +(6,-1) and +(-6,-1) .. +(3,0);
\end{tikzpicture}
\end{document}
Output
