5

How do I fill a shape in tikz which is drawn by a combination of different commands? I mean something like this:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\mk}{node [fill=black] {}}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\foreach \x in {0,60,...,359}
    {\draw (O) -- (\x:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1) -- +(\x+30:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1) -- +(\x-30:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x+30:1) -- +(\x:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x-30:1) -- +(\x:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x+30:1)++(\x:1) -- +(\x-30:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x-30:1)++(\x:1) -- +(\x+30:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x+30:1) -- +(\x+60:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x-30:1) -- +(\x-60:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x+30:1)++(\x+60:1) --
     ++(\x-30:1) -- ++(\x-60:1) -- ++(\x-120:1) -- +(\x-150:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x+30:1)++(\x:1)++(\x-30:1)
    -- ++(\x+30:1) -- ++(\x+90:1) -- ++(\x+150:1);}
\foreach \x in {0,60,...,359}
    {\draw (\x:1)++(\x+30:1)++(\x:1)++(\x-30:1)
    -- ++(\x-30:1) -- ++(\x-90:1) -- ++(\x-150:1);}
\draw (0,0) circle (5);
\end{tikzpicture}
\end{document}

The code above produces the following output:

(Output Image)

Now I want to fill different parts of it with any some colours, so how can I do it?

  • You decide what area you want to fill. You identify what the outlining lines are. You make a closed filled area.... I know that this is not what you were hoping for, but I do not think you can find a better(simpler) solution. – hpekristiansen Jan 7 '14 at 16:02
  • 1
    See subsection 15.4: Filling a path in the Tikz&PGF manual. If you don't clarify what you want to do exactly, I'm afraid this question is likely to get closed. – jub0bs Jan 7 '14 at 17:20
  • @Jubobs I have read the manual. The option [fill=color] can be applied to a single command; But as you can see here, thr reigons are defined by different commands so I cannot use the option. – Kartik Jan 8 '14 at 3:44
  • @Hans-PeterE.Kristiansen I did not understand what you are trying to say. I want to fill the inner flower-like shape. – Kartik Jan 8 '14 at 3:45
  • The inner flower-like shape is surrounded by lines!! Look at the code, and figure out exactly what lines. Now draw these lines a second time - as a closed filled path(a single command). – hpekristiansen Jan 8 '14 at 4:49
6

As Hans-Peter E. Kristiansen suggests in his comment, when you know exactly what you want to fill you can add the code performing the filling without touching the rest of your code. In this way, some lines would be drawn twice and your code would become longer.

On the other hand, I would suggest rewriting your code and taking advantage of macros to make it much shorter. As I'm not sure what you want to fill, I've filled all possible choices. Notice the \hex building block that produces the hexagonal petals of your flowers. Also notice that if you want to fill an area (e.g., what I call the flower's outline or the hexagonal block itself) you must not break it into smaller connecting shapes: use \draw ... \foreach ... if you have to.

Drawing with tikz-pgf is just like programming IMHO. You don't want just some code that does the job. You want short, readable, maintainable, and efficient code that does the job.

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
% the building block
\newcommand\hex[1]{%
  -- ++(#1:1) -- ++(#1+30:1) -- ++(#1+60:1)
  -- ++(#1+180:1) -- ++(#1+210:1) -- cycle
}
% the outer circle
\draw[fill=orange!25!white] (0,0) circle (5);
% the flower's outline
\draw[fill=lightgray] (0:1)++(-30:1)++(-60:1)
  \foreach \x in {0,60,...,359}
    { -- ++(\x+30:1) -- ++(\x+60:1) -- ++(\x+120:1) -- ++(\x+150:1) };
% the inner flower
\foreach \x in {0,60,...,359} \draw[fill=yellow] (0,0) \hex{\x};
% the petals of the outer flower
\foreach \x in {0,60,...,359} \draw[fill=green] (\x:1) \hex{\x-30};
% the outer lines
\foreach \x in {0,60,...,359} {
  \draw (\x:1)++(\x+30:1)++(\x:1)++(\x-30:1) -- ++(\x+30:1) -- ++(\x+90:1) -- ++(\x+150:1);
  \draw (\x:1)++(\x+30:1)++(\x:1)++(\x-30:1) -- ++(\x-30:1) -- ++(\x-90:1) -- ++(\x-150:1);
}
\end{tikzpicture}
\end{document}

result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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