# How to find the intersection point between line and plane in tikz-3dplot?

It is interesting that there is no question in this site about the intersection between line and plane. In 3-space, a line is passing through a plane. Finding intersection in 3-space is a very important problem but seems to be difficult in tikz-3dplot. I have two questions: 1) how to find the intersecting point and 2) how to hide the line behind the plane?

\tdplotsetmaincoords{45}{10}
\begin{tikzpicture}[scale=1,tdplot_main_coords,>=stealth',font=\scriptsize]

%plane
\draw[fill=gray!40,opacity=.8] (.5,-1.5,0) -- (4,-1.5,0) -- (4,1.5,0) -- (.5,1.5,0) -- cycle;

%points
\coordinate (or) at (2,1.5,-4);
\tdplotsetcoord{p}{5}{10}{0};
\tdplotsetcoord{oo}{0}{0}{0};

%line
\draw (or) -- (p);

%axis
\draw[->] (oo) -- +(.5,0,0) node[right]{$x$};
\draw[->] (oo) -- +(0,.5,0) node[right]{$y$};
\draw[->] (oo) -- +(0,0,.5) node[left]{$z$};
\end{tikzpicture}

-

your plane is in the x-y plane so it is pretty simple to find the point in this plane. Run the example with xelatex

\documentclass{standalone}
\usepackage{pst-solides3d}
\begin{document}
\psset{viewpoint=20 20 20 rtp2xyz,lightsrc=10 15 7,Decran=20}
\begin{pspicture}(-5,-5)(6,5)
\psSolid[object=line,args=4 3 -4 3 1 0]
\psSolid[object=new,
sommets=0.5 -2.5 0
4   -2.5 0
4    4.5 0
0.5  4.5 0,
faces={[0 1 2 3]},fillcolor=red!40]
\psSolid[object=line,args=2 -1 4 3 1 0]
\psSolid[object=point,args= 3 1 0]
\axesIIID(4,4,3)
\end{pspicture}
\end{document}


-
Amazing! This method has even the perspective and lighring effects. All the troubles with tikz-3dplot seem to be unnecessary. Thanks Herbert. – gnoejh Aug 17 '12 at 17:49

My solution is not elegant as the Herbert one, but it is given using LuaLaTeX.

\documentclass{standalone}

\usepackage{tikz,tikz-3dplot,luacode}

\def\prPoint#1{\directlua{%
for i,v in ipairs(#1) do
tex.sprint(v)
if i\string~=3 then tex.sprint(",") end
end
}}

\begin{document}
\tdplotsetmaincoords{45}{10}
\begin{tikzpicture}[scale=1,tdplot_main_coords,>=stealth',font=\scriptsize]

\begin{luacode}
-----------
-- PLANE --
-----------
A         = { .5,-1.5,0}
B         = {4  ,-1.5,0}
C         = {4  , 1.5,0}
AB        = {B[1]-A[1],B[2]-A[2],B[3]-A[3]}
AC        = {C[1]-A[1],C[2]-A[2],C[3]-A[3]}
plane_dir = {AB[2]*AC[3]-AB[3]*AC[2],
AB[3]*AC[1]-AB[1]*AC[3],
AB[1]*AC[2]-AB[2]*AC[1]}
local mod = math.sqrt(plane_dir[1]^2+plane_dir[2]^2+plane_dir[3]^2)
plane_dir = {plane_dir[1]/mod,plane_dir[2]/mod,plane_dir[3]/mod}
plane_d   = -A[1]*(AB[2]*AC[3]-AB[3]*AC[2])+
A[2]*(AB[1]*AC[3]-AB[3]*AC[1])-
A[3]*(AB[1]*AC[2]-AB[2]*AC[1])

function plane(x,y)
return -(plane_dir[1]*x+plane_dir[2]*y+plane_d)/(plane_dir[3])
end

----------
-- LINE --
----------
D         = {2, 1.5,-4}
E         = {2,.3  , 1}
line_dir  = {D[1]-E[1],D[2]-E[2],D[3]-E[3]}
local mod = math.sqrt(line_dir[1]^2+line_dir[2]^2+line_dir[3]^2)
line_dir  = {line_dir[1]/mod,line_dir[2]/mod,line_dir[3]/mod}

function line(t)
return {E[1]+line_dir[1]*t,E[2]+line_dir[2]*t,E[3]+line_dir[3]*t}
end

------------------------
-- INTERSECTION POINT --
------------------------
t         = -(plane_d+(plane_dir[1]*E[1]+plane_dir[2]*E[2]+plane_dir[3]*E[3]))/
(plane_dir[1]*line_dir[1]+plane_dir[2]*line_dir[2]+plane_dir[3]*line_dir[3])
int_point = line(t)
\end{luacode}

% Plane
\draw[fill=gray!80!](\prPoint{A})--
(\prPoint{B})--
(\prPoint{C})--
(\prPoint{{.5, 1.5,plane(.5,1.5)}})--
cycle;

% Points
\coordinate (or) at (2,1.5,-4);
\tdplotsetcoord{p}{5}{10}{0};
\tdplotsetcoord{oo}{0}{0}{0};

% Line
\draw      (\prPoint{D})--(\prPoint{int_point});
\draw[red] (\prPoint{int_point})--(\prPoint{E});

% Axis
\draw[->] (oo) -- +(.5,0,0) node[right]{$x$};
\draw[->] (oo) -- +(0,.5,0) node[right]{$y$};
\draw[->] (oo) -- +(0,0,.5) node[left]{$z$};
\end{tikzpicture}
\end{document}


Have fun!!

-