5

With my code, I am now trying to add a background shading in the triangle created by C_4, D and C_5. I tried with pgfonlayer, but it only creates rectangular shading. Here is my current code:

\documentclass{article}
\usepackage{tikz, color, amsmath}
\usetikzlibrary{calc, arrows,backgrounds,positioning,fit}

\newcommand\ptext[1]{\textcolor{purple}{$#1$}}

\begin{document}

\newcommand{\tikzAngleOfLine}{\tikz@AngleOfLine}
  \def\tikz@AngleOfLine(#1)(#2)#3{
  \pgfmathanglebetweenpoints{
    \pgfpointanchor{#1}{center}}{
    \pgfpointanchor{#2}{center}}
  \pgfmathsetmacro{#3}{\pgfmathresult}
  }

\begin{tikzpicture}[dot/.style 2 args={circle, color={#2},outer sep=0pt, inner sep=1.5pt, fill ,name=#1},
dot/.default={}{blue},
  ex line/.default = {1cm},
  ex line/.style={shorten >=-0.5#1, ->, color=blue},
  line/.default=1cm,
  line/.style={}]

\node [dot={C_1}{green}, label={[xshift=-0.15cm, yshift=0.05cm]\ptext{C_1}}] at (0, 0) {};
\node [dot={C_2}{green}, label={[xshift=0.1cm, yshift=0.1cm]\ptext{C_2}}] at (-2.3,1) {};
\node [dot={C_3}{green}, label={[xshift=0cm, yshift=0cm]\ptext{C_3}}] at (-4,4.3) {};
\node [dot={C_4}{green}, label={[xshift=-.10cm, yshift=0.05cm]\ptext{C_4}}] at (2.5,5) {};
\node [dot={C_5}{green}, label={[xshift=0cm, yshift=0cm]\ptext{C_5}}] at (4.9,3) {};
\node [dot={C_6}{green}, label={[xshift=-0.15cm, yshift=0cm]\ptext{C_6}}] at (4,1) {};

\draw [line] (C_1) -- (C_2) -- (C_3) -- (C_4) -- (C_5) -- (C_6) -- (C_1);

\node [dot={D}{blue},label={[xshift=0.15cm, yshift=-0.7cm]\ptext{D}}] at (.85,2.17) {};

\tikzAngleOfLine(D)(C_1){\AngleStart}

\foreach \num in {2, 3, 4, 5, 6}{    
\draw [ex line] (D) -- (C_\num);
\tikzAngleOfLine(D)(C_\num){\AngleEnd}
\ifnum\num=6\relax
\draw[red,<-] (D)+(\AngleEnd:0.75+\num*0.15cm) arc (\AngleEnd:360+\AngleStart:0.75+\num*0.15cm);   
\else
\draw[red,->] (D)+(\AngleStart:0.75+\num*0.15cm) arc (\AngleStart:\AngleEnd:0.75+\num*0.15cm);   
\fi
}
\draw [ex line, very thick] (D) -- (C_1);

\node [dot={P}{red},label={[xshift=0.0cm, yshift=0.0cm]\ptext{P}}] at (3.85,4.87) {};
\draw[line, thick] (D) -- (P);

\begin{pgfonlayer}{background}
  \node [fill=black!30,fit=(C_5) (D) (C_4)] {};
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

and it produces:

outcome:

How can I set the shading only in the triangle? Thank you.

EDIT

In a separate problem, I need to shade the whole region bounded by the extended arrows at C_4 & C_5 joined by a chord (that contains P) and D. I have changed to

\begin{pgfonlayer}{background}
 \clip  (C_5.center) -- (D.center) -- (C_4.center) -- (P) -- cycle;
 \fill[yellow!70] (D) circle (4.7);
\end{pgfonlayer}

It produces a quadrilateral:

Thanks for your help.

8

Instead of adding a node defined with fit, just use \fill:

\begin{pgfonlayer}{background}
  \fill[black!30] (C_5.center) -- (D.center) -- (C_4.center) -- (C_5.center);
\end{pgfonlayer}

enter image description here

\documentclass{article}
\usepackage{tikz, color, amsmath}
\usetikzlibrary{calc, arrows,backgrounds,positioning,fit}

\newcommand\ptext[1]{\textcolor{purple}{$#1$}}

\begin{document}

\newcommand{\tikzAngleOfLine}{\tikz@AngleOfLine}
  \def\tikz@AngleOfLine(#1)(#2)#3{
  \pgfmathanglebetweenpoints{
    \pgfpointanchor{#1}{center}}{
    \pgfpointanchor{#2}{center}}
  \pgfmathsetmacro{#3}{\pgfmathresult}
  }

\begin{tikzpicture}[dot/.style 2 args={circle, color={#2},outer sep=0pt, inner sep=1.5pt, fill ,name=#1},
dot/.default={}{blue},
  ex line/.default = {1cm},
  ex line/.style={shorten >=-0.5#1, ->, color=blue},
  line/.default=1cm,
  line/.style={}]

\node [dot={C_1}{green}, label={[xshift=-0.15cm, yshift=0.05cm]\ptext{C_1}}] at (0, 0) {};
\node [dot={C_2}{green}, label={[xshift=0.1cm, yshift=0.1cm]\ptext{C_2}}] at (-2.3,1) {};
\node [dot={C_3}{green}, label={[xshift=0cm, yshift=0cm]\ptext{C_3}}] at (-4,4.3) {};
\node [dot={C_4}{green}, label={[xshift=-.10cm, yshift=0.05cm]\ptext{C_4}}] at (2.5,5) {};
\node [dot={C_5}{green}, label={[xshift=0cm, yshift=0cm]\ptext{C_5}}] at (4.9,3) {};
\node [dot={C_6}{green}, label={[xshift=-0.15cm, yshift=0cm]\ptext{C_6}}] at (4,1) {};

\draw [line] (C_1) -- (C_2) -- (C_3) -- (C_4) -- (C_5) -- (C_6) -- (C_1);

\node [dot={D}{blue},label={[xshift=0.15cm, yshift=-0.7cm]\ptext{D}}] at (.85,2.17) {};

\tikzAngleOfLine(D)(C_1){\AngleStart}

\foreach \num in {2, 3, 4, 5, 6}{    
\draw [ex line] (D) -- (C_\num);
\tikzAngleOfLine(D)(C_\num){\AngleEnd}
\ifnum\num=6\relax
\draw[red,<-] (D)+(\AngleEnd:0.75+\num*0.15cm) arc (\AngleEnd:360+\AngleStart:0.75+\num*0.15cm);   
\else
\draw[red,->] (D)+(\AngleStart:0.75+\num*0.15cm) arc (\AngleStart:\AngleEnd:0.75+\num*0.15cm);   
\fi
}
\draw [ex line, very thick] (D) -- (C_1);

\node [dot={P}{red},label={[xshift=0.0cm, yshift=0.0cm]\ptext{P}}] at (3.85,4.87) {};
\draw[line, thick] (D) -- (P);

\begin{pgfonlayer}{background}
  \fill[black!30] (C_5.center) -- (D.center) -- (C_4.center) -- (C_5.center);
\end{pgfonlayer}

\end{tikzpicture}
\end{document}
1

After some pseudo-random experiments, I managed to do what I wanted:

\begin{pgfonlayer}{background}
        \fill[orange!30] (C_5.center) -- (D.center) -- (C_4.center);
        \clip (3.7, 4) circle (1.56204);  
        \fill[yellow]  (C_5.center)-- (C_4.center) -- (4.1, 7.0285) -- (7.1, 4.24489);
\end{pgfonlayer}

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.