绘制以虚线开始、实线结束的路径

绘制以虚线开始、实线结束的路径

我想要绘制一条线或路径,其起点为虚线,终点为实线,但只能在一条路径上(我的意思是不绘制两条路径),例如:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (3,0) -- (1,0)[dashed] -- (0,0)[solid] ;
\end{tikzpicture}
\end{document}

答案1

如果这些是直线段,那就再简单不过了。曲线会变得有趣。附件是几个例子,我同意——这太复杂了。剪辑是判断实体部分应该在哪里停止的最准确方法,但它需要自己的范围。淡入淡出有利于平滑过渡,但如果路径不是或多或少地朝一个方向运行,可能只会让人头疼。 在此处输入图片描述

我很确定有一种方法可以对路径进行程序化描边,但到目前为止我还没有找到有关它的任何信息。我​​个人喜欢一种简单的样式,它允许轻松地从路径的起点到终点改变线型,执行类似于第二个自定义虚线图案的操作,但无需输入所有内容。但就目前而言,这就是我所做的:

\documentclass[12pt,a4paper]{minimal}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}
\begin{tikzpicture}[line width=1.5pt]


\node at (200pt,40pt) [label=right:{2 line segments}]{};
% for straight segments this is the easiest:
%
\draw[dashed] (200pt,40pt) -- (60pt,40pt);
\draw (60pt,40pt) -- (0pt,40pt);


\node at (200pt,10pt) [label=right:{custom dash pattern}]{};
% for curves you can specify custom dash pattern, but it's cumbersome 
% having to specify every singly dash untill the line becomes solid:
%
\draw[dash pattern=on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 100pt] (200pt,10pt) .. controls (40pt,40pt)  and (90pt,-30pt) .. (0,20pt);


\node at (200pt,-10pt) [label=right:{custom dash pattern}]{};
% it does however allow for nice density modifications (but also by hand):
%
\draw[dash pattern=on 10pt off 9pt on 9pt off 8pt on 8pt off 7.3pt on 7.3pt off 6.7pt on 6.7pt off 6.3pt on 6.3pt off 5.9pt on 5.9pt off 5.4pt on 5.4pt off 5.0pt on 5.0pt off 4.0pt on 4.5pt off 3.0pt on 4.0pt off 2.4pt on 3.5pt off 1.8pt on 100pt] (200pt,-10pt) .. controls (40pt,20pt)  and (90pt,-50pt) .. (0,0pt);


\node at (200pt,-30pt) [label=right:{2 curves: dashed $+$ clipped solid}]{};
% now there's 2 paths - a long dashed one and a clipped solid one:
%
\begin{scope}
\clip (-5pt,-70pt) rectangle (60pt,5pt);
\draw (200pt,-30pt) .. controls (40pt,0pt)  and (90pt,-70pt) .. (0,-20pt);
\end{scope}
\draw[dashed] (200pt,-30pt) .. controls (40pt,0pt)  and (90pt,-70pt) .. (0,-20pt);


\node at (200pt,-50pt) [label=right:{2 curves: dashed $+$ faded solid}]{};
% for a gradient toning down of the solid line you can use fading:
%
\draw[path fading=east] (200pt,-50pt) .. controls (40pt,-20pt) and (90pt,-90pt) .. (0,-40pt);
\draw[dashed,path fading=west] (200pt,-50pt) .. controls (40pt,-20pt) and (90pt,-90pt) .. (0,-40pt);


\end{tikzpicture}
\end{document}

答案2

另一种方法(部分自动化,让您可以控制破折号的数量):

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{figure}[h!]
\begin{tikzpicture}[every path/.style={>=latex}]

% Normal dashed line
\draw[thick,black] (0,1) -- (1,1);
\draw[->,thick,black,dashed] (1,1) -- (4,1) node[anchor=west] {Normal dashed line};

% Specify dashed line starting coordinate and length
\draw[thick,black] (0,0) -- (1,0);
\def\x{1}; \def\y{0}; \def\length{3} \def\N{4}; 
% Draw dashed line using normal lines
\pgfmathsetmacro{\step}{(0.5+1/(4*\N))*\length/\N}; \pgfmathparse{\N-1};
\foreach \i in {0,...,\pgfmathresult} {\draw[thick,black] (\x+2*\i*\step,\y) -- (\x+2*\i*\step+\step,\y);}; 
\draw[->,thick,black] (\x+\length,\y) -- (\x+\length+0.01,\y) node[anchor=west] {Custom dashed line};

\end{tikzpicture}
\end{figure}
\end{document}

这简单地模仿了普通的虚线,通过绘制多条线来连接。参数包括\x起始 x 坐标、\y线的 y 级别、\length线的坐标长度和\N条纹数量。

箭

相关内容