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.

4
  • You can store your path in a macro with \newcommand{\mypath}{...} and reuse it into the mirror scope.
    – SebGlav
    Mar 14 at 19:22
  • Ok, this solves the reuse part, but not the continuous path problem.
    – TobiBS
    Mar 14 at 20:04
  • The last answer to the post you provided seems to answer your need, but in pstricks. I don't think it's feasible in TikZ without having to dig into the pgf layer.
    – SebGlav
    Mar 14 at 21:01
  • This looks like the sort of thing that the spath3 library eats for breakfast. I'm not on a device where I can cook something up myself, but someone else might be able to. Mar 14 at 21:23

3 Answers 3

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

3
  • 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. Mar 21 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: ctan.org/pkg/spath3?lang=en Mar 21 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!! Mar 28 at 4: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

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;
2
  • 1
    The operator & (Chapter 5: Bezier curves) be a better replacement for your mypath. Mar 15 at 3:21
  • Yes, that is right! I added an update for that.
    – Black Mild
    Mar 15 at 14:10

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.