是否有 TikZ 虚线装饰器或其他方法可以使路径的一部分变为虚线?

是否有 TikZ 虚线装饰器或其他方法可以使路径的一部分变为虚线?

问题

我想在 TikZ 中绘制一条抛物线路径,该路径会(突然)从连续线变为虚线。改变路径外观的明显方法是使用装饰库,但是不存在虚线装饰器。有没有一种简单的方法可以做到这一点,而不需要绘制多个串联的抛物线?

沃纳的建议(现已转化为答案)非常有效!但是有没有更简单的方法呢?

答案1

根据 Werner 的建议,以下是建议方法的实现(绘制每个抛物线,使用路径裁剪)和 MWE。我删除了原始图表中的冗余部分。

两条相交的抛物线,通过路径裁剪实现线型变化

\documentclass{minimal}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=3.14159]
\begin{scope}
\clip (0.8,0) rectangle (1.5,-0.5);
\draw[<->,color=red,dashed] (0,0) parabola[parabola height=-0.5cm] (1,0);
\end{scope}

\begin{scope}
\clip (-0.1,0) rectangle (0.8,-0.5);
\draw[<->,color=red] (0,0) parabola[parabola height=-0.5cm] (1,0);
\end{scope}

\begin{scope}
\clip (-0.1,0.2) rectangle (0.8,-0.5);
\draw[<->,dashed] (0.5,.125) parabola[parabola height=-0.5cm] (1.5,0.125);
\end{scope}

\begin{scope}
\clip (0.8,0.2) rectangle (1.6,-0.5);
\draw[<->] (0.5,.125) parabola[parabola height=-0.5cm] (1.5,0.125);
\end{scope}
\fill (0.81,-0.305) circle (0.5pt);
\node (oh) at (0.81,-0.205) {a};
\fill (0.5,-0.5) circle (0.5pt);
\node (c) at (0.5,-0.4) {c};
\fill (1,-0.375) circle (0.5pt);
\node (e) at (1,-0.275) {e};
\end{tikzpicture}

\end{document}

答案2

以下是使用以下工具制作的类似图形:pstricks. 代码对于熟悉的人来说应该是不言自明的pstricks,甚至tikz/pgf

在此处输入图片描述

\documentclass{article}
\usepackage{pstricks}% http://www.tug.org/PSTricks/main.cgi
\usepackage{pstricks-add}% http://ctan.org/pkg/pstricks-add
\begin{document}

\begin{pspicture*}(-6.2,-6.2)(6.2,6.2)
  \psset{unit=5mm,plotpoints=200,algebraic=true,arrows=<->,linewidth=1pt}%
  \psclip{\psframe[linestyle=none,linewidth=0pt](-5.5,-1)(3.2045,10)}%
    \psplot[linecolor=red,linestyle=solid]{-5.5}{5.5}{0.2*x^2}
  \endpsclip

  \psclip{\psframe[linestyle=none,linewidth=0pt](3.2045,-1)(5.5,10)}%
    \psplot[linecolor=red,linestyle=dashed]{-5.5}{5.5}{0.2*x^2}
  \endpsclip

  \psclip{\psframe[linestyle=none,linewidth=0pt](3.2045,-1)(15,10)}%
    \rput(5.5,1){%
        \psplot[linecolor=black,linestyle=solid]{-5.5}{5.5}{0.2*x^2}}
  \endpsclip

  \psclip{\psframe[linestyle=none,linewidth=0pt](-5.5,-1)(3.2045,10)}%
    \rput(5.5,1){%
        \psplot[linecolor=black,linestyle=dashed]{-5.5}{5.5}{0.2*x^2}}
  \endpsclip

  \psdot(0,0) \uput{10pt}[u]{0}(0,0){c}%
  \psdot(5.5,1) \uput{10pt}[u]{0}(5.5,1){a}%
  \psdot(3.2045,2.0538) \uput{10pt}[u]{0}(3.2045,2.0538){e}%
\end{pspicture*}


\end{document}

答案3

您可以按照您的建议使用装饰库。实际上,有一种装饰可以做成虚线的样子,即border来自pathreplacing家族的装饰。一个非常简单的例子如下:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}   
  \begin{tikzpicture}[decoration={border, segment length=4pt,amplitude=2pt, angle=0}]
    \draw [help lines] grid (3,2);
    \draw [decorate] (0,0) -- (3,2);
  \end{tikzpicture}
\end{document}

从您对修改装饰的熟悉程度来看,我推测您能够将其应用到您的问题中。

答案4

解决方案是decoration。问题是计算每种情况下的长度,其他问题取决于length缩放系数。有趣的是创建一个宏来计算两个节点之间的路径长度。

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{decorations} 
\usetikzlibrary{intersections} 
\begin{document}

\begin{tikzpicture}[scale=3.14159] 

  \tikzset{
  solid part/.style={%
  postaction={solid, decorate, draw,
  decoration={moveto,
             pre=curveto, 
             post=curveto, 
             pre length=#1,
             post length=0}}
    }
}   
\draw[name path=curve 1,<->,color=red,dashed,solid part=3.5cm] (0,0) parabola[parabola height=-0.5cm] (1,0);

\draw[name path=curve 2,<->,dashed,solid part=1.7cm] (0.5,.125) parabola[parabola height=-0.5cm] (1.5,0.125);

\fill [name intersections={of=curve 1 and curve 2, by={a}}]
        (a) circle (.5pt); 
        \fill (0.81,-0.305) circle (0.5pt);
\node[above]  at (a) {a};
\fill (0.5,-0.5) circle (0.5pt);
\node (c) at (0.5,-0.4) {c};
\fill (1,-0.375) circle (0.5pt);
\node (e) at (1,-0.275) {e};
\end{tikzpicture}

\end{document}  

在此处输入图片描述

相关内容