4

Question

I would like to know how to add an arbitrary number of coordinates to the default shapes of TikZ—particularly to the side of a polygon.

Sample

This is similar to https://tex.stackexchange.com/a/47856/13552

TikZ Polygon Shape

4

If you're looking for an arbitrary coordinate around the perimeter you can use (name.number), where name is the shape name and number is an angle in degrees measured from name.east.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,plotmarks}
\begin{document}
\begin{tikzpicture}
\node[name=s, shape=regular polygon, inner sep=1cm,draw,color=black!30,thick] {};
\draw[shift=(s.east),blue!70] plot[mark=o] coordinates{(0,0)} {};
  \foreach \anchor in
    {0,10,20,...,350}
\draw[shift=(s.\anchor),red] plot[mark=x] coordinates{(0,0)} {};
\end{tikzpicture}
\end{document}

enter image description here

Edit

When using shape=regular polygon, TikZ names all the sides and corners, starting from the top of the shape (see TikZ 3.0.0 manual Sec. 67.3 Geometric Shapes for details.) In order to specify an arbitrary point along an arbitrary side, you could define a command that places a point some distance between two consecutive corners (i.e., along a side) of the regular polygon. This requires \usetikzlibrary{calc}.

In the example below, I've defined the command mysidemark, which has the following syntax: \mysidemark{<start corner>}{<end corner>}{<fraction of distance along path (between 0 and 1)>}.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric}
\begin{document}
\newcommand{\mysidemark}[3]{%
\draw[red,shift=($(s.corner #1)!#3!(s.corner #2)$)] plot[mark=*] coordinates{(0,0)} {};
}
\begin{tikzpicture}
\node[name=s, shape=regular polygon, regular polygon sides=6, inner sep=1cm,draw,color=black!30,thick] {};
\mysidemark{1}{2}{0}
\mysidemark{1}{2}{0.17}
\mysidemark{1}{2}{0.46}
\mysidemark{2}{3}{0.66}
\mysidemark{2}{3}{0.90}

\begin{scope}[xshift=4cm]
\node[name=s, shape=regular polygon, regular polygon sides=7, inner sep=1cm,draw,color=black!30,thick] {};
\mysidemark{2}{3}{0.07}
\mysidemark{3}{4}{0.25}
\mysidemark{6}{7}{0.77}
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Note

This command can be used explicitly, as shown, but more work is necessary to allow it to be used in a \foreach command. See, e.g., \foreach with \newcommand how to use them together?.

  • How would you recommend doing exactly that but for a single side..say from {1,2,..100} on a side, so that I can connect to any shape's side in the same position regardless of size (as shape size increases, the perimeter units loose their resolution--effectively moving anchor points around the shape in a counter-clockwise direction). I had considered making a path using the predefined anchor points of the shape in question. Do you have any ideas? – Jonathan Komar Nov 26 '14 at 14:13
  • In other words, could I apply your code to a segment? Given segment A-B, add an arbitrary number (likely 100) of coordinates to that segment (a line). – Jonathan Komar Nov 28 '14 at 12:28
  • 1
    @macmadness86 Please see my edit. – erik Nov 29 '14 at 6:56
6

Defining new anchors is tricky and basically impossible without looking at the PGF code used to define the shape. But aliasing anchors is quite straightforward (once you have the command to do it):

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{shapes.geometric}
\makeatletter
\def\pgfaliasanchor#1#2#3{%
  \expandafter\gdef\csname pgf@anchor@#1@#2\endcsname{%
    \pgf@sh@reanchor{\csname pgf@sh@ns@\pgfreferencednodename\endcsname}{#3}%
  }%
}%
\makeatother

\pgfaliasanchor{regular polygon}{peter}{90}
\pgfaliasanchor{regular polygon}{paul}{330}
\pgfaliasanchor{regular polygon}{mary}{south west}
\begin{document}
\begin{tikzpicture}
\node [fill=gray!50, regular polygon, minimum size=1.5in] (shape) at (2,2) {};
\foreach \anchor in {peter, paul, mary}
  \draw [draw=red] (shape.\anchor) 
    circle [radius=1/20] node [above] {\tt\anchor};
\end{tikzpicture}
\end{document}

enter image description here

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.