Skip to content

Instantly share code, notes, and snippets.

@asinghvi17
Last active March 3, 2023 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asinghvi17/ddd7fa8667dd6657ae2c870716bb84f8 to your computer and use it in GitHub Desktop.
Save asinghvi17/ddd7fa8667dd6657ae2c870716bb84f8 to your computer and use it in GitHub Desktop.
Visualizing the Klein bottle with Makie.jl
using Makie, WGLMakie
# implementation from the PGFPlots example gallery, at https://pgfplots.net/klein-bottle/
klein(u, v) = Point3f((-2/15 * cos(u) * (
3*cos(v) - 30*sin(u)
+ 90 *cos(u)^4 * sin(u)
- 60 *cos(u)^6 * sin(u)
+ 5 * cos(u)*cos(v) * sin(u))
),
(-1/15 * sin(u) * (3*cos(v)
- 3*cos(u)^2 * cos(v)
- 48 * cos(u)^4*cos(v)
+ 48*cos(u)^6 *cos(v)
- 60 *sin(u)
+ 5*cos(u)*cos(v)*sin(u)
- 5*cos(u)^3 * cos(v) *sin(u)
- 80*cos(u)^5 * cos(v)*sin(u)
+ 80*cos(u)^7 * cos(v) * sin(u))
),
(2/15 * (3 + 5*cos(u) *sin(u))*sin(v)))
us = LinRange(0, π, 41)
vs = LinRange(0, 2π, 25)
points = klein.(us', vs)
xt, yt, zt = map(i -> getindex.(points, i), 1:3)
index_color = (((u, v),) -> v + (u-1)*length(vs)).(tuple.(axes(us, 1)', axes(vs, 1)))
u_color = first.(tuple.(us', vs))
colormap = cgrad([:blue, :yellow, :orange, :red, :orange, :yellow, :blue])
surface(xt, yt, zt; color = color, colormap = colormap, shading = false, axis = (; type = Axis3))
scatter(xt |> vec, yt |> vec, zt |> vec; color = u_color |> vec, colormap = :viridis) # produces the correct topology
@asinghvi17
Copy link
Author

klein2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment