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}
