# TikZ: Perpendicular line in a plane (3D problem) How to draw a line from the point X perpendicular to AB in the ground plane OAB?

Is there a simple mechanism or do I have to use something like tikz-3d?

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\pgfmathsetmacro{\a}{5}%
\pgfmathsetmacro{\b}{5}%
\begin{tikzpicture}[font=\footnotesize,]

\coordinate[label=left:$O$] (O) at (0,0,0);
\coordinate[label=below:$B$] (B) at (\b,0,0);
\coordinate[label=below:$A$] (A) at (0,0,\a);
\coordinate[label=below:$X$] (X) at ($(A)!0.4!(B)$);

\draw[] (A) -- (B);
\draw[help lines] (O) -- (A);
\draw[help lines] (O) -- (B);

\draw[red] (X) -- ($(X)!1 cm!-90:(A)$);

\begin{scope}[-latex, shift={(-0.5*\a,0.5*\a,0)}]
\foreach \P/\s/\Pos in {(1,0,0)/x/below, (0,1,0)/y/left, (0,0,2)/z/right}
\draw[] (0,0,0) -- \P node[\Pos, pos=0.9,inner sep=2pt]{$\s$};
\end{scope}
\end{tikzpicture}
\end{document}


Let u = cross(OA,OB) and v = (B) - (A), then direction vector of the line that you want is found by cross(u,v). By calculating, we have cross(u,v)= {-a^2 b, 0, -a b^2}. I use {a,0,b} as direction vector of the line. Then I write the equation of the line through X and has direction vector, I get the point Y = (\b/4 -\a,0,3*\a/4-\b) on this line. The line though two points X and Y. I add the projection point H of the point O on the line AB. Note that, XY is parallel to OH.

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\pgfmathsetmacro{\a}{5}%
\pgfmathsetmacro{\b}{5}%
\begin{tikzpicture}[font=\footnotesize,]

\coordinate[label=left:$O$] (O) at (0,0,0);
\coordinate[label=below:$B$] (B) at (\b,0,0);
\coordinate[label=below:$A$] (A) at (0,0,\a);
\coordinate[label=below:$X$] (X) at (\b/4,0,3*\a/4);
\coordinate[label=below:$Y$] (Y) at (\b/4 -\a,0,3*\a/4-\b);
\coordinate[label=below:$H$] (H) at ({\a*\a*\b/(\a*\a+\b*\b)}, 0, {\a*\b*\b/(\a*\a+\b*\b)});
\draw[] (A) -- (B);
\draw[help lines] (O) -- (A);
\draw[help lines] (O) -- (B);
\draw[red] (Y) -- (X) ;
\draw[blue] (O) -- (H) ;
\begin{scope}[-latex, shift={(-0.5*\a,0.5*\a,0)}]
\foreach \P/\s/\Pos in {(1,0,0)/x/below, (0,1,0)/y/left, (0,0,2)/z/right}
\draw[] (0,0,0) -- \P node[\Pos, pos=0.9,inner sep=2pt]{$\s$};
\end{scope}
\end{tikzpicture}
\end{document} In the above code, The coordinates of the point H is found by Maple soft. Based on this answer, you don't need find it.

    \documentclass[tikz,border=1 mm,12pt]{standalone}
\usepackage{fouriernc}
\usetikzlibrary{3dtools}
\tikzset{intersection of line trough/.code args={#1 and #2 with plane
containing #3 and normal #4}{%
\pgfmathsetmacro{\ltest}{abs(TD("#2o#4")-TD("#1o#4"))}%
\ifdim\ltest pt<0.01pt
\message{Plane and line are parallel!^^J}
\pgfmathsetmacro{\myd}{0}
\else
\pgfmathsetmacro{\myd}{(TD("#3o#4")-TD("#1o#4"))/(TD("#2o#4")-TD("#1o#4"))}%
\fi
\pgfmathsetmacro{\myP}{TD("#1+\myd*#2-\myd*#1")}%
\pgfkeysalso{insert path={%
(\myP)
}}
}}

\begin{document}
\pgfmathsetmacro{\a}{5}%
\pgfmathsetmacro{\b}{5}%
\begin{tikzpicture}
\path
(0,0,0) coordinate (O)
(\b,0,0) coordinate (B)
(0,0,\a)  coordinate (A)
({\a*\a*\b/(\a*\a+\b*\b)}, 0, {\a*\b*\b/(\a*\a+\b*\b)}) coordinate (H')
[
3d coordinate={(myn)=(A)-(B)}
];
\path[intersection of line trough={(A) and (B) with plane containing
(O) and normal (myn)}]
coordinate (H);
\foreach \p in {A,B,O,H}
\draw[fill=black] (\p) circle (1.5pt);
\foreach \p/\g in {A/135,B/90,O/180}
\path (\p)+(\g:3mm) node{$\p$};
\draw (A) -- (B) -- (O) -- cycle;
\path[red] foreach \X in {H}
{(\X) node[above] {$\X$} (\X') node[below] {$\X'$}};
\draw (O) -- (H);
\end{tikzpicture}
\end{document} • Very good! :) ;) – cis Dec 13 '19 at 13:34
• Are you aware of the problems of /utils/exec? I see more and more people using it, because it was popularized by tallmarmot, but it doesn't actually work all that well. – Henri Menke Dec 14 '19 at 6:05
• @HenriMenke I copied the code at above link. I don't know it doesn't actually work all that well really. In this case, it works true. Thank you very much. – minhthien_2016 Dec 14 '19 at 6:11