How to best draw a 3D Torus?

Hi everyone,

for a thesis I am trying to draw a 3D torus to visualize some trajectories on there. I am trying to achieve something similar-looking as the peeps over on the Tex side did here: tex.stackexchange
So far I have a working prototype:

#let torus(θ, φ) = (
  (R + r * calc.cos(φ)) * calc.cos(θ),
  (R + r * calc.cos(φ)) * calc.sin(θ),
  r * calc.sin(φ),
)
#cz.canvas({
 import cz.draw: *
  ortho(x: 70deg, y: 0deg, sorted: true, {
    // “longitude” rings (constant θ)
    for i in range(0, n_major) {
      let θ = i * 2 * calc.pi / n_major
      let pts = (range(0, n_samples + 1)).map(j => {
        let φ = j * 2* calc.pi / n_samples
        torus(θ, φ)
      })
      line(..pts, close: true)
    }

    // “latitude” rings (constant φ)
    for k in range(0, n_minor) {
      let φ = k * 2 * calc.pi / n_minor
      let pts = (range(0, n_samples + 1)).map(j => {
        let θ = j * 2 * calc.pi / n_samples
        torus(θ, φ)
      })
      line(..pts, close: true)
    }
  })
})

This achieves the look of a wire-frame torus, but it is not quite the look I am trying to get. It is a bit straining to look at. I would love to see as few lines as possible, i.e. mainly just the silhouette with some extra “shading” lines that make it clear it is a torus (see above link for references).
Does anybode here have some ideas of how I could achieve the desired look? I would be grateful for any input!

Thank you!

1 Like

As far as I render your code, it seems like just set the stroke to be more transparent should be good.

#cz.canvas({
 import cz.draw: *
 set-style(stroke: 0.5pt + black.transparentize(80%))
 // your code
})

1 Like