Regular polygons can be drawn after loading the shapes.geometric
library by using something like \node (<name>) [regular polygon, regular polygon sides=5, draw] {}
for a pentagon. The corners of the polygon are then available as (<name>.corner <n>)
, and the midpoints of the sides as (<name>.side <n>)
.
To reflect the corners along a line coincident with a side, you can use a combination of two TikZ functions: The let
syntax, which allows you to temporarily store a point in a macro, and the calc
library, with which you can perform coordinate calculations.
Here's a pentagon with the corners labeled:
\node (A) [draw,regular polygon, regular polygon sides=5, minimum size=2cm,outer sep=0pt] {};
\foreach \n in {1,...,5} {
\node at (A.corner \n) [anchor=360/5*(\n-1)+270] {\scriptsize\texttt{A.corner \n}};
}
Now say you want to mirror corner 4 on the line passing through corner 1 and corner 2. TikZ doesn't provide a mirror operation directly, but you can use the projection syntax for this. Coordinate calculation expressions are surrounded by ($...$)
, and the projection syntax is (<first coordinate)!(<projection coordinate>)!(<second coordinate>)
, so the projection of corner 4 on the line passing through corners 1 and 2 can be expressed as
($(A.corner 1)!(A.corner 4)!(A.corner 2)$)
If we call that point Q
, the reflection of corner 4 (let's call that point C4
) can be expressed as C4' = Q - (C4 - Q) = 2*Q - C4
. In TikZ notation, this would look like
($ 2*($(A.corner 1)!(A.corner 4)!(A.corner 2)$) - (A.corner 4) $)
In order to do this for all corners of the polygon, we can use a \foreach
loop. To make the code a little more readable, the points A.corner 1
and A. corner 2
can be saved into temporary macros using the let
syntax. Finally, the whole thing can be wrapped into a TikZ style.
The lines
\node (A) [polygon=5, thick] {};
\draw [blue,mirror polygon=1];
\draw [orange,mirror polygon=2];
\draw [red,mirror polygon=3];
\draw [cyan,mirror polygon=4];
\draw [purple,mirror polygon=5];
will yield
and the lines
\node (A) [polygon=3, thick] {};
\draw [blue,mirror polygon=1];
\draw [orange,mirror polygon=2];
\draw [red,mirror polygon=3];
will yield
Here's the complete code:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, calc}
\begin{document}
\begin{tikzpicture}
\tikzset{
polygon n/.code=\gdef\polygonN{#1}, % Save N in a global macro
polygon/.style={
regular polygon,
regular polygon sides=#1,
polygon n=#1,
draw,
minimum size=2cm,
outer sep=0pt
},
mirror polygon/.style={
insert path={
let \p1 = (A.corner #1),
\p2 = (A.side #1) in (\p1)
($ 2*($(\p1)!(A.corner 1)!(\p2)$) - (A.corner 1) $) % Path needs to be started before the foreach
\foreach \n in {2,...,\polygonN} {
-- ($ 2*($(\p1)!(A.corner \n)!(\p2)$) - (A.corner \n) $)
} -- cycle
}
}
}
\node (A) [polygon=5, thick] {};
\draw [blue,mirror polygon=1];
\draw [orange,mirror polygon=2];
\draw [red,mirror polygon=3];
\draw [cyan,mirror polygon=4];
\draw [purple,mirror polygon=5];
\end{tikzpicture}
\end{document}
Best Answer
The
regular polygon
shape is not a "TikZ shape" (and neither are any of the other predefined shapes), they're all "PGF shapes". General rule: If the relevant keys in thepgfmanual
include the/pgf
directory, they're PGF keys. To set the parameters likeregular polygon sides
, you have to call\pgfkeys{/pgf/<option>=<value}
: