15

This question led to a new package:
pst-intersect

In my experience, there is no macro in PSTricks to get the intersection nodes of two curves. Is it possible to do this in PSTricks? Or Am I am dreaming an impossible thing?

\documentclass[pstricks,border=12pt]{standalone}

\begin{document}
\begin{pspicture}[showgrid](4,3)
    \pscurve[linecolor=red](0,3)(1,1)(2,3)(3,2)(4,0)
    \pscurve[linecolor=blue](0,0)(1,3)(2,1)(3,1)(4,3)
\end{pspicture}
\end{document}

enter image description here

  • Anything that can be done by TikZ can also be done by PSTricks. However, the reverse is not necessarily correct. – kiss my armpit Jun 26 '13 at 19:37
  • 3
    Seems appropriate that you get a tikz solution to your PSTricks question given your history :) – cmhughes Jun 26 '13 at 21:08
  • convert both \pscurve to pathforall and then compare both values. PSTricks has some other examples for pathforall, so you can see how it works. –  Sep 10 '13 at 11:17
  • @CodeMocker Your first statement is incorrect. – Paul Gaborit Feb 07 '14 at 00:43
  • @PaulGaborit: Why? – kiss my armpit Feb 07 '14 at 00:52
  • @CodeMocker Example: PSTricks can not calculate the bounding box of a PostScript graphic and transmit this result to (La)TeX. – Paul Gaborit Feb 07 '14 at 00:57
  • @PaulGaborit: It can be done by multiple compilation theoretically --- the first run saves the intermediate data to a temporary file and it will be updated for the next run until it converges to a stable value --- but I have not done it yet. – kiss my armpit Feb 07 '14 at 01:00
  • @CodeMocker So, your first statement is theoretically correct... but not practically ! (And for your second sentence, it is the opposite!) – Paul Gaborit Feb 07 '14 at 01:04
  • @PaulGaborit: I am trying to prove that my first and second postulates are practically and theoretically correct. :-) – kiss my armpit Feb 07 '14 at 01:56

2 Answers2

15

Here's how this could be done with TikZ using the intersections library:

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
    \draw [red, smooth, name path=first] plot coordinates {(0,3)(1,1)(2,3)(3,2)(4,0)};
    \draw [blue, smooth, name path=second] plot coordinates {(0,0)(1,3)(2,1)(3,1)(4,3)};
    \draw [thick, name intersections={of=first and second, total=\n}]
        \foreach \i in {1,...,\n}{
            (intersection-\i) circle [radius=3pt]
        };
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • 3
    +1: Thank you very much for answering. Unfortunately no bounty for TikZ solution. :-) – kiss my armpit Jun 26 '13 at 19:29
  • 1
    show it with the same coordinates as in the above example ... ;-) –  Jul 06 '13 at 10:31
  • 2
    @Herbert: "Please"... ;-) – Jake Jul 06 '13 at 11:57
  • 1
    Your comment does not get enough attention in this link. – kiss my armpit Jul 06 '13 at 12:03
  • 1
    @Yake: but your curves look fare different. You know, that you have no equivalent function in tikZ for \pscurce... ;-) –  Jul 06 '13 at 12:50
  • 1
    @Herbert: Ah, that's what you mean. Why didn't you say that directly? You may be right that there's no equivalent function in TikZ for \pscurve, I don't know, because I don't know what \pscurve does exactly. That's beside the point of mozartstraße's question though, the question was about finding the intersections of arbitrary paths. – Jake Jul 06 '13 at 13:46
  • 1
    @mozartstraße: I think it would be best if you leave the answer unaccepted. That way, it's more likely for someone to notice that the issue is still unsolved for PSTricks, and maybe they will come up with a solution. Thanks for the bounty, though =) – Jake Jul 09 '13 at 14:44
  • No problem. I am tired of waiting for the PSTricks solutions. :-) – kiss my armpit Jul 09 '13 at 14:48
9

I just released a new PSTricks package pst-intersect, which can calculate the intersections of almost arbitrary paths.

Concerning your curves, you can calculate the intersections as follows:

\documentclass{article}
\usepackage{multido, pst-intersect}
\thispagestyle{empty}
\begin{document}
\begin{pspicture}[showgrid](4,3)
  \pssavepath[linecolor=red]{A}{\pscurve(0,3)(1,1)(2,3)(3,2)(4,0)}
  \pssavepath[linecolor=blue]{B}{\pscurve(0,0)(1,3)(2,1)(3,1)(4,3)}
  \psintersect[showpoints, name=C]{A}{B}
  \pstracecurve[linewidth=2\pslinewidth, istart=2, istop=3]{C}{A}
  \multido{\i=1+1}{3}{\uput[0](C\i){\i}}
\end{pspicture}
\end{document}

With the result

enter image description here

This also shows some other package features: Using the intersection nodes, and retracing parts of saved paths.

In order to get the coordinates of the intersection nodes, you can either use pst-nodes's parameter saveNodeCoors, or the package macro \psGetIsectCenter (both options available since version 0.3). The values can then be shown with \psPrintValue of the pst-tools package:

\documentclass{article}
\usepackage{multido, pst-intersect, pst-tools}
\thispagestyle{empty}
\begin{document}
\begin{pspicture}[showgrid](4,3)
  \pssavepath[linecolor=red]{A}{\pscurve(0,3)(1,1)(2,3)(3,2)(4,0)}
  \pssavepath[linecolor=blue]{B}{\pscurve(0,0)(1,3)(2,1)(3,1)(4,3)}
  \psintersect[showpoints, name=C, saveNodeCoors]{A}{B}
  \pstracecurve[linewidth=2\pslinewidth, istart=2, istop=3]{C}{A}
  \multido{\i=1+1}{3}{\uput[0](C\i){\i}}
\end{pspicture}

\bigskip

\makebox[4em]{X} \makebox[4em]{Y}\\
\multido{\i=1+1}{3}{%
  \makebox[4em]{\psPrintValue{N-C\i.x}}
  \makebox[4em]{\psPrintValue{N-C\i.y}}\\
}%

\makebox[4em]{X} \makebox[4em]{Y}\\
\multido{\i=1+1}{3}{%
  \makebox[4em]{\psPrintValue{\psGetIsectCenter{C}{}{\i} I-C\i.x}}
  \makebox[4em]{\psPrintValue{\psGetIsectCenter{C}{}{\i} I-C\i.y}}\\
}%
\end{document}

enter image description here

Christoph
  • 16,593
  • 1
  • 23
  • 47