# Celestial Programming : Moon Phase Image Rendering

### Rigorous

For those not wanting to settle for an approximation, rendering a photorealistic, properly shaded (with mountains and craters having shadows) image is pretty easy with existing tools like Blender, and NASA's CGI Moon Kit. Since tutorials on 3D rendering are available elsewhere, this will focus on an approximate solution which generally looks right.

### Approximate

An approximate method is to imagine a plane intersecting the center of the Moon, perpendicular to the direction of the Sun. As the phase changes, the plane is rotated about the Y axis. To keep things simple, we can ignore any perspective distortion (e.g. ignore the Z coordinate), the Y coordinate won't change, leaving only the X coordinate to be computed, and the rotation matrix simplifies to: $$x=r \cos \theta$$ Where $$\theta$$ is the is the phase from 0° (new moon) to 360°, and r is the radius of the full sphere.

Since the sun is an approximate .5° sphere as viewed from the moon, the terminator is not a hard edge. Additionally, the moon's mountains and craters will also soften the transition from light to dark, so the code below creats a 2° gradient between the light and dark regions to provide a more realistic effect.

Moon Image Credit: NASA