4

enter image description here

I am utilizing some code for a 2D observing eye and trying to position it within a 3D Cartesian coordinate system. However, when I execute the code below, the 2D eye is positioned well outside the specified axes and it is unclear as to what coordinate system it is being oriented with respect to. How can I control the position of the 2D structure while leaving its definition intact?

\documentclass[border=4pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepgfplotslibrary{colormaps,external}
\usetikzlibrary{calc,3d,arrows,shapes.geometric}

\pgfplotsset{compat=1.9,colormap={whitered}{color(0cm)=(yellow);       
 color(1cm)=(orange!75!red)},colormap={bidom}{color(0cm)=(white); 
 color(1cm)=(blue!75!red)}}


\begin{document}

\tdplotsetmaincoords{60}{150}%

\begin{tikzpicture}[tdplot_main_coords]


\begin{axis}[
    axis equal,
    axis lines = center,
    width = 8cm,
    height = 8cm,
    view/h=25,
    axis lines=none
]

\newcommand{\eye}[4]% size, x, y, rotation
{   \draw[rotate around={#4:(#2,#3)}] (#2,#3) -- ++(-.5*55:#1) (#2,#3) --    
++(.5*55:#1);
\draw (#2,#3) ++(#4+55:.75*#1) arc (#4+55:#4-55:.75*#1);
% IRIS
\draw[fill=gray] (#2,#3) ++(#4+55/3:.75*#1) arc       
(#4+180-55:#4+180+55:.28*#1);
%PUPIL, a filled arc 
\draw[fill=black] (#2,#3) ++(#4+55/3:.75*#1) arc (#4+55/3:#4-55/3:.75*#1);
}

\addplot3[surf, opacity = 0.5,
    samples=30,
    domain=-15:15,
    y domain=0:pi,
    z buffer=sort,color=gray!50!black]
  ({sqrt(15*15-x^2) * cos(deg(y))},
 {sqrt( 15*15-x^2 ) * sin(deg(y))},
 x);

\addplot3[surf, opacity = 0.5,
    samples=31,
    domain=-15:15,
    y domain=pi/2:3*pi/2,
    z buffer=sort,color=gray!50!black]
 ({sqrt(15*15-x^2) * cos(deg(y))},
 {sqrt( 15*15-x^2 ) * sin(deg(y))},
 x); 

\addplot3[surf, opacity = 0.5,
    samples=16,
    domain=-15:0,
    y domain=0:-pi/2,
    z buffer=sort,color=gray!50!black]
  ({sqrt(15*15-x^2) * cos(deg(y))},
 {sqrt( 15*15-x^2 ) * sin(deg(y))},
 x); 


    \addplot3[surf, opacity = 0.5,
    samples=21,domain=0:15.0,
    y domain=0:0.5*pi,z buffer=sort]
     ({sqrt( 15*15-x^2 ) * sin(deg(y))}, 0, x);

    \addplot3[surf, opacity = 0.5,
        samples=21,domain=0:15.0,
    y domain=-0.5*pi:0,z buffer=sort]
     (0, {sqrt( 15*15-x^2 ) * sin(deg(y))}, x);

     \addplot3[surf, opacity = 0.5,
        samples=21,domain=0:15.0,
        y domain=-0.5*pi:0,z buffer=sort]
        (x, {sqrt( 15*15-x^2 ) * sin(deg(y))}, 0);


     \addplot3 [domain = 0.0:15.0, samples = 50, samples y = 0, thick,     
       smooth,color=blue]
         (x,0,{x^3*exp(-x/1.5)});

     \addplot3 [domain = 0.066:15.0, samples = 50, samples y = 0, thick,  
       smooth,color=red]
         (x,0,{1/(x)});


     \addplot3[surf,opacity=0.1,domain=0.05:15.0,y domain=-  
        10.0:0.0,samples=50]
      ({x*cos(y)}, {x*sin(y)}, {x^3*exp(-x/1.5)});



    \draw [->,black] (axis cs:0,0,0) -- (axis cs:20,0,0);   
    \draw [->,black] (axis cs:0,0,0) -- (axis cs:0,0,20);
    \draw [->,black] (axis cs:0,0,0) -- (axis cs:0,-20,0);

   \begin{scope}[canvas is xz plane at y=0]
     \eye{16}{8}{8}{45}
   \end{scope}    

\end{axis}
\end{tikzpicture}
\end{document}

lightweight code

\documentclass[border=4pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepgfplotslibrary{colormaps,external}
\usetikzlibrary{calc,3d,arrows,shapes.geometric}

\pgfplotsset{
    compat=1.9,
    colormap={whitered}{color(0cm)=(yellow);color(1cm)=(orange!75!red)},
    colormap={bidom}{color(0cm)=(white);color(1cm)=(blue!75!red)}
}

\begin{document}

    \tdplotsetmaincoords{60}{150}%
    \begin{tikzpicture}[tdplot_main_coords]
        \begin{axis}[axis equal,axis lines=center,width = 8cm,height = 8cm,view/h=25,axis lines=none]
        \newcommand{\eye}[4]% size, x, y, rotation
        {
            \draw[rotate around={#4:(#2,#3)}](#2,#3)--++(-.5*55:#1)(#2,#3)--++(.5*55:#1);
            \draw(#2,#3)++(#4+55:.75*#1)arc(#4+55:#4-55:.75*#1);
            % IRIS
            \draw[fill=gray](#2,#3)++(#4+55/3:.75*#1)arc(#4+180-55:#4+180+55:.28*#1);
            %PUPIL, a filled arc 
            \draw[fill=black](#2,#3)++(#4+55/3:.75*#1)arc(#4+55/3:#4-55/3:.75*#1);
        }
            \addplot3[surf,opacity=.5,samples=10,domain=-15:15,y domain=0:pi,z buffer=sort,color=gray!50!black]
                ({sqrt(15*15-x^2)*cos(deg(y))},{sqrt(15*15-x^2)*sin(deg(y))},x);
            \addplot3[surf,opacity=.5,samples=9,domain=-15:15,y domain=pi/2:3*pi/2,z buffer=sort,color=gray!50!black]
                ({sqrt(15*15-x^2)*cos(deg(y))},{sqrt(15*15-x^2)*sin(deg(y))},x); 
            \addplot3[surf,opacity=.5,samples=9,domain=-15:0,y domain=0:-pi/2,z buffer=sort,color=gray!50!black]
                ({sqrt(15*15-x^2)*cos(deg(y))},{sqrt(15*15-x^2)*sin(deg(y))},x); 
            \addplot3[surf,opacity=.5,samples=9,domain=0:15,y domain=0:.5*pi,z buffer=sort]({sqrt(15*15-x^2)*sin(deg(y))},0,x);
            \addplot3[surf,opacity=.5,samples=9,domain=0:15,y domain=-.5*pi:0,z buffer=sort](0,{sqrt(15*15-x^2)*sin(deg(y))},x);
            \addplot3[surf,opacity=.5,samples=9,domain=0:15,y domain=-.5*pi:0,z buffer=sort](x,{sqrt(15*15-x^2)*sin(deg(y))},0);
            \addplot3[domain=0:15,samples=9,samples y=0,thick,smooth,color=blue](x,0,{x^3*exp(-x/1.5)});
            \addplot3[domain=.066:15,samples=9,samples y=0,thick,smooth,color=red](x,0,{1/(x)});
            \addplot3[surf,opacity=.1,domain=.05:15,y domain=-10:0,samples=9]({x*cos(y)},{x*sin(y)},{x^3*exp(-x/1.5)});
            \draw[->,black](axis cs:0,0,0)--(axis cs:20,0,0)node{x};
            \draw[->,black](axis cs:0,0,0)--(axis cs:0,0,20);
            \draw[->,black](axis cs:0,0,0)--(axis cs:0,-20,0);
            \begin{scope}[canvas is xz plane at y=0]
                \eye{16}{8}{8}{45}
            \end{scope}

        \end{axis}
    \end{tikzpicture}
\end{document}
7
  • rotate around is a 2D option and canvas is xz plane at y is a 3D option. Mixing them does not make perfect sense. What exactly do you want?
    – Symbol 1
    May 7, 2017 at 14:38
  • The 'eye' definition carries 4 parameters, two of which are its x- and y-coordinates in 2D space. Save defining a 3D analog, would it be possible to define the location of the 2D-eye with respect to the 3D origin in the figure albeit restricting it to a specific plane as I have tried to do. In short, I would like to have control of the location of the 2D eye wrt the 3D origin. May 7, 2017 at 14:49
  • OK... So what do you want to achieve. I naturally guess that you want the eye located on, say, positive x-axis and looking toward the origin. Right? And what do you want to achieve restricting the drawing to xz-plane? because all eyes I had seen lie on the 2D canvas.
    – Symbol 1
    May 7, 2017 at 15:05
  • @Symbol 1 - well the eye doesn't draw well at all if it isn't, naturally, restricted to the xz-plane. That is, presumably, a perspective effect. I want a number of eyes equidistant from one another aligned along a 2D line that gives the impression of parallelity to the x-axis. That latter part isn't two difficult - i can figure out the required math! My stumbling block is controlling the eye's location on the 2D plane. It is, presumably, quite easy to draw a simple 2D shape at a specific node/location in the 3D system - is the same true for the relatively more complex eye given its definition? May 7, 2017 at 15:16
  • Yes it is. But if you want to do the rotation in the xz plane (that is, rotation about the y-unit vector), you cannot use rotate around --- this option does not recognize the 3D information and will mess things up. I recently found a way to achieve true 3D rotation. But it might be overkill.
    – Symbol 1
    May 7, 2017 at 15:59

1 Answer 1

3

enter image description here

Taking into consideration Symbol 1's comments regarding the employment of "rotation" and "rotation around", I redefined the "eye" construct so that it was tailored to a 3D Cartesian coordinate system:

\documentclass[border=4pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepgfplotslibrary{colormaps,external} 
\usetikzlibrary{calc,3d,arrows,shapes.geometric}

\pgfplotsset{compat=1.9,colormap={whitered}{color(0cm)=(yellow);       
color(1cm)=(orange!75!red)},colormap={bidom}{color(0cm)=(white); 
color(1cm)=(blue!75!red)}}

\newcommand{\eye}[5]% size, x, y, z, rotation
{   \draw (axis cs: #2,#3,#4) (axis cs: #2,#3,#4) -- ++(-.5*55+#5:#1)    
    (axis cs: #2,#3,#4) -- ++(.5*55+#5:#1);
    \draw (axis cs: #2,#3,#4) ++(#5+55:.75*#1) arc (#5+55:#5-55:.75*#1);
    % IRIS
    \draw[fill=gray] (axis cs: #2,#3,#4) ++(#5+55/3:.75*#1) arc 
    (#5+180-55:#5+180+55:.28*#1);
    %PUPIL, a filled arc 
    \draw[fill=black] (axis cs: #2,#3,#4) ++(#5+55/3:.75*#1) arc 
    (#5+55/3:#5-55/3:.75*#1);
}

\tdplotsetmaincoords{60}{150}%

\begin{document}

\begin{tikzpicture}[tdplot_main_coords]

\begin{axis}[
    axis equal,
    axis lines = center,
    width = 8cm,
    height = 8cm,
    view/h=25,
    axis lines=none
]

 \addplot3[surf, opacity = 0.5,
    samples=30,
    domain=-15:15,
    y domain=0:pi,
    z buffer=sort,color=gray!50!black]
    ({sqrt(15*15-x^2) * cos(deg(y))},
    {sqrt( 15*15-x^2 ) * sin(deg(y))},
    x);

 \addplot3[surf, opacity = 0.5,
     samples=31,
     domain=-15:15,
     y domain=pi/2:3*pi/2,
     z buffer=sort,color=gray!50!black]
  ({sqrt(15*15-x^2) * cos(deg(y))},
  {sqrt( 15*15-x^2 ) * sin(deg(y))},
  x); 

 \addplot3[surf, opacity = 0.5,
     samples=16,
     domain=-15:0,
     y domain=0:-pi/2,
     z buffer=sort,color=gray!50!black]
  ({sqrt(15*15-x^2) * cos(deg(y))},
  {sqrt( 15*15-x^2 ) * sin(deg(y))},
  x); 


     \addplot3[surf, opacity = 0.5,
     samples=21,domain=0:15.0,
     y domain=0:0.5*pi,z buffer=sort]
     ({sqrt( 15*15-x^2 ) * sin(deg(y))}, 0, x);

     \addplot3[surf, opacity = 0.5,
     samples=21,domain=0:15.0,
     y domain=-0.5*pi:0,z buffer=sort]
      (0, {sqrt( 15*15-x^2 ) * sin(deg(y))}, x);

     \addplot3[surf, opacity = 0.5,
     samples=21,domain=0:15.0,
     y domain=-0.5*pi:0,z buffer=sort]
      (x, {sqrt( 15*15-x^2 ) * sin(deg(y))}, 0);


     \addplot3 [domain = 0.0:15.0, samples = 50, samples y = 0, thick, 
      smooth,color=blue] (x,0,{x^3*exp(-x/1.5)});

     \addplot3 [domain = 0.066:15.0, samples = 50, samples y = 0, thick, 
      smooth,color=red] (x,0,{1/(x)});

     \addplot3[surf,opacity=0.1,domain=0.05:15.0,y domain=-   
       10.0:0.0,samples=50]
       ({x*cos(y)}, {x*sin(y)}, {x^3*exp(-x/1.5)});


     \draw [->,black] (axis cs:0,0,0) -- (axis cs:20,0,0);
     \draw [->,black] (axis cs:0,0,0) -- (axis cs:0,0,20);
     \draw [->,black] (axis cs:0,0,0) -- (axis cs:0,-20,0);

     \eye{16}{3}{-30}{0}{45}

\end{axis}
\end{tikzpicture}
\end{document}

This code sufficiently satisfies by original problem.

5
  • Would you mind adding an image resulting from the above code? May 8, 2017 at 21:18
  • Image added Stefan. May 8, 2017 at 21:20
  • @StefanPinnow - How would you eliminate the need to consistently write "axis cs". I saw something somewhere but I have forgotten what it entailed. May 8, 2017 at 22:24
  • 1
    simply change compat=1.9 to compat=1.11. May 8, 2017 at 22:38
  • I had a feeling it was a compatibility issue. Thanks. May 8, 2017 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .