Mathematically Drawing a Sphere

In pursuit of developing my own planetary software the first thing I needed to know was how to draw a sphere modelling the sky and how to project the stars on it.

Drawing spheres is simply when using spherical coordinates instead of the cartesian ones.

Spherical coordinates have 3 components:

  • The radius (r) is the Euclidean distance from the origin (O) to the point (P).
  • The inclination angle (Theta)¬†between the zenith direction and the line segment OP.
  • The azimuth angle (Phi)¬†measured from the azimuth reference direction to the orthogonal projection of the line segment OP on the reference plane.

By taking a look at the picture above you can easily see that keeping the radius and one of the angles fixed and moving the other one between 0 and 2*PI Radian (0 and 360 Degree) a circle can be drawn in the three dimensional space.

In case you don’t know how to convert between Radians and Degrees please read this article.

Latitude and Longitude circles can be drawn following this logic:

  • Keep the Theta and Radius fixed and move the Phi angle between 0 and 2*PI Radians.
  • Keep the Phi and Radius fixed and move the Theta angle between 0 and 2*PI Radians.

Once the sphere is drawn in the three-dimensional space the next step is to project it to a two dimensional canvas. This can be done by computing the Orthographic Projection, Camera and Rotation Matrix.

I wrote this HTML/Javascript example that will show you how to draw a sphere and project it to a two dimensional canvas. Visit this website for a live demo:

Source code can be found in this Git repository:

Leave a Reply

Your email address will not be published. Required fields are marked *