Skip to content

Instantly share code, notes, and snippets.

@stla
Last active February 16, 2022 08:35
Show Gist options
  • Select an option

  • Save stla/6613ad396f43a98e3ff482b391241acd to your computer and use it in GitHub Desktop.

Select an option

Save stla/6613ad396f43a98e3ff482b391241acd to your computer and use it in GitHub Desktop.
Hopf torus with moving colors using Asymptote
settings.render = 4;
settings.outformat = "eps";
size(500,0);
import graph3;
import palette;
currentlight = light(gray(0.85), ambient=black, specularfactor=13,
(0.5,0.5,1), specular=white, viewport=false);
// files to create
string[] files = {
"TH000", "TH001", "TH002", "TH003", "TH004", "TH005",
"TH006", "TH007", "TH008", "TH009", "TH010", "TH011",
"TH012", "TH013", "TH014", "TH015", "TH016", "TH017",
"TH018", "TH019", "TH020", "TH021", "TH022", "TH023",
"TH024", "TH025", "TH026", "TH027", "TH028", "TH029",
"TH030", "TH031", "TH032", "TH033", "TH034", "TH035",
"TH036", "TH037", "TH038", "TH039", "TH040", "TH041",
"TH042", "TH043", "TH044", "TH045", "TH046", "TH047",
"TH048", "TH049", "TH050", "TH051", "TH052", "TH053",
"TH054", "TH055", "TH056", "TH057", "TH058", "TH059"};
pen[] klingon = {
rgb("fffa0cff"), rgb("cfa001ff"), rgb("c97000ff"), rgb("ca2500ff"), rgb("b10000ff"),
rgb("850000ff"), rgb("590000ff"), rgb("2c0000ff"), rgb("000000ff")};
triple F(pair uv){
real t = uv.x;
real phi = uv.y;
real nlobes = 3;
real A = 0.44;
real G = pi/2 - (pi/2-A)*cos(nlobes*t);
real H = t + A*sin(2*nlobes*t);
real sinG = sin(G);
real p1 = cos(G) + 1;
real p2 = sinG * cos(H);
real p3 = sinG * sin(H);
real xden = sqrt(2*p1);
real cosphi = cos(phi);
real sinphi = sin(phi);
real x4 = cosphi*p1;
real x3 = sinphi*p1;
real x2 = cosphi*p2 - sinphi*p3;
real x1 = cosphi*p3 + sinphi*p2;
return (x1/(xden-x4), x2/(xden-x4), x3/(xden-x4));
}
real M = abs(F((0, 0)));
surface s = surface(F,(0,0),(2pi,2pi),60,65,Spline);//Notaknot,Notaknot);
for(int i=0; i<60; ++i){
picture pic;
real f(triple xyz){
return (sin(2*pi*(abs(xyz)/M - i/60)) + 1) / 2;
}
s.colors(palette(s.map(f),Gradient(... klingon)));
draw(pic, s);
add(rotate(-20,(0,1,0))*rotate(-45,(0,0,1))*rotate(90,(1,0,0))*pic);
shipout(files[i]);
erase();
}
// gs -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=png16m -r600 -sOutputFile=HopfTorusMovingColors_klingon%02d.png TH*.eps
// mogrify -resize 512x HopfTorusMovingColors_k*.png
// gifski HopfTorusMovingColors_k*.png --fps 10 --output HopfTorusMovingColors_klingon.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment