Drawing a torus with rgl

Posted on May 29, 2018 by Stéphane Laurent
Tags: R, graphics, rgl, geometry

The following function creates a torus as a mesh3d object, which can be drawn with the rgl package.

Denote by \(O\) the center of the torus and by \(R\) its major radius. The equator of this torus (circle of center \(O\) and radius \(R\)) lies in the \(xy\)-plane (the plane \(z=0\)).

Now we are going to give a method to draw a torus such that its equator passes by three given points. This will be done with the help of the transform3d function of the rgl package. Thus, the key element to construct is the transformation matrix.

Firstly, we write a function which returns the center and the radius of a circle passing by three given points in the 3D space. This is the function circleCenterAndRadius below. The plane3pts function is an helper function which returns the equation of the plane passing by three points.

Note that circleCenterAndRadius actually returns not only the center and the radius, but also a vector perpendicular to the plane of the circle.

Now, we are ready to write the function which returns the desired transformation matrix.

Our function transfoMatrix also returns a radius, the major radius of the desired torus.

Let us test it now.

It works. So let’s play with it now.