19

I have a set of three regular polygons with six sides which I create using regular polygon from the shapes tikz package. However this always creates the polygon with the flat side on the bottom. How do I get one of the corners on the bottom with minimal changes to my code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}
\def\x{0}
\def\y{0}
\def\r{1.5}
\pgfmathsetmacro\dx{\r*(1+cos(60))}
\pgfmathsetmacro\dy{\r*sin(60)}
\pgfmathsetmacro\dAx{\r*cos(60)}
\pgfmathsetmacro\dAy{\r*sin(60)}
\pgfmathsetmacro\dBx{\r*cos(120)}
\pgfmathsetmacro\dBy{\r*sin(120)}
\pgfmathsetmacro\d{2*\r}

\def\xc{\x} %mittelpunkt des aktuellen hexagons
\def\yc{\y}
\node[draw, regular polygon, regular polygon sides=6,minimum size=\d cm]
at (\x,\y) {};
\node[circle, radius=0.15cm, label=A, fill=black] (A) at (\xc+\dAx,    
\yc+\dAy) {};
\node[circle, radius=0.15cm, label=below:A, fill=black] (AA) at     
(\xc+\dAx,\yc-\dAy) {};
\node[circle, radius=0.15cm, label=A, fill=black] at (\xc-\r,\yc) {};
\node[circle, radius=0.15cm, label=B, fill=gray] at (\xc+\dBx,\yc+\dBy) {};
\node[circle, radius=0.15cm, label=below:B, fill=gray] at (\xc+\dBx,    \yc-\dBy) {};

\def\xc{\x+\dx}
\def\yc{\y+\dy}
\node[draw, regular polygon, regular polygon sides=6,minimum size=\d cm]
at (\x+\dx,\y+\dy) {};
\node[circle, radius=0.15cm, label=A, fill=black] (AAA) at (\xc+\dAx,\yc+\dAy) {};
\node[circle, radius=0.15cm, label=right:A, fill=black] (AAAA) at (\xc+\dAx,\yc-\dAy) {};
\node[circle, radius=0.15cm, label=right:B, fill=gray] (BB) at (\xc+\r,\yc) {};
\node[circle, radius=0.15cm, label=B, fill=gray] at (\xc+\dBx,\yc+\dBy) {};

\def\xc{\x+\dx}
\def\yc{\y-\dy}
\node[draw, regular polygon, regular polygon sides=6,minimum size=\d cm]
at (\x+\dx,\y-\dy) {};
\node[circle, radius=0.15cm, label=below:A, fill=black] at (\xc+\dAx,\yc-\dAy) {};
\node[circle, radius=0.15cm, label=right:B, fill=gray] (BBB)at (\xc+\r,\yc) {};
\node[circle, radius=0.15cm, label=B, fill=gray] (B) at (\xc+\dBx,\yc+\dBy) {};
\node[circle, radius=0.15cm, label=below:B, fill=gray] (BBBB)at (\xc+\dBx,\yc-\dBy) {};
\end{tikzpicture}
\end{document}

Latex output

7
  • rotate=<angle> in the node options will turn the node. Is that what you mean? Obviously that is going to throw your calculated positions off if you still want them to fit together.
    – cfr
    Commented Apr 29, 2016 at 12:57
  • Rotating the shape is easy, but they're not going to match anymore. Is that a problem?
    – Alenanno
    Commented Apr 29, 2016 at 12:59
  • can I just rotate the whole picture at once. I tried Commented Apr 29, 2016 at 13:02
  • 1
    You can say \begin{tikzpicture}[rotate=30,transform shape], but the labels will be rotated as well. Commented Apr 29, 2016 at 13:04
  • Is there an easy fix for the labels as well? It looks almost right except for the labels as you stated. I want them to stick together like they already do. Just rotate by 30deg and keep the labels upright. Commented Apr 29, 2016 at 13:11

4 Answers 4

14

shape border rotate is the parameter that you can use to rotate a shape node. But in this case, you also need to change where labels are placed.

Unfortunately this answer will change all your code. It uses hexagon corners as reference points and also as anchors to place them one over the other. Hope it helps.

First example is not rotated and second one has a 30 degree rotation.

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}[%
    point/.style={circle, radius=0.15cm, fill=#1},    
    hex/.style={draw, regular polygon, regular polygon sides=6,minimum size=3 cm}]

\node[hex] (A) {};
\node[hex, anchor=corner 4] (B) at (A.corner 2) {};
\node[hex, anchor=corner 6] (C) at (A.corner 2) {};

\foreach \i/\j/\k in {
    B/1/above, B/3/above, B/5/right,
    C/3/above, C/5/below, A/5/below}
    \node[point=black, label=\k:A] at (\i.corner \j) {};

\foreach \i/\j/\k in {
    B/2/above, B/4/above, B/6/right,
    C/2/above, C/4/below, A/4/below, A/6/right}
    \node[point=gray, label=\k:B] at (\i.corner \j) {};

\end{tikzpicture}

\begin{tikzpicture}[%
    point/.style={circle, radius=0.15cm, fill=#1},    
    hex/.style={draw, regular polygon, regular polygon sides=6,minimum size=3 cm, shape border rotate=30}]

\node[hex] (A) {};
\node[hex, anchor=corner 4] (B) at (A.corner 2) {};
\node[hex, anchor=corner 6] (C) at (A.corner 2) {};

\foreach \i/\j/\k in {
    B/1/above, B/3/left, B/5/right,
    C/3/below, C/5/below, A/5/below}
    \node[point=black, label=\k:A] at (\i.corner \j) {};

\foreach \i/\j/\k in {
    B/2/above, B/4/above, B/6/right,
    C/2/above, C/4/below, A/4/below, A/6/right}
    \node[point=gray, label=\k:B] at (\i.corner \j) {};

\end{tikzpicture}

\end{document}

enter image description here

10

Okay, here is an even more compact form using the shapes.geometric library of tikz:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
%
\begin{document}
%
\begin{tikzpicture}
    \foreach \a in {30,150,270}{
        \node[regular polygon, regular polygon sides=6, draw, minimum size=3cm, rotate=30] (\a) at (\a:{1.5cm+.2pt}) {};       
        \foreach \b/\c in {1/2,3/4,5/6}{
            \fill (\a.corner \c) circle (2.5pt) node [above] {A};
            \fill[gray] (\a.corner \b) circle (2.5pt) node[black, below] {B};
            }
        }
\end{tikzpicture}
%
\end{document}

enter image description here

7

Here's a simplified version for your graph.

Output

enter image description here

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\tikzset{
    poly/.style={draw, regular polygon, regular polygon sides=6, rotate=30, minimum size=\d cm},
    style1/.style={circle, fill=gray, label={below:B}},
    style2/.style={circle, fill=black, label={above:A}},
}

\begin{document}
\begin{tikzpicture}
\def\x{0}
\def\y{0}
\def\r{1.5}
\pgfmathsetmacro\dx{\r*cos(30)}
\pgfmathsetmacro\dy{\r*cos(30)}
\pgfmathsetmacro\d{2*\r}
\def\xc{\x+\dx}

\node[poly] (n1) at (\x,\y) {};
\node[poly] (n2) at (\xc*2,\y) {};
\node[poly] (n3) at (\xc,\y-\r*1.5) {};

% foreach for all three
\foreach \xx/\group in {1/{1,2,3},2/{1,2,3,4,5,6},3/{2,3,4,5}}{%
    \foreach \x [evaluate=\x as \angle using int(90+(60*(\x-1)))] in \group {%% 
    \pgfmathsetmacro\switch{int(mod(\x, 2)) ? "style1" : "style2"} % choose style
    \node[\switch] at (n\xx.corner \x) {};
    }
}

\end{tikzpicture}
\end{document}
4

And (mainly for amusement at this end, but partly for comparison) here is a Metapost + luamplib implementation based on the observation that the A points form a triangle and the B points form a hexgaon.

enter image description here

Compile with lualatex (or adapt for plain Metapost).

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
beginfig(1);
path A, B; labeloffset := 5; u := 1cm;
A := for t=0 upto 2: (0,-2u)    rotated 120t -- endfor cycle;
B := for t=0 upto 5: (1.732u,0) rotated  60t -- endfor cycle;
draw for t=0 upto 5: point (t+1)/2 of A -- point t of B -- endfor cycle;
     for t=0 upto 2: draw origin -- point t+1/2 of A; endfor
     for t=0 upto 5: 
       fill fullcircle scaled 4 shifted point (t+1)/2 of A; 
       fill fullcircle scaled 4 shifted point t of B withcolor .5 white;
       label.top("A", point (t+1)/2 of A);
       label.bot("B", point t of B);
     endfor
     fill fullcircle scaled 4 withcolor .5 white;
     label.bot("B", origin);
endfig;
\end{mplibcode}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .