WD

Torus in Asymptote

A torus[1] is a great example of a parametric equation[2] with two parameters. We can think of one parameter α\alpha that revolves around the Z-axis to form a large circle; and another parameter β\beta that sweeps small circles to form the torus.


Fig 1: Parametric torus
©\copyright CC 2.5 - David Burke

The concept of these two circles can be seen in figure 1. Note that α\alpha goes is the direction of the blue arrow and β\beta's sweep is shown by the red arrow.

Asymptote[3] is an application that allows us to create technical drawings using a programming language. It is a very powerful application - what we show here only touches the surface of what is possible to do with it. We can play with it using the Asymptote online sandbox hosted by the University of Alberta, or installing the software. If you have Latex installed, you might have the program on your workstation already.

This article has the following straight forward challenge:

Use Asymptote to draw a torus that is defined as a parametric equation.

Math groundwork #

First off, let's get the mathematics down. We use the notation [x,y,z][x,y,z] as a row vector to denote a vector or a point in three dimensional Cartesian space[4].

For our blue circle, centered around the origin [0,0,0][0,0,0], we get this parametric equation:

Where

Note that the radius rr does not exactly trace blue line in figure 1. Our large circle traces through the centers of the small circles.

We can think of faf_a as a generator of vectors. For each vector vv we need to draw the smaller circle that is described by β\beta.

Consider vu=v/vv_u = v / |v| as the unit vector of vv. The small circle is drawn for this unit vector. It is a circle where the 'right' direction is vuv_u and the 'up' direction is the yy axis:


Where

We have to draw this circle for each vv, so for the small circles we get this equation:

Where

Note that the length of vv is always the radius of the large circle (v=r1|v| = r_1). So we can combine equation 1 and 2:

Where

We leave this function in the vector form because Asymptote can work with vectors. This function ff is the function of our torus in parametric form.

Asymptote basics #

Asymptote uses the data type called triple to keep three dimensional coordinates.

In program 1 below, we declare a triple and print its length.

triple p = (2,3,sqrt(12));
write("p=",p);
write("length of p=",length(p));

Program 1: Asymptote triple

In the next program, we show how to declare and invoke a function:

triple fun(triple v) {
return v*2;
}
triple p = (2,3,4);
write("fun(p)=",fun(p));

Program 2: Asymptote function

Asymptote torus #

The program below draws the torus. (If you try it online here, you get an interactive 3d drawing.)

import graph3;
real r_1 = 2;
real r_2 = 0.25;

size(10cm,0);
currentprojection=perspective(1,2,1);

triple f(real r_1,real r_2, real alpha, real beta) {
return (cos(alpha),sin(alpha),0)*(r_1 + r_2*cos(beta))
+ r_2*(0,0,sin(beta));
}

triple mapper(pair t) {
return f(r_1,r_2,t.x,t.y);
}

surface s=surface(mapper,(0,0),(2pi,2pi),16,16,Spline);
draw(s,rgb(0.6,0.6,0.7));

Program 3: Torus drawing

You can see the image produced by this program in figure 3 below.


Fig 3: Image produced by Asymptote

We need the mapper function to go from a pair to a call to the function ff. This mapping not absolutely required, but it is nice to see our function ff defined in equation 3 directly coded into the Asymptote language.

Conclusion #

We have seen here how to get a parametric function of a torus and also how to draw that in the Asymptote application. The end result is a very small program.

References #

  1. Torus - Wikipedia
  2. Parametric equation - Wikipedia
  3. Asymptote site
  4. Cartesian coordinates - Wikipedia