轻量级代码

轻量级代码

在此处输入图片描述

我正在使用一些用于 2D 观察眼的代码,并尝试将其定位在 3D 笛卡尔坐标系中。但是,当我执行下面的代码时,2D 眼睛的位置远远超出了指定的轴,并且不清楚它相对于哪个坐标系定向。我如何控制 2D 结构的位置,同时保持其定义不变?

\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}

轻量级代码

\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}

答案1

在此处输入图片描述

考虑到符号 1 关于使用“旋转”和“旋转”的评论,我重新定义了“眼睛”结构,使其适合 3D 笛卡尔坐标系:

\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}

该代码充分满足原始问题。

相关内容