4

How can I fill the part of a shape between two positions with a color? Is there an easy way with 'path picture' etc.?

PS: · I know there are examples for that task, but these mostly for paths, not for shapes.
· Yes, there should be a brute force solution (by drawing an arc) in that case as well.
But I asked myself, whether there is an elegant way but setting some keys etc.
I need to fill plenty of parts and I am looking for an systematic way, not how to create the shown picture very fast (note: this is just an example).

MWE:

enter image description here

\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}[font=\tiny\sffamily, very thin]
\node[rounded rectangle, draw, inner sep=0pt,
line width=5mm, 
minimum width=40mm, minimum height=20mm, 
](s){*};

\draw[brown] (s.center) -- +(s.0);

\draw[red] (s.center) -- ++(s.north east) -- ++(0,-10mm-2.5mm); \draw[red] (s.center) -- ++(s.north east) -- ++(0,-10mm-2.5mm); \end{tikzpicture} \end{document}

cis
  • 8,073
  • 1
  • 16
  • 45

2 Answers2

7

I found a solution with clip:

enter image description here

\documentclass[margin=5mm, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}[
mystyle/.style={rounded rectangle, draw, inner sep=0pt,
line width=5mm, 
minimum width=40mm, minimum height=20mm, },
]
% Main node
\node[mystyle](s){*};
% Help Lines
\draw[brown] (s.center) -- +(s.0);
\draw[brown] (s.center) -- +(s.north east);
% Colored Part
\begin{scope}[]
\clip (s.0) rectangle (s.north east);
\node[mystyle, red](s colored part){*};
\end{scope}
\end{tikzpicture}
\end{document}
cis
  • 8,073
  • 1
  • 16
  • 45
4

I had time to think of something better, more modular and I hope it answers your question.

I used a pic that I defined as :

\begin{tikzpicture}[
    dashedrect/.pic={ 
            \draw  [red] (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle; % draw the red rect
            \draw  [dash pattern= #1] 
            (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle;
        }
    ]   

The pic says draw a rectangle according to the path defined by 4 points. the argument #1 will be used to send values for the dash pattern.

Then I can draw the pics on coordinates or nodes like this:

\coordinate (s) at (0,0);
\pic at (s) {dashedrect=on 20 off 10};
\pic at (2,2) {dashedrect= on 5 off 25 on 10};

enter image description here This is the proof of concept of modularity via pic.

I can draw anything inside the pic. Let's change the definition of the pic then:

\begin{tikzpicture}[
    font=\tiny\sffamily,
    very thin,
    dashedrect/.pic={ % <-- defined a pic  
            \draw  [thick,red] 
            (0,0) arc[radius=0.5cm, start angle=270, end angle=90]  --  (1,1) arc[radius=0.5cm, start angle=90, end angle=-90] (1,0) --  (0,0);
            \draw  [thick,dash pattern= #1] 
            (0,0) arc[radius=0.5cm, start angle=270, end angle=90]  --  (1,1) arc[radius=0.5cm, start angle=90, end angle=-90] (1,0) --  (0,0);
        }
    ]   
\coordinate (s) at (0,0);
\pic at (s) {dashedrect=on 20 off 10};
\pic at (2,2) {dashedrect= on 5 off 25 on 10};
\end{tikzpicture}

enter image description here

I can keep going on and on drawing anything inside the pic.

anis
  • 1,510
  • 5
  • 13