6

The goal: Make a polygon (and a circle containing that polygon at the nodes of the polygon, without the polygon's lines) with automatically numbered nodes according to their coordinates (x,y) [which latex must calculate] or unique names.

[EDIT: I have added a different MWE (Ignasi's), to clarify, since it does away with two macros in my old MWE at expense of one option. In this example: how does one control the size of the dots on the nodes [the option lost], and to make (x,y) coordinate positions of the nodes their visible labels, as per title of the question?]

Of course,

\node (n1) at (x,y) {$1$};

or

\node (n1) at (x,y) {$\boldsymbol{W_1(z)}$};

or

\node (n1) at (x,y) {$(x,y)$};

could be done manually on top any figure otherwise made but for more many nodes makes for long code and lots of guessing where (x,y) the nodes on the circle or polygon actually are.

!!! The labels should preferably all be away from the lines of the nodes, like here: Drawing a regular polygon encompassed by a circle

MWE(Ignasi):

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}[scale=1]
\foreach \a in {4,...,5}
{
\draw[blue, solid, line width = 1mm] (\a*7,0) circle(3cm);
\node[regular polygon,
    regular polygon sides=\a,
    minimum size=6cm,
    draw] at (\a*7,0) (A) {};
\foreach \i in {1,...,\a}
    \node[circle,
    label=above:{$w_\i(z)$},
    fill=red] at (A.corner \i) {};
}
\end{tikzpicture}

\end{document}

MWE(old):

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

%POLYGON command
\newcommand{\polygon}[2]{%
let \n{len} = {2*#2*tan(360/(2*#1))} in
++(0,-#2) ++(\n{len}/2,0) \foreach \x in {1,...,#1} { -- ++(\x*360/#1:\n{len})}}

%DOTS command
\makeatletter
\tikzset{
    dot diameter/.store in=\dot@diameter,
    dot diameter=3pt,
    dot spacing/.store in=\dot@spacing,
    dot spacing=7pt,
    dots/.style={
    line width = \dot@diameter,
    line cap = round,
    dash pattern = on 0pt off \dot@spacing}
        }\makeatother

%BEGIN

\begin{tikzpicture}
\node (n1) at (-2,-2) {$\boldsymbol{0}$};

\draw[black, dot diameter=3pt, dot spacing=40pt, dots] (0,0) circle (2);
\draw[black, solid, line width=0.5mm] (5,0) \polygon{9}{2};
\draw[black, solid, line width=0.5mm] (10,0) circle (2);
\end{tikzpicture}

%figure out how to automatically number nodes

\end{document}
  • 2
    did you check shapes.geometric library? – percusse May 22 '14 at 22:02
  • Shouldn't the title of the question say "automatically labelled" instead of "automatically numbered"? – JLDiaz May 23 '14 at 7:37
  • You're right, it should! – Guido Jorg May 24 '14 at 10:07
8

Next is an example from page 177 on pgfmanual with some little additions

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

\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
\foreach \a in {3,...,7}{
\draw[blue, dashed] (\a*4,0) circle(1.5cm);
\node[regular polygon, regular polygon sides=\a, minimum size=3cm, draw] at (\a*4,0) (A) {};
\foreach \i in {1,...,\a}
    \node[circle, label=above:\i, fill=red] at (A.corner \i) {};
}
\end{tikzpicture}

\end{document}

enter image description here

Update

Next code show how to draw or undraw regular polygons defined with \node[regular polygon,..., it also show how you can cusomize corner marks and print corner coordinates.

Code for coordinates printing was take from Paul Gaborit's answer and from Torbjørn T. suggested question.

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

\usetikzlibrary{shapes.geometric,positioning}

\makeatletter
\newcommand\xcoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@x/\pgf@xx}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@y/\pgf@yy}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

\begin{document}

\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2}
\tikzset{dot/.style={circle,fill=blue,minimum size=3pt, inner sep=0pt},
         square/.style={rectangle,fill=red,minimum size=4pt, inner sep=0pt,%    
                    rotate=30,anchor=center},%
        mytext/.style={anchor=north,text width=3cm,align=center}
    }

\newcommand{\labelcorner}[2][]{%
    \coordinate (aux) at (#2);
    \node[#1] at (aux) {(\xcoord{aux},\ycoord{aux})}}

\begin{tikzpicture}

\node[regular polygon, regular polygon sides=9, minimum size=3cm] (A) {};
\foreach \i in {1,...,9}
    \node[dot] at (A.corner \i) {};
\node[mytext, below=of A.south] {non drawn nonagon};

\node[regular polygon, regular polygon sides=9, minimum size=3cm, right=3cm of A, rotate=15,draw, anchor=center] (B) {};
\foreach \i in {1,...,9}
    \node[square] at (B.corner \i) {};
\node[mytext, below=of B.south] {drawn nonagon with squared dots in each corner};

\node[regular polygon, regular polygon sides=5, minimum size=3cm, right=4cm of B.center, rotate=25, anchor=center] (C) {};
\draw[thick,purple] (C.corner 1)--(C.corner 2)--(C.corner 3)--(C.corner 4);
\draw[dashed,blue] (C.center) circle (1.5cm); 
\foreach \i in {1,...,5}
    \node[square, green] at (C.corner \i) {};
\node[mytext, below=of C.south] {partially drawn pentagon with circumscribed circle};

\labelcorner[above]{A.corner 1};

\labelcorner[below left,font=\tiny]{B.corner 4};
\end{tikzpicture}
\end{document}

enter image description here

  • @GuidoJorg I don't understand your comment. With a regular polygon you get special anchors in every corner. This anchors are node name.corner 1, node name.corner 2, ... You can use these anchor to draw anything you want on them (I've drawn an empty circular node with a label on top of them) and you can draw any label near them. Isn't it what you want? – Ignasi May 22 '14 at 22:52
  • I meant: how do I get the (x,y) coordinates of the nodes into the label that shows up? The label part is label=above:{\i}, for 1, 2, 3, ..., or label=above:{$w_\i(z)$}, for w_1(z), w_2(z), w_3(z), and so on. But how to get (x,y) coordinates of the nodes calculated and part of the label, like in (3,3), (4,2), (3,1), ...? Also, like in the MWE, how to make the nodes appear without any polygon lines, if necessary. (In that case, there would only be a circle with the nodes, or just the nodes, if the circle command is deleted.) – Guido Jorg May 23 '14 at 0:05
  • 1
    @GuidoJorg To remove the lines of the polygon, remove draw from the node options. One way of getting the coordinates of a node anchor is given in tex.stackexchange.com/questions/18292/… – Torbjørn T. May 23 '14 at 5:37
  • 1
    @GuidoJorg I've update my answer. I hope it's better now. – Ignasi May 23 '14 at 8:09
  • Three good answers, but this code seems to be the productive and robust in the end. :) – Guido Jorg May 24 '14 at 10:12
4

Here is an example with explicit coordinates:

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{pt/.style={circle,fill=#1,minimum size=6pt,inner sep=0}}
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2}
\begin{document}
\begin{tikzpicture}
  \foreach \n in {3,...,8}{
    \begin{scope}[yshift=\n*4.5cm]
      \pgfmathsetmacro\angle{360/\n}
      % circle
      \draw[dashed] circle(2cm);
      % polygon
      \draw[red] (\angle:2cm) node[name=s 1,pt=blue]{}
      \foreach \v in {2,...,\n}{
        -- (\angle*\v:2cm) node[name=s \v,pt=blue]{}
      } -- cycle;
      % coordinates and labels
      \foreach \v in {1,...,\n}{
        % extraction of coordinates
        \path let \p1=(s \v) in \pgfextra{
          \pgfmathsetmacro\x{\x1/2cm} % scale to unit circle 
          \pgfmathsetmacro\y{\y1/2cm} % scale to unit circle 
        }
        (\angle*\v:2cm) -- (\angle*\v:0cm)
        node[pos=.5,sloped,font=\scriptsize]
          {$\pgfmathprintnumber{\x},\pgfmathprintnumber{\y}$}
        (\angle*\v:2.3cm)
        node[font=\scriptsize] {\v};
      }
    \end{scope}
  }
\end{tikzpicture}
\end{document}
  • After about a day with three good answers, I'd like to give acceptance to all 3 ... :) Interesting use of angles for labels. – Guido Jorg May 24 '14 at 10:14
4

Combining the answer by Ignasi to this question and the answer by Jake to Accessing the logic values of a TikZ coordinate, I built what I think is what the OP asked for:

Result

Code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{shapes.geometric,calc}

\makeatletter
\newcommand\xcoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@x/\pgf@xx}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@y/\pgf@yy}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

\begin{tikzpicture}
\foreach \a in {3,...,5}{
\draw[blue, dashed] (\a*6,0) circle(2cm);
\node[regular polygon, regular polygon sides=\a, minimum size=4cm, draw] at (\a*6,0) (A) {};
\foreach \i in {1,...,\a} {
    \coordinate (aux) at (A.corner \i);
    \node[circle, fill=red] at (aux) {};
    \path (A.center) -- (aux) node[pos=1.3] {(\xcoord{aux},\ycoord{aux})}; 
 }
}
\end{tikzpicture}
\end{document}
  • After about a day with three good answers, I'd like to give acceptance to all 3 ... :) – Guido Jorg May 24 '14 at 10:14

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.