pgf-tikz / pgf

A Portable Graphic Format for TeX
https://pgf-tikz.github.io/
1.14k stars 108 forks source link

The time index of intersection point of line and line. #732

Open QJLc opened 5 years ago

QJLc commented 5 years ago

The lines

https://github.com/pgf-tikz/pgf/blob/5e2f4a884cc0886fbce3f063f5b6705dfff37a89/tex/generic/pgf/libraries/pgflibraryintersections.code.tex#L357-L370

are used to define \pgf@intersect@time@a. I think that, the \pgf@x and \pgf@y in lines 364 and 365, could be coordinates of current intersection point, but their value may be modified by \pgf@ifsolution@duplicate in line 357.

The line 366 gets the vector "start--end", stored in \pgf@x and \pgf@y, the line 368 gets the length of "start--end", so, the line 368 could be modified.

A possible modification starting from line 363 would be:

            \ifpgf@intersect@sort%
                \pgf@intersect@solution@candidate%   <-- add this line
                \pgf@xc=\pgf@x%
                \pgf@yc=\pgf@y%
                \pgf@process{\pgfpointdiff{\pgfpoint@intersect@start@a}{\pgfpoint@intersect@end@a}}%
                \edef\pgf@marshal{%
                    \noexpand\pgfmathveclen@{\pgfmath@tonumber{\pgf@x}}{\pgfmath@tonumber{\pgf@y}}%  <-- this line is modified
                }%
               \pgf@marshal%

I tried this:

\begin{pgfpicture}
  \pgfintersectionsortbyfirstpath
  \pgfintersectionofpaths
  {
    \pgfpathmoveto{\pgfpoint{1cm}{0cm}}
    \pgfpathlineto{\pgfpoint{1cm}{3cm}}
    \pgfpathlineto{\pgfpoint{2cm}{3cm}}
    \pgfpathlineto{\pgfpoint{2cm}{0cm}}
    \pgfgetpath\temppath
    \pgfusepath{stroke}
    \pgfsetpath\temppath
  }{
    \pgfpathmoveto{\pgfpoint{0cm}{2cm}}
    \pgfpathlineto{\pgfpoint{3cm}{2cm}}
    \pgfpathlineto{\pgfpoint{3cm}{1cm}}
    \pgfpathlineto{\pgfpoint{0cm}{1cm}}
    \pgfgetpath\temppath
    \pgfusepath{stroke}
    \pgfsetpath\temppath
  }
  \foreach \s in {1,...,\pgfintersectionsolutions}
  {\pgfpathcircle{\pgfpointintersectionsolution{\s}}{2pt}}
  \pgfusepath{stroke}
\end{pgfpicture}

\def\showrecord#1#2#3#4{%
#1;#2;#3;#4;%
}
\makeatletter
\edef\pointA{\csname pgfpoint@g@intersect@solution@1@props\endcsname}
\edef\pointB{\csname pgfpoint@g@intersect@solution@2@props\endcsname}
\edef\pointC{\csname pgfpoint@g@intersect@solution@3@props\endcsname}
\edef\pointD{\csname pgfpoint@g@intersect@solution@4@props\endcsname}
\expandafter\showrecord\pointA\relax%
\expandafter\showrecord\pointB\relax%
\expandafter\showrecord\pointC\relax%
\expandafter\showrecord\pointD\relax%
\makeatother

And output numbers are:

0;0;0.6667;;
0;2;0.33336;;
2;0;2.33336;;
2;2;2.6667;;

These may be expected.

hmenke commented 5 years ago

Could you please

  1. give this issue a meaningful title
  2. explain what you are trying to do
QJLc commented 5 years ago

Thanks. I have improved it.

ghost commented 5 years ago

The pgfplots library has such a functionality in the sense that it allows one to decompose the path into intersection segments. There is also \pgfintersectiongetsolutiontime, but it does not give the time in the sense of a percentage at which the intersection is located. See also https://tex.stackexchange.com/q/432532.