2

I found this answer from the User Caramdir and am actually trying to fit the code to my needs. There's something like this

\newcommand\definePointByXYZ[4]{
    %\coordinate (#1) at (#2,#3,#4);
    \expandafter\gdef\csname tsx@point@#1\endcsname{
        \def\tsx@point@x{#2}
        \def\tsx@point@y{#3}
        \def\tsx@point@z{#4}
    }
}

% Define a plane.
% #1 = name of the plane
% #2*x + #3*y + #4*z = #5 is the equation of the plane
\newcommand*\definePlaneByEquation[5]{
    \expandafter\gdef\csname tsx@plane@#1\endcsname{
        \def\tsx@plane@xcoeff{#2}
        \def\tsx@plane@ycoeff{#3}
        \def\tsx@plane@zcoeff{#4}
        \def\tsx@plane@scalar{#5}
    }
}

% Project a point to a plane.
% #1 = name of the new point
% #2 = name of old point
% #3 = name of plane
\newcommand\projectPointToPlane[3]{{
    \csname tsx@point@#2\endcsname
    \csname tsx@plane@#3\endcsname
% ...
}}

In the command projectPointToPlane the arguments are two points that get defined by \definePointByXYZ and then it's possible to calculate anything, for example:

\pgfmathparse{\tsx@plane@xcoeff*\tsx@plane@xcoeff + \tsx@plane@ycoeff*\tsx@plane@ycoeff + \tsx@plane@zcoeff*\tsx@plane@zcoeff}
    \let\nnormsq\pgfmathresult

Now I'd like to create something like this

% #1 = Name of the object
% #2 = Point A
% #2 = Point B
\newcommand*\mynewcommand[3]{
   \csname tsx@point@#2\endcsname
   \csname tsx@point@#3\endcsname
   % Calculate something
   \pgfmathsetmacro{\myvariable}{%any expression}
}

My Question: How can I calculate with the coordinates of two Points? Is it even possible? My first try was to rename one line to \csname tsx@pointtwo@#3\endcsname and then

\pgfmathsetmacro{\myvariable}{\tsx@point@x *\tsxpointtwo@x} 

but that didn't work. I'm new to TeX-Programming, so any help is really appreciated. :)

joni
  • 219
  • It's not very clear what "Point A" and "Point B" are. Are they coordinate names? If so you need to extract the x y values from them using \newdim\mypointx \pgfextractx\mypointx{\pgfpointachor{PointA}{center}}, the same for y, then you can use \mypointx and \mypointy in your calculations. If "Point A" and B are something else, then what are they exactly? – Guilherme Zanotelli Jan 18 '17 at 16:49
  • "Point A" and "Point B" are two Points that get defined by \definePointByXYZ. For example: \definePointByXYZ{A}{1}{2}{3} and \definePointByXYZ{B}{1}{4}{2}defines points named A and B with the coordinates x=1,y=2,z=3 and x=1,y=4,z=2. Then I can use \mynewcommand{...}{A}{B}. Now I want to calculate with the coordinates of the both points inside my \mynewcommand similar to \projectPlaneToPoint from the linked answer. – joni Jan 19 '17 at 09:01

1 Answers1

2

Your problem is that \csname tsx@point@#1 \endcsname defines all \tsx@point@x, y and z, once you call \csname tsx@point@#2 \endcsname it defines them again overwriting the previous values, so you're left with only the coordinates of the last issued tsx@point@#1.

One way to get around this is to \let\first@point@x\tsx@point@x and so on for y and z before calling the other \csname tsx@point@#2\endcsname, then you can access the first declared point through the \first@point@<x,y,z> holder.

MWE

% #1 = Name of the object
% #2 = Point A
% #2 = Point B
\newcommand*\mynewcommand[3]{
   \csname tsx@point@#2\endcsname
   \let\first@point@x\tsx@point@x
   \let\first@point@y\tsx@point@y
   \let\first@point@z\tsx@point@z
   \csname tsx@point@#3\endcsname
   % Calculate something
   \pgfmathsetmacro{\sumofpointsx}{\tsx@point@x+\first@point@x}
}

EDIT

You could so this without using custom commands to define the points. PGF has the \pgfextract<x,y,z> command, so if you say \coordinate (A) at (1,2,3) you can access these values through:

\newdim\mypointx\pgfextractx{\mypointx}{\pgfpointanchor{A}{center}}
\newdim\mypointy\pgfextracty{\mypointy}{\pgfpointanchor{A}{center}}
\newdim\mypointz\pgfextractz{\mypointz}{\pgfpointanchor{A}{center}}

MWE using \pgfextract<x,y,z>

\newdim\lastx\newdim\lasty\newdim\lastz
\newcommand*{\extracXYZ}[1]{%
    \pgfextractx{\lastx}{\pgfpointanchor{#1}{center}}
    \pgfextracty{\lasty}{\pgfpointanchor{#1}{center}}
    \pgfextractz{\lastz}{\pgfpointanchor{#1}{center}}
}

...

\coordinate (A) at (1,2,3);
\coordinate (B) at (3,2,1);
\newcommand*\mynewcommand[2]{
   \ectractXYZ{#1}
   \let\firstx\lastx
   \let\firsty\lasty
   \let\firstz\lastz
   \ectractXYZ{#2}
   % Calculate something
   \pgfmathsetmacro{\sumofpointsx}{\firstx+\lastx}
}