# How to dash a specific curve segment in Tikz?

I'm trying to draw a three dimensional object inside which sits surface (blue) and a curve (green). To make it look more 3d I'd like to dash the part of the curve (green) in between the two red dots on the right side of the diagram.

Here is my example:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1.0]
%
\draw [line width=1pt] plot [smooth cycle, tension=1] coordinates {(0,0) (1,2) (3,3) (5,0) (2,-2)};
%
\filldraw[line width=1pt, top color=blue]
(0.28, 0.5) to [out=20, in=70]   (4.96,0) to [out=250, in=200](0.28,0.5);
%
\draw [green, line width=1pt] plot [smooth cycle, tension=1] coordinates {(3,2.99) (3,0) (3,-1.85) (1.5,-1) (1,0.5)};
\node [red] at (3.1,1.12) {$\bullet$};
\node [red] at (0.87,-0.41) {$\bullet$};
\node [red] at (3.05,-0.78) {$\bullet$};
\end{tikzpicture}
\end{center}
\end{document}


Does anyone know how to do this?

• Welcome to TeX.SE!, yes just add dahsed to the corresponding line:\draw [dashed,green... – SAM Jul 7 '16 at 10:57
• That would dash the whole curve, wouldn't it? I just want to dash that specific segment and since it is a curve and not piecewise linear I don't know how to do that. – Sebastian Thyssen Jul 7 '16 at 11:01
• Since I'm not very good at this, I drew all curves and the surface by adjusting their coordinates manually until they fit. Now I have added red dots to make my question more precise. I'd like to dash the segment (which I don't have explicitly) of the green curve in between the two red dots. – Sebastian Thyssen Jul 7 '16 at 11:08

You can plot the line twice. The first time you can clip the dashed segment and the second time use Paul Gaborit's invclip to clip everything except the dashed segment. Both clip should be inside an scope to prevent side effects.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz,pgfplots}

\tikzset{invclip/.style={clip,insert path={{[reset cm]
(-\maxdimen,-\maxdimen) rectangle (\maxdimen,\maxdimen)
}}}}

\begin{document}

\begin{tikzpicture}[scale=1.0]
%
\draw [line width=1pt] plot [smooth cycle, tension=1] coordinates {(0,0) (1,2) (3,3) (5,0) (2,-2)};
\filldraw[line width=1pt, top color=blue] (0.28, 0.5) to [out=20, in=70]   (4.96,0) to [out=250, in=200](0.28,0.5);
%

\begin{scope}
\clip (2.5,1.12) rectangle (3.5,-0.78);
\draw [green, dashed] plot [smooth cycle, tension=1] coordinates {(3,2.99) (3,0) (3,-1.85) (1.5,-1) (1,0.5)};
\end{scope}

\begin{scope}
\begin{pgfinterruptboundingbox}
\path[invclip] (2.5,1.12) rectangle (3.5,-0.78);
\end{pgfinterruptboundingbox}

\draw [green] plot [smooth cycle, tension=1] coordinates {(3,2.99) (3,0) (3,-1.85) (1.5,-1) (1,0.5)};
\end{scope}
\node [red] at (3.1,1.12) {$\bullet$};
\node [red] at (0.87,-0.41) {$\bullet$};
\node [red] at (3.05,-0.78) {$\bullet$};

\end{tikzpicture}
\end{document}


It can probably be done using soft clipping. Here is a draft of what could be done.

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.pathreplacing,decorations.softclip}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1.0]
%
\draw [line width=1pt] plot [smooth cycle, tension=1] coordinates {(0,0) (1,2) (3,3) (5,0) (2,-2)};
\filldraw[line width=1pt, top color=blue] (0.28, 0.5) to [out=20, in=70]   (4.96,0) to [out=250, in=200](0.28,0.5);
%
\draw [green,
postaction={decorate,draw,dotted,semithick,color=red},
decoration={soft clip,soft clip path={(2,1.12) rectangle (4,-0.78)},},
] plot [smooth cycle, tension=1] coordinates {(3,2.99) (3,0) (3,-1.85) (1.5,-1) (1,0.5)};
\node [red] at (3.1,1.12) {$\bullet$};
\node [red] at (0.87,-0.41) {$\bullet$};
\node [red] at (3.05,-0.78) {$\bullet$};
\draw[thick] (2,1.12) rectangle (4,-0.78);
\end{tikzpicture}
\end{center}
\end{document}


Refinements are in order (smooth cycling the postaction is bad, the green line is still visible, etc.), but it might give you an idea.