# Is there a way in TikZ to reflect regular polygons of an arbitrary number of edges?

Is there a way in TikZ to reflect regular polygons of an arbitrary number of edges, along each edge-considered-as-mirror?

I'm using MacTeX, and I'm new to LaTeX, pgf and TikZ.

I'd like to do this to demonstrate a geometric construction that can be done using reflections, with polygons of an arbitrary number of edges.

For some polygons (e.g. triangles and hexagons), this can be iterated to generate a tessellation of the plane. (Others have asked about hexagonal tilings, etc.)

Ideally, I'd like to be able simply to reference an edge, generate a line coincident with it, and provide the line as an argument to a subroutine that can perform the reflection of the polygon along the line through that edge.

This reflection can be simulated by rotations and translations, but I'd like to avoid the translations altogether (and the translation distance computations in (x,y) coordinates, or even in polar coordinates). This is to have the code also serve as a demonstration of the principles of construction, not just the graphics. For polygons of an odd number of edges n, a rotation of 180/n is needed before translation; for polygons of an even number of edges, such rotation is not needed.

I've tried referencing nodes of superposed constructions to re-center the effectively-reflected polygons, but my references didn't seem to work. I've also tried what I'd thought were planar coordinates (ordered pairs) based on a radial coordinate system, which I'd taken from an example, but they didn't work as expected in my code.

-
Welcome to TeX.SE. It is always best to compose a MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. In this case you could at least provide the code to produce the original polygon. –  Peter Grill Nov 12 '11 at 4:28

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}

-

run texdoc pst-cox for the documentation of creating regular polytopes. A simple example, run with xelatex:

\documentclass{minimal}
\usepackage{pst-coxcoor}
\begin{document}
\begin{pspicture}(-4,-4)(4,4)
\CoxeterCoordinates[linewidth=0.1mm,choice=33]
\end{pspicture}
\end{document}


-
As the question is about Tikz, a preamble in the answer explaining this is an alternative approach would be useful. –  Joseph Wright Nov 12 '11 at 19:19

Similar to Jake’s solution but with my qrr.trans library ([1], [2]).

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric,qrr.trans}
\tikzset{
reg poly/.style={shape=regular polygon, regular polygon sides={#1}, outer sep=+0pt}}
\tikzset{
unreg poly/.style={/utils/exec=\def\myPolygonCounter{1},
@unreg poly/.list={#1,(unreg poly-corner 1)}, insert path={-- cycle}},
@unreg poly/.style={
insert path={coordinate         (unreg poly-corner \myPolygonCounter)
-- coordinate[midway] (unreg poly-side \myPolygonCounter) #1},
/utils/exec=\pgfmathtruncatemacro\myPolygonCounter{\myPolygonCounter+1}},
unreg poly*/.style={
@unreg poly*/.list={#1},
insert path={-- cycle}},
@unreg poly*/.style={insert path={-- #1}}}
\tikzset{every picture/.append style={line join=round,thick}}
\begin{document}
\begin{tikzpicture}
\node[reg poly=5, minimum size=+2cm, draw, very thick] (a) {};
\foreach \i[evaluate={\col=(\i-1)/.04}] in {1,...,5}
\node [mirror=(a.corner \i)--(a.side \i), transform shape,
reg poly=5, minimum size=+2cm, draw=red!\col!blue] {};
\end{tikzpicture}
\begin{tikzpicture}
\draw[very thick] (0,0) [unreg poly={(1,1), (2,-.5), ++(south west:1), ++(left:1)}];
%\foreach \i[evaluate={\col=(\i-1)/.04}] in {1,...,5}
%  \draw[red!\col!blue, mirror=(unreg poly-corner \i)--(unreg poly-side \i)]
%    (0,0) [unreg poly*={(1,1), (2,-.5), ++(south west:1), ++(left:1)}];
\foreach \i[evaluate={\col=(\i-1)/.04}] in {1,...,5}
\draw[red!\col!blue, m/.style={mirror=(unreg poly-corner \i)--(unreg poly-side \i)}]
([m]unreg poly-corner 1) \foreach\j in {2,...,5}{-- ([m]unreg poly-corner \j)} -- cycle;
\end{tikzpicture}
\end{document}


-