# Problem with the order of intersections using 3d tikz library

I would like to obtain intersections between two paths, in a specific order.

When these paths are drawn as a 2D picture, everything is fine.

But for some reason, when I plot the same figure using the 3d library the order of intersections changes, despite the sort by key.

Anyone knows how to get correct order in the 3D case?

MWE:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{3d, intersections}

\begin{document}

% 2D picture : order of intersections is OK
\begin{tikzpicture}
\draw[red, name path = structure]   (3,-1)coordinate(A) -- (3,1) -- (1,1)coordinate(B) --  (0,0) -- (2,0) -- (3,1);
\draw[blue,->, name path = line] (A) -- (B);
\draw [name intersections={of=structure and line, name=i, sort by=line}] (i-1) node{+}node[below]{1} (i-2) node{+}node[below]{2} (i-3) node{+}node[below]{3};
\end{tikzpicture}

% 3D picture : order of intersections is weird
\begin{tikzpicture}[x  = {(1cm,0cm)},  y  = {(0.5cm,0.5cm)},   z  = {(0cm,1cm)}]
\draw[red, name path = structure] (2,2,0)coordinate(A) -- ++(0,0,2) --++(-2,0,0)coordinate(B) --++(0,-2,0) --++(2,0,0) --++(0,2,0);
\draw[blue,->, name path = line] (A) -- (B);
\draw [name intersections={of=structure and line, name=i, sort by=line}] (i-1) node{+}node[below]{1} (i-2) node{+}node[below]{2} (i-3) node{+}node[below]{3};
\end{tikzpicture}

\end{document}


2D picture :

3D picture :

IMHO this has nothing to do with 3d but with the fact that sorting along straight lines that are drawn with the -- syntax the outcome is a bit of a lottery. This has been discussed at length under this question, and to solve it you can draw a straight line that pretends to be a curve.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{3d, intersections}

\begin{document}

% 2D picture : order of intersections is OK
\begin{tikzpicture}
\draw[red, name path = structure]   (3,-1)coordinate(A) -- (3,1) -- (1,1)coordinate(B) --  (0,0) -- (2,0) -- (3,1);
\draw[blue,->, name path = line] (A) -- (B);
\draw [name intersections={of=structure and line, name=i, sort by=line}] (i-1) node{+}node[below]{1} (i-2) node{+}node[below]{2} (i-3) node{+}node[below]{3};
\end{tikzpicture}

% 3D picture : order of intersections is weird
\begin{tikzpicture}[x  = {(1cm,0cm)},  y  = {(0.5cm,0.5cm)},   z  = {(0cm,1cm)}]
\draw[red, name path = structure] (2,2,0)coordinate(A) -- ++(0,0,2) --++(-2,0,0)coordinate(B) --++(0,-2,0) --++(2,0,0) --++(0,2,0);
\draw[blue,->, name path = line] (A) to[bend left=0] (B);
\draw [name intersections={of=structure and line, name=i, sort by=line}] (i-1) node{+}node[below]{1} (i-2) node{+}node[below]{2} (i-3) node{+}node[below]{3};
\end{tikzpicture}

\end{document}


• Thanks for the tip but it seem your link is pointing to a single question. Could you correct it please? Dec 30 '18 at 10:28
• @Tobard Sorry! Done.
– user121799
Dec 30 '18 at 14:28