4

Sometimes, I need determine coordinates of projection of a point on a line in 3D. I know The Syntax of Projection in 2D at 13.5.5 The Syntax of Projection Modifiers at Manual for Version 3.1. This syntex is not correct in 3D. In my example. In triangle, SA=AB, then projection must be midpoint of segment SB, point E. With syntax coordinate (J) at ($(B)!(A)!(S)$) is not correct. Is there a command to find coordinates of projection of a point on a line in 3D?

My code

\documentclass[border=3mm,12pt]{standalone}
\usepackage{fouriernc}
\usepackage{tikz,tikz-3dplot} 
\usepackage{tkz-euclide}
\usetkzobj{all}
  \begin{document}
 \tdplotsetmaincoords{70}{110}
  %\tdplotsetmaincoords{80}{100}
 \begin{tikzpicture}[tdplot_main_coords,scale=1.5]
  \pgfmathsetmacro\a{4}
 \pgfmathsetmacro\b{3}
 \pgfmathsetmacro\h{4}

 % definitions
 \path
 coordinate(A) at (0,0,0)
coordinate (B) at (\a,0,0)
coordinate (C) at (0,\b,0)                           
coordinate (S) at (0,0,\a)                
coordinate (E) at ($(B)!0.5!(S)$)
coordinate (J) at ($(B)!(A)!(S)$);
 \draw[dashed,thick]
       (A) -- (B)  (A) -- (C)  (A) -- (E)  (S)--(A)  ;
       \draw[thick]
       (S) -- (B) -- (C) -- cycle;
  \draw[red, thick, dashed]  (A)-- ($(B)!(A)!(S)$);
 \foreach \point/\position in {A/left,B/left,C/below,S/above,E/left,J/left}
 {
   \fill (\point) circle (.8pt);
   \node[\position=3pt] at (\point) {$\point$};
 }
 \end{tikzpicture}
     \end{document} 

enter image description here

1 Answers1

5

TikZ does not store the 3d components of coordinates AFAIK, so the best I can offer is a style that computes the projection for points for which you specify the 3d coordinates explicitly. This is what the line

\path[projection of point={(0,0,0) on line through (\a,0,0) and (0,0,\a)}]
 coordinate[label=above left:$P$] (P)
 [projection of point={(0,0,0) on line through (0,\b,0) and (0,0,\a)}]
 coordinate[label=above right:$Q$] (Q)
 [projection of point={(0,0,0) on line through (0,\b,0) and (\a,0,0)}]
 coordinate[label=below:$R$] (R);

does in

\documentclass[border=3mm,12pt,tikz]{standalone}
\usepackage{fouriernc}
\usepackage{tikz,tikz-3dplot} 
\tikzset{projection of point/.style args={(#1,#2,#3) on line through (#4,#5,#6)
and (#7,#8,#9)}{%
/utils/exec=\pgfmathsetmacro{\myprefactor}{((#1-#4)*(#7-#4)+(#2-#5)*(#8-#5)+(#3-#6)*(#9-#6))/((#7-#4)*(#7-#4)+(#8-#5)*(#8-#5)+(#9-#6)*(#9-#6))},
insert path={%
({#4+\myprefactor*(#7-#4)},{#5+\myprefactor*(#8-#5)},{#6+\myprefactor*(#9-#6)})}
}}
  \begin{document}
 \tdplotsetmaincoords{70}{110}
  %\tdplotsetmaincoords{80}{100}
 \begin{tikzpicture}[tdplot_main_coords,scale=1.5]
  \pgfmathsetmacro\a{4}
 \pgfmathsetmacro\b{3}
 \pgfmathsetmacro\h{4}

 % definitions
 \path
 coordinate(A) at (0,0,0)
coordinate (B) at (\a,0,0)
coordinate (C) at (0,\b,0)                           
coordinate (S) at (0,0,\a)                
%coordinate (E) at ($(B)!0.5!(S)$)
coordinate (J) at ($(B)!(A)!(S)$);
 \path[projection of point={(0,0,0) on line through (\a,0,0) and (0,0,\a)}]
 coordinate[label=above left:$P$] (P)
 [projection of point={(0,0,0) on line through (0,\b,0) and (0,0,\a)}]
 coordinate[label=above right:$Q$] (Q)
 [projection of point={(0,0,0) on line through (0,\b,0) and (\a,0,0)}]
 coordinate[label=below:$R$] (R);
 \draw[dashed,thick]
       (A) -- (B)  (A) -- (C)  (S)--(A)  ;
       \draw[thick]
       (S) -- (B) -- (C) -- cycle;
  %\draw[red, thick, dashed]  (A)-- ($(B)!(A)!(S)$);
  \draw[red, thick, dashed]  (A)-- (P) (A)-- (Q) (A)-- (R);
 \foreach \point/\position in {A/left,B/left,C/below,S/above,J/left}
 {
   \fill (\point) circle (.8pt);
   \node[\position=3pt] at (\point) {$\point$};
 }
 \end{tikzpicture}
\end{document} 

enter image description here

An animation to illustrate this.

\documentclass[border=3mm,12pt,tikz]{standalone}
\usepackage{fouriernc}
\usepackage{tikz-3dplot} 
\tikzset{projection of point/.style args={(#1,#2,#3) on line through (#4,#5,#6)
and (#7,#8,#9)}{%
/utils/exec=\pgfmathsetmacro{\myprefactor}{((#1-#4)*(#7-#4)+(#2-#5)*(#8-#5)+(#3-#6)*(#9-#6))/((#7-#4)*(#7-#4)+(#8-#5)*(#8-#5)+(#9-#6)*(#9-#6))},
insert path={%
({#4+\myprefactor*(#7-#4)},{#5+\myprefactor*(#8-#5)},{#6+\myprefactor*(#9-#6)})}
}}

  \begin{document}
\foreach \X in {5,15,...,355}  
{\tdplotsetmaincoords{70}{\X}
  %\tdplotsetmaincoords{80}{100}
 \begin{tikzpicture}[tdplot_main_coords,scale=1.5]
 \path[tdplot_screen_coords,use as bounding box] (-5,-2) rectangle (5,5);
 \pgfmathsetmacro\a{4}
 \pgfmathsetmacro\b{3}
 \pgfmathsetmacro\h{4}

 % definitions
 \path
 coordinate(A) at (0,0,0)
coordinate (B) at (\a,0,0)
coordinate (C) at (0,\b,0)                           
coordinate (S) at (0,0,\a)                
%coordinate (E) at ($(B)!0.5!(S)$)
coordinate (J) at ($(B)!(A)!(S)$);
 \path[projection of point={(0,0,0) on line through (\a,0,0) and (0,0,\a)}]
 coordinate[label=above left:$P$] (P)
 [projection of point={(0,0,0) on line through (0,\b,0) and (0,0,\a)}]
 coordinate[label=above right:$Q$] (Q)
 [projection of point={(0,0,0) on line through (0,\b,0) and (\a,0,0)}]
 coordinate[label=below:$R$] (R);
 \draw[dashed,thick]
       (A) -- (B)  (A) -- (C)  (S)--(A)  ;
       \draw[thick]
       (S) -- (B) -- (C) -- cycle;
  %\draw[red, thick, dashed]  (A)-- ($(B)!(A)!(S)$);
  \draw[red, thick, dashed]  (A)-- (P) (A)-- (Q) (A)-- (R);
 \foreach \point/\position in {A/left,B/left,C/below,S/above,J/left}
 {
   \fill (\point) circle (.8pt);
   \node[\position=3pt] at (\point) {$\point$};
 }
 \end{tikzpicture}}
\end{document} 

enter image description here

  • Can I post a new question about projection of a point on a plane which passing thourgh points?
  • I am using Sumatra. How can I view anamination?
  • – minhthien_2016 Jan 20 '19 at 06:06
  • @minhthien_2016 You can always ask a new question. ;-) (But please ask a new official question because I think both of them are sufficiently distinct and interesting.) And I do not know what Sumatra does. The second picture is an animated gif. If you really cannot view it with the standard viewers on your system, you could just compile the example. This will give you a multipage pdf. This very nice answer discusses how to convert it to mp4. –  Jan 20 '19 at 06:11
  • I almost don't understand your lines \tikzset{projection of point/.style args={(#1,#2,#3) on line through (#4,#5,#6) and (#7,#8,#9)}{% /utils/exec=\pgfmathsetmacro{\myprefactor}{((#1-#4)*(#7-#4)+(#2-#5)*(#8-#5)+(#3-#6)*(#9-#6))/((#7-#4)*(#7-#4)+(#8-#5)*(#8-#5)+(#9-#6)*(#9-#6))}, insert path={% ({#4+\myprefactor*(#7-#4)},{#5+\myprefactor*(#8-#5)},{#6+\myprefactor*(#9-#6)})} }}

    What's new in edited?

    – minhthien_2016 Jan 20 '19 at 07:00
  • @minhthien_2016 The new thing is /utils/exec=, which is described on p. 964 of the pgfmanual, and which allows me to compute the prefactor and using it without making it global. So the code is a tiny bit more efficient. –  Jan 20 '19 at 07:04
  • @minhthien_2016 Could you please tell me what your question is? (Did you mean \coordinate (H) at ({\b^2*\c^2*\a/((\b^2+\c^2)*\a^2+\b^2*\c^2)},{\b*\c^2*\a^2/((\b^2+\c^2)*\a^2+\b^2*\c^2)},{\b^2*\c*\a^2/((\b^2+\c^2)*\a^2+\b^2*\c^2)});? –  Jan 21 '19 at 05:10
  • I have just post a new equation at here https://tex.stackexchange.com/questions/471091/is-there-a-command-to-find-coordinates-of-projection-of-a-point-on-a-plane – minhthien_2016 Jan 21 '19 at 07:17
  • With multipage pdf, how can I note at each figure a view, e.g, \tdplotsetmaincoords{70}{10}, \tdplotsetmaincoords{70}{15}, ...? – minhthien_2016 Feb 06 '19 at 04:16
  • @minhthien_2016 Just put \node[anchor=north east] at (current bounding box.north east) {$\theta=\tdplotmaintheta^\circ,\phi=\tdplotmainphi^\circ$}; before \end{tikzpicture}. –  Feb 06 '19 at 04:20