Coding challenge

A torus (donut shape) can be described parametrically with two angles $u$ and $v$ (both ranging from $0$ to $2\pi$), a ring radius $R = 30$, and a tube radius $r = 10$: $$x = (R + r\cos(v))\cos(u)$$ $$y = (R + r\cos(v))\sin(u)$$ $$z = r\sin(v)$$ Using nested for-loops, step $u$ and $v$ around $0$ to $2\pi$ and draw small spheres with sphere(x, y, z, 1.5, "ffaa00"). Call scene3d() at the start.

Try adding spin(20) at the end to watch your 3D donut rotate in the viewport!

Type your code here:


Lua reference

See your results here: