4

In the stamp class example in the pgfmanual, the position for the stamp is passed in as two separate numbers, an x coordinate and a y coordinate. Is it possible, instead, to pass in a coordinate or a 'point' of some kind?

@percusse I have made some progress since I posted the question but it's not very elegant. I would welcome pointers on a better way to get the coordinates into the class.

\documentclass{article}
\usepackage{tikz}
\usepgfmodule{oo}
%http://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz
\makeatletter
\newcommand{\gettikzxy}[3]{%
    \tikz@scan@one@point\pgfutil@firstofone#1\relax
    \edef#2{\the\pgf@x}%
    \edef#3{\the\pgf@y}%
}
\makeatother

\pgfooclass{rr}{
  \method rr(#1,#2) { % The constructor; everything is done in here
    % This doesn't seem to work
    % \def\start{#1} % nor \def\start{(#1)}
    % But here I can get named x and y coordinates
    \gettikzxy{(#1)}{\spx}{\spy}
    \gettikzxy{(#2)}{\epx}{\epy}
    % I'd like named points to work with
    \coordinate (Start) at (\spx, \spy);
    \coordinate (End) at (\epx, \epy);
   \draw (Start) -- (End) node[right] {It works!};
  }
}

\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (0,0);
    \coordinate (B) at (0,-1);
    \def\hi{0.25}
    \pgfoonew \AB=new rr(A,B);
    \fill (A) circle (3pt);
    \fill (B) circle (3pt);
\end{tikzpicture}
\end{document}

I am refactoring some code I wrote years ago. This is where I'm going:enter image description here

5
  • Yes but then you have to process the point and use the coordinates, can you provide your own code?
    – percusse
    Commented Oct 9, 2016 at 12:05
  • @percusse I have added code to my original question but I'm not sure whether you will be notified of the change so I've added this comment. I see your name frequently answering questions here and I'd appreciate feedback on the etiquette: should I edit the question or include all the code in a comment?
    – dmorg
    Commented Oct 12, 2016 at 2:08
  • No this is perfect. Thank you. Indeed it is the comment ping that goes out to users inbox not the question edits. I'll try to have a look in the evening.
    – percusse
    Commented Oct 12, 2016 at 9:27
  • You start with named coordinates. Then you break them up. Then you turn them back into named coordinates, where these are the same coordinates you started with but named differently. This seems a very scenic route .... Why not just \coordinate (start) at (#1);? I'm missing something obvious, I guess ....
    – cfr
    Commented Oct 13, 2016 at 21:58
  • Yes, it's a circuitous route but I haven't managed to access the coordinates directly from the method parameters. @percusse mentioned above that, although you can pass the coordinates, you then have to extract the x and the y coordinates. If you h
    – DaveM
    Commented Oct 15, 2016 at 18:57

1 Answer 1

2

The reason why the position is passed as two separate numbers is that the comma in \mystamp.apply(1,2) serve as the separator of parameters, not part of the coordinate.

You probably had already known that in the usual TikZ, it is inappropriate to write

\draw[shift=(3,4)](A)--(B);

simply because the comma serves as the separator of styling keys, and one should instead write

\draw[shift={(3,4)}](A)--(B);

Here the idea is the same: if you are willing to protect your parameters by braces, then you can pass almost everything: including but not limited to

  • xy-coordinates (1,2)
  • xyz-coordinates (3,4,5)
  • polar coordinates (60:7)
  • coordinate system (canvas cs:x=8,y=9)
  • node and node-anchor (A.south east)
  • calculations provided by calc library ($(A)+(B)!.5!(C)$)

Basically the dirty job is done by the underlying TikZ parser, and the oo part is merely syntactic sugar.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepgfmodule{oo}
\begin{document}

\pgfooclass{pen}{
    \method pen(){
        %
    }
    \method point(#1,#2,#3){ % position,name,color
        \path(#1)coordinate(#2)node[below right,text=#3]{$#2$};
    }
    \method segment(#1,#2,#3){ % start,end,color
        \draw(#1)circle(.1)(#2)circle(.1);
        \draw[#3](#1)--(#2);
    }
    \method ray(#1,#2,#3){ % start,end,color
        \draw(#1)circle(.1)(#2)circle(.1);
        \draw[#3,overlay,shorten >=-10000](#1)--(#2);
    }
    \method line(#1,#2,#3){ % start,end,color
        \draw(#1)circle(.1)(#2)circle(.1);
        \draw[#3,overlay,shorten <=-10000,shorten >=-10000](#1)--(#2);
    }
}
\pgfoonew \mypen=new pen()
\begin{tikzpicture}
    \draw[lightgray](-5,-5)grid(5,5);
    \draw[->](-6,0)--(6,0);
    \draw[->](0,-6)--(0,6);
    \mypen.point({3,3},A,violet)
    \mypen.point({current bounding box.200},B,olive)
    \mypen.point({0,-4},C,brown)
    \mypen.segment({-5,4},A,red)
    \mypen.ray(B,{40:2},blue)
    \mypen.line({$(A)+(0,-4)$},{$(C)!.5!(B)$},teal)
\end{tikzpicture}

\end{document}

2
  • What I've ended up doing is passing the coordinates, then using a \gettikzxy function to extract the x and y values. It's easier to do that once in the object constructor than every time I instantiate a new object.
    – dmorg
    Commented Dec 4, 2016 at 6:52
  • @dmorg It is good to see you solve the problem by yourself. By the way, I have never seen any PGF/TikZ library that is built over oo. I certainly have no idea, and no preference, how things should be done.
    – Symbol 1
    Commented Dec 4, 2016 at 16:17

You must log in to answer this question.

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