pgfplots:如何画克莱因瓶?

pgfplots:如何画克莱因瓶?

我想画一个这样朴实无华的克莱因瓶
在此处输入图片描述
(这意味着:表面线条很好,但颜色不多;实际上也没有灰度,就像下面的 MWE 一样

维基百科我说:
在此处输入图片描述

但我明白
在此处输入图片描述

我必须设置什么?

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[
declare function={
b=2;
h=6;
r(\u)=(2-cos(\u));
p(\u)=exp(  -(0.5*\u-pi)*(0.5*\u-pi)  );
q(\u)=exp(  -(\u-1.5*pi)*(\u-1.5*pi)  );
X(\u,\v)=b*(1-sin(\u))*cos(\u)+r(\u)*cos(\v)*(2*p(\u)-1);
Y(\u,\v)=r(\u)*sin(\v);
Z(\u,\v)=h*sin(\u)+0.5*r(\u)*sin(\u)*cos(\v)*q(\u);
},
]
\begin{axis}[
xlabel=$x$, ylabel=$y$, zlabel=$z$, 
%view/h=-10,
]
\addplot3[
surf,
z buffer=sort,
colormap={mycolormap}{%
color=(black) 
color=(white)
},
%semitransparent,
trig format plots=rad,
domain=0:2*pi, 
domain y=0:2*pi,
%restrict y to domain=-1:1,
%samples=41, samples y=25,
variable=\u, variable y=\v,
point meta=u,
] ({X(\u,\v)},   {Y(\u,\v)},    {Z(\u,\v)});
\end{axis}
\end{tikzpicture}
\end{document}

答案1

最后

import graph3;
import palette;
size(300);

currentprojection=orthographic(0.3,-1,0.4);
real b=2,h=6;
triple f(pair t) {
  real u=t.x;
  real v=t.y;
  real r=2-cos(u);
  real x=b*cos(u)*(1-sin(u))+r*cos(v)*(2*exp(-((u/2-pi)^2))-1);
  real y=r*sin(v);
  real z=h*sin(u)+0.5*r*sin(u)*cos(v)*exp(-((u-3*pi/2)^2));
  return (x,y,z);
}

surface s=surface(f,(0,0),(2pi,pi),15,15,Spline);
// s.colors(palette(s.map(zpart),Rainbow()));
// draw(s,meshpen=black+thick());
draw(s,lightgray+opacity(0.7),meshpen=black+thick());

在此处输入图片描述

在此处输入图片描述

仅用于比较渐近线

https://asymptote.sourceforge.io/gallery/3Dwebgl/Klein.asy,我得到

import graph3;
size(300);

currentprojection=orthographic(0.6,-1,0.8);
real b=2,h=6;
triple f(pair t) {
  real u=t.x;
  real v=t.y;
  real r=2-cos(u);
  real x=b*cos(u)*(1-sin(u))+r*cos(v)*(2*exp(-((u/2-pi)^2))-1);
  real y=r*sin(v);
  real z=h*sin(u)+0.5*r*sin(u)*cos(v)*exp(-((u-3*pi/2)^2));
  return (x,y,z);
}

surface s=surface(f,(0,0),(2pi,2pi),15,15,Spline);
draw(s,lightyellow, meshpen=black+thick());

在此处输入图片描述

或者

import graph3;
size(300);

currentprojection=orthographic(-0.5,1,0.5);
triple f(pair t) {
  real u=t.x;
  real v=t.y;
// https://commons.wikimedia.org/wiki/File:Klein_bottle.svg
real x=  v<2*pi ? (2.5-1.5*cos(v))*cos(u):
        v<3*pi ? -2+(2+cos(u))*cos(v): -2+2*cos(v)-cos(u);
real y=      v<2*pi ? (2.5-1.5*cos(v))*sin(u):  sin(u);
real z= v<pi ? -2.5*sin(v): 
            v < 2*pi ? 3*v-3*pi:
            v<3*pi ? (2+cos(u))*sin(v)+3*pi: -3*v+12*pi;
  return (x,y,-z);
}

surface s=surface(f,(0,0),(2pi,4pi),15,15,Spline);
draw(s,lightyellow+opacity(.5), meshpen=black+thick());

在此处输入图片描述

即可正常互动。

相关内容