6

Here is a simple path, which has a symmetry:

\begin{document}
\begin{tikzpicture}
\draw[thick] (0,0) -- ++(-9,0) -- ++(5,3) -- ++(2,7) -- ++(2,0);

\draw[green,thick] (0,10) -- ++(2,0) -- ++(2,-7) -- ++(5,-3) -- ++(-9,0); \end{tikzpicture} \end{document}

Now instead of drawing the green part manually, would it be possible to just say -- mirror cycle or similar and draw it automatically? I know there are ways as explained in Can we mirror a part in tikz ("axial symmetry", "reflection")? but there the path is not continous and it requires an extra environment, where the path is copied, so there is no reduction in maintanence, when the path changes.

TobiBS
  • 5,240

3 Answers3

10

It can be done with spath3

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{spath3}
\begin{document}
\begin{tikzpicture}
\path[spath/save=apath] (0, 0) -- ++(-9, 0) -- ++(5, 3) -- ++(2, 7) -- ++(2, 0);
\draw[thick, fill=green, spath/use=apath] [spath/transform={apath}{xscale=-1}, spath/use={apath, reverse, move, weld}] -- cycle;
\end{tikzpicture}
\end{document}

Green filled symmetrical shape

  • Nice solution! Where can I find documentation on that tikz library? I tried to find out more about it on the pgfmanual on CTAN but there was nothing. – Mark Fantini Mar 21 '22 at 21:04
  • @MarkFantini: You presumably already have the documentation as part of your LaTeX installation - just type texdoc spath3 in a terminal. -or you can find it here: https://ctan.org/pkg/spath3?lang=en – hpekristiansen Mar 21 '22 at 21:18
  • Thank you! Will check it. Can I find other docs like this? I'm new to using the terminal by trying other stuff (learning Python and programming), and it could be a game changer to find docs like this instead of going to ctan.org. EDIT: Just tried it and it works! This is amazing!! Thank you a lot!! – Mark Fantini Mar 28 '22 at 04:38
5

The mirror axis is at x = 0, thus a simple macro with the sign as parameter is a simple solution. Because of the color change, a continuous solution is not possible anyway:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thick]
    (0, 0) -- ++(-9, 0) -- ++(5, 3) -- ++(2, 7) -- ++(2, 0)
  ;
  \draw[green, thick]
    (0, 10) -- ++(2, 0) -- ++(2, -7) -- ++(5, -3) -- ++(-9, 0)
  ;
\end{tikzpicture}
\end{document}

enter image description here

A continuous solution is more complicate. For example, the mirrored points can be calculated and stored in a list in reverse order:

\documentclass[tikz]{standalone}

\newdimen\myX

\makeatletter \newcommand{\myInit}[1]{% #1% \pgfextra{% \global\myX\pgf@x \global\let\myList@empty }% } \newcommand{\myNext}[1]{% -- #1% \pgfextra{% \xdef\myList{% --(\the\dimexpr2\myX-\pgf@x\relax,\the\pgf@y)% \myList }% }% } \makeatother

\begin{document} \begin{tikzpicture} \draw[thick, fill=lightgray] \myInit{(0, 0)} \myNext{++(-9, 0)} \myNext{++(5, 3)} \myNext{++(2, 7)} \myList -- cycle ; \end{tikzpicture} \end{document}

enter image description here

Heiko Oberdiek
  • 271,626
2

To illustrate handy path operations in Asymptote: reverse, xscale, etc.

enter image description here

// copy to http://asymptote.ualberta.ca/ and click Run
unitsize(1cm);
path Lpath=(0,0)--(-9,0)--(-4,3)--(-2,10)--(0,10);   // the left path
path Rpath=xscale(-1)*Lpath;                         // the right path is symmetric with the left to axis x=0   
path mypath=Lpath--reverse(Rpath)--cycle;            // the whole (closed) path
fill(mypath,lightgreen);                            // filling the whole path
draw(Lpath,red+2pt);                                // draw the left path    
draw(Rpath,blue+2pt);                               // draw the right path

shipout(bbox(5mm,invisible)); // make the boundary bigger

Update A more appropriate for the whole path is using & connector, as suggested by @NguyenVanChi1998

path mypath=Lpath & reverse(Rpath) & cycle;
Black Mild
  • 17,569