5

Given two (A) and (B) 2-D points in tikzpicture environment, I need to collect both angle of the line (A)-(B) with respect to the vector(1,0) and the half of its distance in two variables, say \Aab and \Dab respectively. SOLVED (see Coordinates A, B: compute |B-A| and angle between +x and (B-A))

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{calc}
\makeatletter
\newcommand{\getLengthAndAngle}[2]{%
\pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}
{\pgfpointanchor{#2}{center}}
\global\let\myangle\pgfmathresult % we need a global macro
\pgfpointdiff{\pgfpointanchor{#1}{center}}
{\pgfpointanchor{#2}{center}}
\pgf@xa=\pgf@x % no need to use a new dimen
\pgf@ya=\pgf@y
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)/28.45274} % to convert from pt 
 to cm
\global\let\mylength\pgfmathresult % we need a global macro}
\makeatother 

\begin{document}
    \begin{tikzpicture}
    \clip (0,4) rectangle (7,-5);
    \coordinate (A) at (1,1);
    \coordinate (B) at (3,4);
    \getLengthAndAngle{A}{B}    
    \draw[help lines,gray] (0,-3) grid (5,5);
    \begin{scope}[blue, thick]  
    \draw (A) -- (B)--+(\mylength,0); 
    \draw[rotate around={-\myangle:(A)}] (A)--+(\mylength,0);
    \end{scope}
    \draw (B) circle (\mylength cm);
    \end{tikzpicture} 
\end{document}
JOM
  • 457
  • 3
    Did you try something? – Sebastiano Nov 14 '18 at 19:54
  • look angle library. – Zarko Nov 14 '18 at 20:06
  • 2
    Related: https://tex.stackexchange.com/questions/39293/coordinates-a-b-compute-b-a-and-angle-between-x-and-b-a?rq=1 – Torbjørn T. Nov 14 '18 at 20:14
  • @Zarko If you mean the angles library that is as you know for drawing an angle symbol between two lines, the OP wants to calculate an angle and a distance. – Torbjørn T. Nov 14 '18 at 20:16
  • 2
    It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that at least sets up the problem.

    While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. This will also serve as a test case and ensure that the solution actually works for you. As it is it is not clear what you mean by "collect".

    – Peter Grill Nov 14 '18 at 20:23
  • @TorbjørnT., yes i mean angles. it seems that i misunderstood the question. – Zarko Nov 14 '18 at 20:33
  • \pgfmathanglebetweenpoints{\pgfpoint{1cm}{3cm}}{\pgfpoint{2cm}{4cm}} \pgfmathresult has the inconvenient that I don't know how calculate the coordinates x and y of the two given points... and \pgfmathresult must rapidly used, or it changes – JOM Nov 14 '18 at 20:39
  • and veclen in the pgfmanual is trated as let \p1 = ($ (B) - (A) $) in.... and I am looking for allocate the result for a posterior usage. – JOM Nov 14 '18 at 20:41
  • Yes @TorbjørnT.... I didn't found it . Thank you. and thak you gays, I solved with that help. – JOM Nov 14 '18 at 21:07
  • If you move the } after we need a global macro to a new line, your code compiles. And you are loading calc, so I do not inevitably see a reason for all the gymnastics. You could just do \drawlet \p1=($(B)-(A)$), \n1={atan2(\y1,\x1)}, \n2={veclen(\y1,\x1)} in [rotate around={-\n1:(A)}] (A)--+(\n2,0); to have angle and the length in \n1 and \n2, respectively. That is, this line reproduces your result without the need of these macros. –  Nov 14 '18 at 21:47
  • Hi JOM. Please undelete this question. Your question is answered in comments, so it is closed. That is how this site works. No worries, your question is not bad, and it is useful for future visitors. It is just like "closed issues on GitHub". No one is disturbed by closed questions - we are only disturbed by spam questions, and your question is not a spam. Please undelete the question. –  May 17 '19 at 06:35

1 Answers1

3

Here is an alternative solely based on calc, i.e. not using extra macros, and with a little helper that allows you to "export" the length outside the path (and scope).

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}[globalize/.code n args={2}{\xdef#2{#1}}]
    \clip (0,4) rectangle (7,-5);
    \coordinate (A) at (1,1);
    \coordinate (B) at (3,4);
    \draw[help lines,gray] (0,-3) grid (5,5);
    \begin{scope}[blue, thick]  
    \draw let \p1=($(B)-(A)$), \n1={veclen(\y1,\x1)}, \n2={veclen(\y1,\x1)}
     in [globalize={\n2}{\mylength}] (A) -- (B)--+(\n2,0)
     [rotate around={-\n1:(A)}] (A)--+(\n2,0);
    \end{scope}
    \draw (B) circle (\mylength);
    \end{tikzpicture} 
\end{document}

enter image description here

  • \n1={atan2(\y1,\x1)}. Can we globalize both variables... and no drawing at all? – JOM Nov 14 '18 at 22:31
  • 1
    @JOM Sure: \path let \p1=($(B)-(A)$), \n1={veclen(\y1,\x1)}, \n2={veclen(\y1,\x1)} in [globalize={\n2}{\mylength},globalize={\n1}{\myangle}];. Just watch out for globalization critics! ;-) –  Nov 15 '18 at 00:03