2

I am trying to draw two arc sectors whose origin is an arbitrary point. I feel like I am missing something here, because this should work, but, as you can see in the image below, the red arcs are centralized on +90 degrees, instead of facing the black dot:

enter image description here

What I am doing is (MWE below):

  1. If I want to draw the three arcs from the red point to the black, I use the macro \ExtractCoordinateto get the coordinates of the black dot.
  2. Then, I use tikzmath and atan2 to get the angle between the red and black points. As I am within a scope, I dont need the red point coordinates, since its defined at (0,0) in the scope <- (!) I feel the error might be here.
  3. Finally, I draw three parametrized arcs with different radii.

As you can see in the image, this process works for the arcs between black -> red, but not for the ones between red -> black. What am I missing here?

Thank you in advance!

MWE:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{positioning,calc,math}
\tikzstyle{help lines}=[thin,gray!40]
%
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%
%
\begin{document}
    \begin{tikzpicture}
        %\draw[help lines,step=1] (-3,-3) grid (3,3);
        \coordinate (Origin) at (0,0);
        %- Red
        \begin{scope}[thick,color=red,shift={(-2,0.5)}]
            \coordinate (Tag 1) at (0,0);
            \draw[fill] (Tag 1) circle (4pt)  node[above,yshift=0.1cm]  {Tag 1};
            \ExtractCoordinate{$(Origin)$};
            \tikzmath{
                \middleAng = atan2(\YCoord,\XCoord);
            }
            \foreach \y in {0.5,0.75,1}
            \draw[domain=\middleAng-15:\middleAng+15] plot ({\y*cos(\x)}, {\y*sin(\x)});
        \end{scope}
    
        \ExtractCoordinate{$(Tag 1)$};
        \tikzmath{
            \middleAng = atan2(\YCoord,\XCoord);
        }
        \foreach \y in {0.5,0.75,1}
            \draw[thick,domain=\middleAng-15:\middleAng+15] plot ({\y*cos(\x)}, {\y*sin(\x)});
        
        \draw[fill] (Origin) circle (4pt)  node[right,xshift=0.1cm]  {Reader};  
    \end{tikzpicture}
\end{document}

2 Answers 2

3

You can try with expanding waves decoration

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc, decorations.pathreplacing}
%
\begin{document}
   \begin{tikzpicture}[mywaves/.style={decoration={expanding waves, angle=15, segment length=2mm}, decorate, thick}]
    \fill[red] (-2,0.5) coordinate[label=Tag1] (Tag1) circle (4pt);
    \fill[black] (0,0) coordinate[label=right:Reader] (Origin) circle (4pt);
    \draw[mywaves] (Origin)--($(Origin)!.5!(Tag1)$);
    \draw[mywaves, red] (Tag1)--($(Origin)!.5!(Tag1)$);
   \end{tikzpicture} 
\end{document}

enter image description here

1
  • Thank you! I did not know that decoration existed, very neat
    – Jes
    Feb 23, 2021 at 8:47
2

I think that Ignasi has the best solution for you -- why re-invent the expanding waves wheel? -- but I do find it interesting to figure out why something that ought to work doesn't. So this is about what's going wrong rather than with how best to fix it.

You are right in thinking that the issue is with the scope. The problem is that the transformation defined by the scope is interfering with your coordinate calculation.

At its heart, as far as I understand it, what you are trying to do at that point is to say "I have two coordinates -- say a and b -- and I want to construct a point that is as b is from a." Without any scopes, this is straightforward as it is at ($(b)-(a)$).

However, when you put transformations into the picture then things get a little messy. The coordinates get placed at specific points and they are then baked and unaffected by local transformations. That is, \draw (a) -- (b); produces the same path regardless of what transformations are in effect because when TikZ sees (a) and (b) then it turns off the transformation while it parses them to figure out where they are. But once TikZ has figured out where they are then in ($(a)-(b)$) they get fed back in to the parser and now TikZ forgets that they originally came from coordinates and so applies any local transformations.

It's quite intriguing, because there are various wrong answers that feel like they ought to work:

  1. The original \ExtractCoordinate{$(Origin)$} produces the point (0pt,0pt) because that's where (Origin) is located in the main coordinate system.

  2. The difference \ExtractCoordinate{$(Origin)-(Tag 1)$}, which is what you would have to do if (Tag 1) were not at the origin of the local scope, also fails. Although (Origin)-(Tag 1) gets parsed correctly, it then gets shifted by the transformation and results back in ... (0pt, 0pt).

The solution is to stop TikZ using the local transformation when it does the coordinate calculation, and to use $(Origin)-(Tag 1)$ in that calculation.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/584452/86}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,math}
\tikzstyle{help lines}=[thin,gray!40]
%
%\newdimen\XCoord
%\newdimen\YCoord

\makeatletter

\newcommand*{\ExtractCoordinate}[1]{\path[reset cm] (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%
%
\begin{document}
    \begin{tikzpicture}
        %\draw[help lines,step=1] (-3,-3) grid (3,3);
        \coordinate (Origin) at (0,0);
        %- Red
        \begin{scope}[thick,color=red,shift={(-2,0.5)}]
            \coordinate (Tag 1) at (0,0);
            \draw[fill] (Tag 1) circle (4pt)  node[above,yshift=0.1cm]  {Tag 1};
            \ExtractCoordinate{$(Origin)-(Tag 1)$};
            \tikzmath{
                \middleAng = atan2(\YCoord,\XCoord);
            }
            \foreach \y in {0.5,0.75,1}
            \draw[domain=\middleAng-15:\middleAng+15] plot ({\y*cos(\x)}, {\y*sin(\x)});
        \end{scope}
    
        \ExtractCoordinate{$(Tag 1)-(Origin)$};
        \tikzmath{
            \middleAng = atan2(\YCoord,\XCoord);
        }
        \foreach \y in {0.5,0.75,1}
            \draw[thick,domain=\middleAng-15:\middleAng+15] plot ({\y*cos(\x)}, {\y*sin(\x)});
        
        \draw[fill] (Origin) circle (4pt)  node[right,xshift=0.1cm]  {Reader};  
    \end{tikzpicture}
\end{document}

(Note that \pgfgetlastxy stores the coordinates in macros, not skips, so the \newdimen doesn't actually do anything.)

Expanding waves heading in the right direction

1
  • Thank you for taking the time to see what is going on in my code! It was an interesting read. I think I should rewrite my code and use coordinates intead of shifting the scope. It will surely simplify everything ;)
    – Jes
    Feb 23, 2021 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .