KABOOM in 180 lines of bare C++
The idea is simple: the sphere has an equation of the form x^2 + y^2 + z^2 – r^2 = 0 ; but the function f(x,y,z) = x^2 + y^2 + y^2 + z^2 – r^2 is defined anywhere in the 3D space. That is, the function f(x,y,z) computes the signed distance to our sphere for each point (x,y,z). When my ray crosses the sphere at some point (x,y,z), the value of the function g(x,y,z) sets the pixel color of the sphere.
Source: github.com