TransWikia.com

pgfplots: How to draw a Klein bottle?

TeX - LaTeX Asked on March 25, 2021

I want to draw an unpretentious Klein bottle like that
enter image description here
(that means: surface lines are ok, but not many colors; actually no grayscale either, like in my MWE below)

wikipedia says me:
enter image description here

But I get with that
enter image description here

What do I have to set?

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}

One Answer

Finally.

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());

enter image description here

enter image description here

Just for comparison with Asymptote.

From https://asymptote.sourceforge.io/gallery/3Dwebgl/Klein.asy, I get

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());

enter image description here

or

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());

enter image description here

You can interact normally.

Answered by user213378 on March 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP