Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to chain multiple bezier curves but when I try to fill them I cannot fill the complete closed curve but it seems to close each intermediate curve before filling. I've tried various things but nothing seems to work. Probably something simple... should be obivous that I do not want the following

enter image description here

I'm simply "chaining" the paths... the same result happens if I do not chain them.

(P1) .. controls (C1) and (C2) .. (P2) (P2) .. controls (C2) and (C3) .. (P3) ...
share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

Don't repeat the last coordinate of each path segment.

Taken verbatim from the pgfmanual, section 14.3 "The Curve-To Operation":

As with the line-to operation, it makes a difference whether two curves are joined because they resulted from consecutive curve-to or line-to operations, or whether they just happen to have the same ending:

\begin{tikzpicture}[line width=10pt]
\draw (0,0) -- (1,1) (1,1) .. controls (1,0) and (2,0) .. (2,0);
\draw (3,0) -- (4,1) .. controls (4,0) and (5,0) .. (5,0);
\useasboundingbox (0,1.5); % make bounding box higher
\end{tikzpicture}

This is important with filling because when PGF encounters a piecewise curve like the original and is asked to fill it then it fills each component separately, and when it fills an open curve then it first closes it with a straight line from finish to start. Hence the result seen in the question.

share|improve this answer
add comment

Only to complete Jake's excellent answer. First the problems appear not only with bezier's curve but like Jake remarks with the line-to or the to operation edge is a special case.

First problem is how the lines are joined, second problem is what area is filled.

The problem is not exactly linked to discontinuous line (see the last example with line to. We have two paths and two continuous lines. With tikz when you don't chain draw operations but when you give a new point, you make a "moveto" operation and you begin a new path, If you have given a fill option inside the draw command, then when the code arrives at the end ;, all the closed paths are filled.

I use the 'to' operation in the first examples to show the different problem with fill and joint.

I give two examples with edge because in this case, you get multiple paths and you can't fill. (see the pgfmanual about edge operation for details).

\documentclass[11pt]{scrartcl}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{%
  arrows,
  calc
} 

\begin{document}

\tikz  \fill[yellow,line width=1mm,draw=black,line join=round] 
                   (0:0)  to [bend left] (0:3) 
                          to [bend left] (-60:3)
                          to [bend left] (0:0) --cycle ;  
\tikz  \fill[yellow,line width=1mm,draw=black,line join=round] 
                  (0:0)  to [bend left] (0:3) 
                         to [bend left] (-60:3)
                         to [bend left] (0:0)  ; 

\tikz  \fill[yellow,line width=1mm,draw=black] 
                   (0:0)   to [bend left] (0:3) 
                   (0:3)   to [bend left] (-60:3)
                   (-60:3) to [bend left] (0:0)  ;
 \tikz  \fill[yellow,line width=1mm,draw=black,line join=round] 
                   (0:0)   to [bend left] (0:3) 
                   (0:3)   edge [bend left] (-60:3)
                   (-60:3) edge [bend left] (0:0)  ;  

\tikz  \fill[yellow,line width=1mm,draw=black,line join=round] 
                   (0:0)  to [bend left] (0:3) 
                          edge [bend left] (-60:3)
                          edge [bend left] (0:0) -- cycle ; 
 \tikz  \fill[yellow,line width=1mm,draw=black,line join=round] 
                   (0:0)    -- (0:3) 
                   (0:3)    -- (-60:3)
                   (-60:3)  -- (0:0) -- cycle ; 


 \tikz  \fill[yellow,line width=1mm,draw=black,line join=round] 
                   (0:0)    -- (0:3) 
                            -- (-60:3)
                            -- (0:0) 
                            -- cycle 
                   [shift={(4,0)} ]
                   (0:0)    -- (0:3) 
                            -- (-60:3)
                            -- (0:0) 
                            -- cycle 
                   ; 
  \end{document} 

enter image description here

The last examples show how to fill (here shade but it's the same problem) multiple paths. Sometimes it's interesting to get what you want to avoid. It's a work about insert path but it's easy to avoid this option. You can use

 \draw[\st]  \foreach \a in {0,10,...,350} {(\a:2) to [bend left](1,0) to [bend left] (\a:2) }; 

instead of

 \draw[\st]  \foreach \a in {0,10,...,350} {(\a:2) coordinate (a) [curve 1] }; 

In these last examples, I show the difference with different paths. I use first two to operations and then to and edge but I give multiple paths with first point (\a:2).

\documentclass[11pt]{scrartcl}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{%
  arrows,
  calc
} 

\begin{document}

\tikzset{curve 1/.style={insert path={to [bend left](1,0) to [bend left] (\tikzlastnode)}}}
\tikzset{curve 2/.style={insert path={to [bend left](1,0) edge [bend left] (\tikzlastnode)}}}     
\tikzset{main/.style={top color=red!50,bottom color=orange}} 
\tikzset{top/.style={black,even odd rule,bottom color=MidnightBlue,top color=green}} 

\begin{tikzpicture} [rotate=-90,scale=1.5] 
\foreach \st in {main,top}
  \draw[\st]  \foreach \a in {0,10,...,350} {(\a:2) coordinate (a) [curve 1] };   
\end{tikzpicture} 
\begin{tikzpicture} [rotate=-90,scale=1.5] 
\foreach \st in {main,top}
  \draw[\st]  \foreach \a in {0,10,...,350} {(\a:2) coordinate (a) [curve 2]  };   
\end{tikzpicture}

\end{document} 

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.