Torus in Asymptote
A torus[1] is a great example of a parametric equation[2] with two parameters. We can think of one parameter that revolves around the Z-axis to form a large circle; and another parameter that sweeps small circles to form the torus.
Fig 1: Parametric torus
CC 2.5 - David Burke
The concept of these two circles can be seen in figure 1. Note that goes is the direction of the blue arrow and '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 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 , we get this parametric equation:
Where
- is the radius of the big circle
- is the parameter
Note that the radius does not exactly trace blue line in figure 1. Our large circle traces through the centers of the small circles.
We can think of as a generator of vectors. For each vector we need to draw the smaller circle that is described by .
Consider as the unit vector of . The small circle is drawn for this unit vector. It is a circle where the 'right' direction is and the 'up' direction is the axis:
Where
- is the radius of the small circle
- is a unit vector
- is the parameter
We have to draw this circle for each , so for the small circles we get this equation:
Where
- is that radius of the small circle
- is the vector from the bigger circle, with denoting the length of that vector.
- is the parameter
Note that the length of is always the radius of the large circle (). So we can combine equation 1 and 2:
Where
- is the radius of the large circle
- is the radius of the small circle
- is a parameter
- is a parameter
We leave this function in the vector form because Asymptote can work with vectors. This function 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 . This mapping not absolutely required, but it is nice to see our function 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 #
- Next: A parametric shell surface
- Previous: A 3-D seashell spiral