# How to get the intersection nodes of 2 arbitrary curves?

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}


-
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
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. – Herbert Sep 10 '13 at 11:17
@CodeMocker Your first statement is incorrect. – Paul Gaborit Feb 7 '14 at 0:43
@PaulGaborit: Why? – kiss my armpit Feb 7 '14 at 0:52

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

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}


-
Can we refer to each intersection point by name for later use such as putting a label at that point? – kiss my armpit Feb 7 '14 at 0:54
I updated the answer to show some features and the new syntax for the newly released package (will appear soon on CTAN). – Christoph Feb 18 '14 at 23:28
Very nice additional features. Thanks. – kiss my armpit Feb 18 '14 at 23:57
Is it possible to get the coordinates of the intersection points? – Svend Tveskæg Feb 21 '14 at 12:35
@SvendTveskæg All the coordinates are stored on the Postscript level in an array Points inside the dictionary PIT@<name> that belongs to the intersection. For the above example, you can see it e.g. by putting \makeatletter\PIT@Verb{ PIT@C /Points get == }\makeatother somewhere after the \psintersect call. – Christoph Feb 21 '14 at 15:37

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

\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}{

@Herbert: "Please"... ;-) – Jake Jul 6 '13 at 11:57
@Yake: but your curves look fare different. You know, that you have no equivalent function in tikZ for \pscurce... ;-) – Herbert Jul 6 '13 at 12:50