2

I would like to plot a normal bounded line to a surface in a point.

The surface is y=x and the point is (1/2,1/2,1). The gradient is (-1,1,0), and at point (1/2,1/2,1) is still the same, thus the normal line would be (x-1/2)/(-1)=(y-1/2)/(1)=(z-1)/(0), hence the line is described by (.5,.5,1)+t(-1,1,0).

However, I want the line to be bounded, in order to make a unitary director vector, but I am not able to do these. I am using t between 0 and 1, but these draw a long line; I want it to be a vector of length 1.

I am not sure if this math is correct. If I find the unitary vector of (-1,1,0) it is (-1,1,0)/(sqrt(2)) i.e. (-sqrt(2)/2,sqrt(2)/2,0) then I still do not have a vector of length 1 (when t is between 0 and 1).

Here is my MWE:

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis} [
                title={Without normalizing $(-1,1,0)$ ($t\in[0,1]$)},
                axis on top,
                axis lines=center,
                xlabel=$x$,
                ylabel=$y$,
                zlabel=$z$,
                ticklabel style={font=\tiny},
                view={115}{25}
            ]
            \addplot3[opacity=.5,surf,samples=21,variable=\t,variable y=\s,domain=0:2,y domain=0:90,z buffer=sort,colormap={red}{color=(red) color=(red)}] ({\t*cos(\s)*(sqrt(2)*.5)},{\t*cos(\s)*(sqrt(2)*.5)},{\t*sin(\s)});%Red S
            \addplot3[-stealth,variable=\t,domain=0:1] ({1/2+t},{1/2-t},{1});
        \end{axis}
    \end{tikzpicture}
    \hfill
    \begin{tikzpicture}
        \begin{axis} [
                title={Normalizing $(-1,1,0)$ ($t\in[0,1]$)},
                axis on top,
                axis lines=center,
                xlabel=$x$,
                ylabel=$y$,
                zlabel=$z$,
                ticklabel style={font=\tiny},
                view={115}{25}
            ]
            \addplot3[opacity=.5,surf,samples=21,variable=\t,variable y=\s,domain=0:2,y domain=0:90,z buffer=sort,colormap={red}{color=(red) color=(red)}] ({\t*cos(\s)*(sqrt(2)*.5)},{\t*cos(\s)*(sqrt(2)*.5)},{\t*sin(\s)});%Red S
            \addplot3[-stealth,variable=\t,domain=0:1] ({1/2+(sqrt(2)/2)*t},{1/2-(sqrt(2)/2)*t},{1});
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}

MWE

Even the line has a bad quality, it looks pixelated:

Pixelated line

Does anyone know how to produce a vector of length 1 or what am I missing?

Some links of interest:

Thanks!!

manooooh
  • 3,203
  • 2
  • 21
  • 46

1 Answers1

1

I hope I am not missing anything obvious. You only need to divide by sqrt(2) in your left example to arrive at

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis} [
                title={Normalizing},
                axis on top,
                axis lines=center,
                xlabel=$x$,
                ylabel=$y$,
                zlabel=$z$,
                ticklabel style={font=\tiny},
                view={115}{25}
            ]
            \addplot3[opacity=.5,surf,samples=21,variable=\t,variable y=\s,domain=0:2,y domain=0:90,z buffer=sort,colormap={red}{color=(red) color=(red)}] ({\t*cos(\s)*(sqrt(2)*.5)},{\t*cos(\s)*(sqrt(2)*.5)},{\t*sin(\s)});%Red S            
            \pgfmathsetmacro{\msq}{sqrt(1/2)}
            \addplot3[-stealth,variable=\t,samples at={0,\msq},samples y=0] ({1/2+t},{1/2-t},{1});
        \end{axis}
    \end{tikzpicture}
\end{center}
\end{document}

enter image description here

The "pixelation" went away after adding samples y=0.

  • Thanks for the trick of samples y=0! I already divided by sqrt(2) in the right side, but I use 1/sqrt(2)=sqrt(2)/2 (I rationalized). Use view{115}{90} to see the line better, I do not think that the line has length 1 :(. – manooooh Feb 26 '19 at 19:01
  • 1
    @manooooh It has the length 1 in the units used in the plot. These are orthonormal projections so the result of a projection looks shorter than the actual length. –  Feb 26 '19 at 19:05
  • So I do not get how do you end up with sqrt(1/2) instead of sqrt(2), nor I do not know how to interpret "You only need to divide by sqrt(2) in your left example". Do you mean that I have to divide each parameter by sqrt(2) or only the vector? – manooooh Feb 26 '19 at 19:07
  • 1
    @manooooh We add a vector in the xy-plane. This vector goes in the (1,-1) direction. The normalized vector in this direction is (1/sqrt(2),-1/sqrt(2))=(cos(-45),sin(-45)). –  Feb 26 '19 at 19:09
  • 1
    That is what I have done. I have used sqrt(2)/2 instead of 1/sqrt(2). – manooooh Feb 26 '19 at 19:10
  • @manooooh Yes, then that's the correct result. (Sorry, I only looked at the left plot and normalized it. ;-) –  Feb 26 '19 at 19:11
  • Haha, no problem :). So, how do you go from ({1/2+(sqrt(2)/2)*t},{1/2-(sqrt(2)/2)*t},{1}) to your answer? I do not know why did you use \pgfmathsetmacro{\msq}{sqrt(1/2)} instead of \pgfmathsetmacro{\msq}{sqrt(2)}. – manooooh Feb 26 '19 at 19:13
  • 1
    @manooooh Because sqrt(2) is by a factor 2 too long. ;-) –  Feb 26 '19 at 19:15
  • LOL! But the norm of (1,-1,0) is sqrt(2), not sqrt(1/2)! – manooooh Feb 26 '19 at 19:15
  • @manooooh Yes, but we have to divide by the norm. –  Feb 26 '19 at 19:17
  • So... it is 1/sqrt(2) (equals to sqrt(2)/2), not 1/sqrt(1/2). – manooooh Feb 26 '19 at 19:17
  • 1
    @manooooh Yes, 1/sqrt(2) and sqrt(1/2) are the same. –  Feb 26 '19 at 19:19