[Tex/LaTex] Drawing cycles using TikZ

tikz-pgf

I would like to draw cycles (or polygonal paths) using TikZ. I provide code in which I try to do this using rectangular coordinates and polar coordinates. In the first drawing, I think the cycle is obscured by the help lines. The second drawing is not drawn the way I had intended.

In both drawings, I have the paths on a grid. I wanted to make the grid lines a quarter of the thickness of the line segments in the cycles and a light gray. I think the default thickness of lines drawn by TikZ is 0.4pt; so, I thought the specification line width=0.1pt would make the help lines a quarter as thick as the cycles. It did not. I forgot how to make the lines a light gray. (I thought help lines were gray by default.)

\documentclass{amsart}
\usepackage{tikz}

\usetikzlibrary{intersections,decorations.pathreplacing,positioning}

\begin{document}

\begin{tikzpicture}
\draw (0,0) grid [xstep=0.5, ystep=0.5, line width=0.1pt,gray] (6,6);
\draw[line width=0.5pt] (4,1) -- ++(1,0) -- ++(0,3) -- ++(-2,0) -- cycle;
\end{tikzpicture}
\vskip0.25in

\begin{tikzpicture}
\draw (0,0) grid [xstep=0.5, ystep=0.5, line width=0.1pt,gray] (6,6);
\draw[line width=0.5pt] (1,1.5) -- ++(2:135) -- ++(1:135) -- ++(3:90) -- ++ (1:120) -- cycle;
\end{tikzpicture}
\end{document}

Best Answer

The options for drawing need to be passed to the \draw command rather than grid. (EDIT: as Qrrbrbirlbel said first.)

Polar coordinates are specified as (angle:distance). But note that an angle of 135, say, is always in the same direction so the first two segments of your path are going in the same direction:

(1,1.5) -- ++(135:2) -- ++(135:1) 

and

(1,1.5) -- ++(135:3)

are equivalent.

So partially corrected code looks like this:

\documentclass{amsart}
\usepackage{tikz}

\begin{document}

  \begin{tikzpicture}
    \draw [gray, line width=0.1pt] (0,0) grid [xstep=0.5, ystep=0.5] (6,6);
    \draw [line width=0.5pt] (4,1) -- ++(1,0) -- ++(0,3) -- ++(-2,0) -- cycle;
  \end{tikzpicture}
  \vskip0.25in

  \begin{tikzpicture}
    \draw [gray, line width=0.1pt] (0,0) grid [xstep=0.5, ystep=0.5] (6,6);
    \draw [line width=0.5pt] (1,1.5) -- ++(135:2) -- ++(135:1) -- ++(90:3) -- ++ (120:1) -- cycle;
  \end{tikzpicture}
\end{document}

but this is probably not what you want:

not really polygons

Close up of lines:

close up

EDIT

The shapes.geometric library lets you draw things like regular polygons easily:

\documentclass[tikz,border=5pt]{standalone}

\usetikzlibrary{shapes.geometric}

\begin{document}

  \begin{tikzpicture}
    \draw [gray, line width=.1pt] (0,0) grid [xstep=0.5, ystep=0.5] (6,6);
    \node [shape=regular polygon, regular polygon sides=5, draw, minimum width=15mm] at (3,3) {};
  \end{tikzpicture}
\end{document}

regular polygon node

However, if that doesn't suit, you could try this:

  \begin{tikzpicture}
    \draw [gray, line width=.1pt] (0,0) grid [xstep=0.5, ystep=0.5] (6,6);
    \begin{scope}[shift={(1,1.5)}]
      \draw [line width=0.5pt] ++(0:1) -- ++(72:1) -- ++(144:1) -- ++(216:1) -- ++ (288:1) -- cycle;
    \end{scope}
  \end{tikzpicture}

regular polygon on grid

EDIT EDIT

Or irregular...

  \begin{tikzpicture}
    \draw [gray, line width=.1pt] (0,0) grid [xstep=0.5, ystep=0.5] (6,6);
    \begin{scope}[shift={(3,1.5)}]
      \draw [line width=0.5pt] ++(0:2) -- ++(72:1) -- ++(144:3) -- ++(216:2.5) -- ++ (288:.5) -- cycle;
    \end{scope}
  \end{tikzpicture}

irregular convex polygon