4

I have a diagram that is just the 4 vertices of a square, all colored. I want to connect the vertices so that the square has edges. How do I do this?

My code currently looks like this:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
    \begin{tikzpicture}[rotate=45]
    \foreach \a in {0,90,180,270} { %\a is the angle variable
    \draw[line width=.7pt,purple,fill=purple] (\a:1.5cm) circle (2pt); }
    \end{tikzpicture}
\end{document}
  • 1
    Welcome! Please could you complete your code fragment so we have a complete minimal example we can compile? – cfr Dec 20 '15 at 23:07
6

Probably the simplest is to place coordinates where the circles are and then just join them afterwards. For example:

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[rotate=45]
  \foreach \a in {0,90,180,270} { %\a is the angle variable
    \draw[line width=.7pt,purple,fill=purple] (\a:1.5cm) coordinate (a\a) circle (2pt); }
  \draw [line width=.7pt,purple] (a0) -- (a90) -- (a180) -- (a270) -- cycle;
\end{tikzpicture}
\end{document}

join the dots

You don't want to draw the lines at the same time as the circles as you don't want the square itself to be filled, I assume.

Note that, as it stands, your code doesn't use the library you load. However, you could use that library to draw the picture in another way by making the square a node and then adding circles at the anchors at each of its four corners:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
  \node (s) [minimum width=3cm, minimum height=3cm, regular polygon, regular polygon sides=4, line width=.7pt, draw=purple] {};
  \foreach \i in {north west, north east, south west, south east}
    \draw [fill=purple, purple, line width=.7pt] (s.\i) circle (2pt);
\end{tikzpicture}
\end{document}

The output is identical to that above.

| improve this answer | |

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.