0

I understand how to clip with a rectangle, for instance. But how to do it with a different path. I wrote many lines with Bezier curves ( .. controls (,) and (,) ..). So now I have a path with all these Bezier curves. So, how do I clip inside this path? Here is a MWE:

\begin{tikzpicture}[line width=0.8pt]
  \draw (0.1,4.95) .. controls (0.6,4.5) and (1.8,4.15) .. (1.8,3.65);
  \draw (1.8,3.65) .. controls (1.8,2.75) and (1.9,3.15) .. (1.9,2.55);
  \draw (1.9,2.55) .. controls (1.9,2.35) and (2.25,2.15) .. (2.25,1.65);
  \draw (2.25,1.65) .. controls (2.25,1.4) and (1.65,0.8) .. (1.65,0.75);
  \draw (1.65,0.75) .. controls (1.65,0.6) and (1.8,0.54) .. (1.9,0.5);
  \draw (1.9,0.5) .. controls (2,0.44) and (2.5,0.6) .. (2.7,1.05);
  \draw (2.7,1.05) .. controls (3,1.45) and (3,1.5) .. (3,1.8);
  \draw (3,1.8) .. controls (3.05,2.1) and (3.1,2.1) .. (3.4,2);
  \draw (3.4,2) .. controls (3.6,2) and (3.6,1.95) .. (3.8,1.95);
  \draw (3.8,1.95) .. controls (3.85,1.95) and (4.1,1.85) .. (4.2,1.85);
  \draw (4.2,1.85) .. controls (4.4,1.85) and (4.5,1.95) .. (5,2);
  \draw (5,2) .. controls (5.4,1.95) and (5.5,1.95) .. (5.8,1.96);
  \draw (5.8,1.96) .. controls (6.55,1.92) and (6.7,1.94) .. (6.7,2.2);
  \draw (6.7,2.2) .. controls (6.7,2.45) and (6.1,2.5) .. (5.9,2.43);
  \draw (5.9,2.43) .. controls (5.8,2.47) and (5.2,2.55) .. (5.1,2.5);
  \draw (5.1,2.5) .. controls (5.0,2.55) and (5,2.57) .. (4.9,2.6);
  \draw (4.9,2.6) .. controls (4.95,3.0) and (4.95,3.0) .. (4.9,3.6);
  \draw (4.9,3.6) .. controls (4.93,4.0) and (4.9,4.0) .. (4.85,4.5);
  \draw (4.85,4.5) .. controls (4.83,4.8) and (4.8,4.6) .. (4.78,5);
  \draw (4.78,5) .. controls (4.76,5.2) and (4.8,5.0) .. (4.75,5.35);
  \draw (4.75,5.35) .. controls (4.35,5.57) and (4.23,5.0) .. (4.33,4.5);
  \draw (4.33,4.5) .. controls (4.3,4.38) and (4.31,4.4) .. (4.3,4.18);
  \draw (4.3,4.18) -- (4.25,4.16);
  \draw (4.25,4.16) .. controls (4,4.5) and (3.4,4.7) .. (3,4.75);
  \draw (3,4.75) .. controls (2.8,4.8) and (2.65,5.03) .. (2.6,5.05);
  \draw (2.6,5.05) .. controls (2.4,5.15) and (1.75,5.8) .. (1.3,6.25);
\end{tikzpicture}

I can't close the path because I don't want a line between the first and the last point (or an invisible line)

2
  • You should maybe provide a minimal working example (MWE), but you can always write \path[clip] ... ; and everything that follows (inside the same scope) will be clipped according to the path. Commented May 24, 2023 at 15:34
  • As @JasperHabicht said, please provide your MWE. You can only clip against a path. You will need to put all the different curves on one path in the right order. If it's a very complex construct, we might be able to use the spath3 function or another trick. Commented May 24, 2023 at 16:07

2 Answers 2

1

I think you need to close the path:

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

\begin{document}
\begin{tikzpicture}
\draw[clip] (0,0)..controls (1,1).. (4,0)..controls (3,-1)..cycle;
\fill[red] (-1,-1) rectangle (2,1);
\fill[green] (2,1) rectangle (4,-1);
\end{tikzpicture}
\end{document}

enter image description here

Update:

The proposed by OP shows several bezier lines that finishes where the new line starts. This way it's easier to build them as one path. This path can be closed to delimit the clip area and be drawn a second time without the closing line.

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

\newcommand{\myline}{%
(0.1,4.95) .. controls (0.6,4.5) and (1.8,4.15) .. (1.8,3.65)%;
%  \draw (1.8,3.65) 
  .. controls (1.8,2.75) and (1.9,3.15) .. (1.9,2.55)%;
%  \draw (1.9,2.55) 
  .. controls (1.9,2.35) and (2.25,2.15) .. (2.25,1.65)%;
%  \draw (2.25,1.65) 
  .. controls (2.25,1.4) and (1.65,0.8) .. (1.65,0.75)%;
%  \draw (1.65,0.75) 
  .. controls (1.65,0.6) and (1.8,0.54) .. (1.9,0.5)%;
%  \draw (1.9,0.5) 
  .. controls (2,0.44) and (2.5,0.6) .. (2.7,1.05)%;
%  \draw (2.7,1.05) 
  .. controls (3,1.45) and (3,1.5) .. (3,1.8)%;
%  \draw (3,1.8) 
  .. controls (3.05,2.1) and (3.1,2.1) .. (3.4,2)%;
%  \draw (3.4,2) 
  .. controls (3.6,2) and (3.6,1.95) .. (3.8,1.95)%;
%  \draw (3.8,1.95) 
  .. controls (3.85,1.95) and (4.1,1.85) .. (4.2,1.85)%;
%  \draw (4.2,1.85) 
  .. controls (4.4,1.85) and (4.5,1.95) .. (5,2)%;
%  \draw (5,2) 
  .. controls (5.4,1.95) and (5.5,1.95) .. (5.8,1.96)%;
%  \draw (5.8,1.96) 
  .. controls (6.55,1.92) and (6.7,1.94) .. (6.7,2.2)%;
%  \draw (6.7,2.2) 
  .. controls (6.7,2.45) and (6.1,2.5) .. (5.9,2.43)%;
%  \draw (5.9,2.43) 
  .. controls (5.8,2.47) and (5.2,2.55) .. (5.1,2.5)%;
%  \draw (5.1,2.5) 
  .. controls (5.0,2.55) and (5,2.57) .. (4.9,2.6)%;
%  \draw (4.9,2.6) 
  .. controls (4.95,3.0) and (4.95,3.0) .. (4.9,3.6)%;
%  \draw (4.9,3.6) 
  .. controls (4.93,4.0) and (4.9,4.0) .. (4.85,4.5)%;
%  \draw (4.85,4.5) 
  .. controls (4.83,4.8) and (4.8,4.6) .. (4.78,5)%;
%  \draw (4.78,5) 
  .. controls (4.76,5.2) and (4.8,5.0) .. (4.75,5.35)%;
%  \draw (4.75,5.35) 
  .. controls (4.35,5.57) and (4.23,5.0) .. (4.33,4.5)%;
%  \draw (4.33,4.5) 
  .. controls (4.3,4.38) and (4.31,4.4) .. (4.3,4.18)%;
%  \draw (4.3,4.18) 
  -- (4.25,4.16)%;
%  \draw (4.25,4.16) 
  .. controls (4,4.5) and (3.4,4.7) .. (3,4.75)%;
%  \draw (3,4.75) 
  .. controls (2.8,4.8) and (2.65,5.03) .. (2.6,5.05)%;
%  \draw (2.6,5.05) 
  .. controls (2.4,5.15) and (1.75,5.8) .. (1.3,6.25)}
\begin{document}

\begin{tikzpicture}[line width=0.8pt]
  \path[clip] \myline--cycle;
  \fill[green!90!black] (0,0) rectangle (7,7);
  \draw \myline;
\end{tikzpicture}

\end{document}

enter image description here

4
  • Thank you. But my path has many lines, not just one. So there are several \draw commands. Maybe is there a way to build a path with several \draw commands? I can’t put a MWE because I am not at home
    – Didier
    Commented May 24, 2023 at 16:02
  • If your path has many lines, you need to close it anyway. You will have to find a way to connect those lines. You should add a minimal working example (MWE) to your question, in order to let us help you accordingly.
    – SebGlav
    Commented May 24, 2023 at 16:50
  • @Didier, please taka a look at updated answer. It's tricky but i think it works.
    – Ignasi
    Commented May 24, 2023 at 18:28
  • Thank you for your answer. Best regards
    – Didier
    Commented May 24, 2023 at 19:20
0

You can clip a slightly different path (notice the -| cycle at the end), draw something against that clip and then, outside of the clip, draw the unclosed path.

(As an alternative, a similar thing can be done with a path picture which is basically a one-path clip where the clipping path can be drawn, though. This doesn't help you much since you clipping path and drawn path differ slightly.)

Code

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
  line width=0.8pt,
  hand/.style={insert path={
    (0.1,4.95) .. controls (0.6,4.5) and (1.8,4.15) .. (1.8,3.65)
               .. controls (1.8,2.75) and (1.9,3.15) .. (1.9,2.55)
               .. controls (1.9,2.35) and (2.25,2.15) .. (2.25,1.65)
               .. controls (2.25,1.4) and (1.65,0.8) .. (1.65,0.75)
               .. controls (1.65,0.6) and (1.8,0.54) .. (1.9,0.5)
               .. controls (2,0.44) and (2.5,0.6) .. (2.7,1.05)
               .. controls (3,1.45) and (3,1.5) .. (3,1.8)
               .. controls (3.05,2.1) and (3.1,2.1) .. (3.4,2)
               .. controls (3.6,2) and (3.6,1.95) .. (3.8,1.95)
               .. controls (3.85,1.95) and (4.1,1.85) .. (4.2,1.85)
               .. controls (4.4,1.85) and (4.5,1.95) .. (5,2)
               .. controls (5.4,1.95) and (5.5,1.95) .. (5.8,1.96)
               .. controls (6.55,1.92) and (6.7,1.94) .. (6.7,2.2)
               .. controls (6.7,2.45) and (6.1,2.5) .. (5.9,2.43)
               .. controls (5.8,2.47) and (5.2,2.55) .. (5.1,2.5)
               .. controls (5.0,2.55) and (5,2.57) .. (4.9,2.6)
               .. controls (4.95,3.0) and (4.95,3.0) .. (4.9,3.6)
               .. controls (4.93,4.0) and (4.9,4.0) .. (4.85,4.5)
               .. controls (4.83,4.8) and (4.8,4.6) .. (4.78,5)
               .. controls (4.76,5.2) and (4.8,5.0) .. (4.75,5.35)
               .. controls (4.35,5.57) and (4.23,5.0) .. (4.33,4.5)
               .. controls (4.3,4.38) and (4.31,4.4) .. (4.3,4.18)
               -- (4.25,4.16)
               .. controls (4,4.5) and (3.4,4.7) .. (3,4.75)
               .. controls (2.8,4.8) and (2.65,5.03) .. (2.6,5.05)
               .. controls (2.4,5.15) and (1.75,5.8) .. (1.3,6.25)}}]
\begin{scope}
  \clip[hand] -| cycle;
  \shade[left color=red, right color=green]
              (current bounding box.south west)
    rectangle (current bounding box.north east);
\end{scope}
\draw[hand];

\tikzset{xshift=7cm}
\path[hand] -| cycle [path picture={
  \shade[left color=red, right color=green]
              (path picture bounding box.south west)
    rectangle (path picture bounding box.north east);
}];
\draw[hand];
\end{tikzpicture}
\end{document}

Output

enter image description here

1
  • Thank you for your answer. Best regards
    – Didier
    Commented May 24, 2023 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .