2

I have a polygon with many vertices. I first created the coordinates by using

\foreach \i in {1,...,\n}
{
    \coordinate (\i) at (\x,\y);
    \coordinate (p\i) at (\z,\t);
}

There are a series of computations for \x, \y, \z, and \t which are not relevant, so I omit those lines.

I need to fill the polygon defined by the coordinates. Do I have to write

(1)--(p1)--(p2)--(2)--(3)--(p3)--(p4)--

and so on, or is there a way that I can do this in a loop?

I can write the loop as follows

\foreach [evaluate ={\j=int(mod(\i,2)); \k=\i+1;}] \i in {1,...,\n}
{
    \ifthenelse{\j = 1}
    {
        \draw (\i)--(p\i)--(p\k)--(\k);
    }
    {
        \draw (\i)--(\k);
    }
}

which only draws the polygon. Can I somehow name the polygon and then fill it?

Here is a MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\coordinate (1) at (0,0);
\coordinate (2) at (0,10);
\coordinate (3) at (10,10);
\coordinate (4) at (10,0);

\fill[gray] (1)--(2)--(3)--(4); % I want to do this line in a \foreach loop

\end{tikzpicture} \end{figure} \end{document}

padawan
  • 1,532

2 Answers2

2

You could fill all single triangles:

enter image description here

€dit: I added \pgfmathsetmacro\StartCornerForFilling{2}

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\pgfmathsetmacro\Corners{5} % Number of corners
\pgfmathsetmacro\StartCornerForFilling{2} 
% Coordinates
\coordinate[label=left:1] (1) at (0,0);
\coordinate[label=2] (2) at (0,10);
\coordinate[label=3] (3) at (10,10);
\coordinate[label=right:4] (4) at (10,0);
\coordinate[label=below:5] (5) at (5, -5);
\coordinate[label=below:6] (6) at (4, -6); % test
\coordinate[label=below:7] (7) at (1, -4); % test
\coordinate[label=below:8] (8) at (0.5, -3); % test

\pgfmathtruncatemacro\NoTriangles{\Corners-2} \foreach[count=\No from 0] \n in {1,...,\NoTriangles} { \pgfmathsetmacro\P{\StartCornerForFilling} \pgfmathtruncatemacro\Q{1+mod(\P+\n-1,\Corners))} \pgfmathtruncatemacro\R{1+mod(\P+\n,\Corners))} \pgfmathsetmacro\colorpercent{100/\n}

\fill[yellow!\colorpercent!red, opacity=0.4] (\P) -- (\Q) -- (\R) --cycle; \draw[] (\P) -- (\Q) -- (\R) --cycle; % Annotations 1/2 \node[right of=3, yshift=-0.7*\No cm, anchor=north west]{ $\Delta_{\n}:$ {\P} -- {\Q} -- {\R}}; }

% Annotations 2/2 \node[above of=2, font=\bfseries, anchor=west]{ convex \Corners-polygon $\Rightarrow$ {\NoTriangles} triangles }; \end{tikzpicture} \end{document}

cis
  • 8,073
  • 1
  • 16
  • 45
1

Try \fill[gray] (1) foreach \n in {2,...,5} {--(\n)} --cycle;

If I was going to try to translate your example, I would probably step by 2 and combine the two cases of your conditional, something like:

\fill[gray] (1) foreach [evaluate ={\j=\i+1; \k=\i+2;}] \i in {1,3,...,\n}
{
  --(p\i)--(p\j)--(\j) -- (\k)
} -- cycle;

I am not sure whether this exactly would work but something similar will.

Full code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\coordinate (1) at (0,0);
\coordinate (2) at (0,10);
\coordinate (3) at (10,10);
\coordinate (4) at (10,0);
\coordinate (5) at (5, -5);

\fill[gray] (1) foreach \n in {2,...,5} {--(\n)} --cycle;
\end{tikzpicture} \end{figure} \end{document}

Hood Chatham
  • 5,467