These examples are taken from the gallery on Asymptote's website. We start by installing the asymptote IPython magics remotely from the github repository.
# Install the IPython magic extension
%install_ext http://raw.github.com/azjps/ipython-asymptote/master/asymagic.py
Installed asymagic.py. To use it, type: %load_ext asymagic
# Load the extension
%reload_ext asymagic
%%asy
size(250);
real a=3;
real b=4;
real c=hypot(a,b);
transform ta=shift(c,c)*rotate(-aCos(a/c))*scale(a/c)*shift(-c);
transform tb=shift(0,c)*rotate(aCos(b/c))*scale(b/c);
picture Pythagorean(int n) {
picture pic;
fill(pic,scale(c)*unitsquare,1/(n+1)*green+n/(n+1)*brown);
if(n == 0) return pic;
picture branch=Pythagorean(--n);
add(pic,ta*branch);
add(pic,tb*branch);
return pic;
}
add(Pythagorean(12));
Using the -f --outformat
argument, the output format of the image can be changed. By default the image is output as a png. Asymptote may require additional configurations or third party programs (ImageMagick) for additional formats.
%%asy -f png
import graph3;
import palette;
import contour3;
size(400);
real f(real x, real y, real z) {
return cos(x)*sin(y)+cos(y)*sin(z)+cos(z)*sin(x);
}
surface sf=surface(contour3(f,(-2pi,-2pi,-2pi),(2pi,2pi,2pi),12));
sf.colors(palette(sf.map(abs),Gradient(red,yellow)));
currentlight=nolight;
draw(sf,render(merge=true));
A local asymptote code file can also be compiled and displayed if its specified as the argument to an asy file:
!head fin.asy
// See http://asymptote.sourceforge.net/gallery/fin.asy import three; import palette; int N = 26; real[] C = array(N,0); real[][] A = new real[N][N]; for(int i = 0; i < N; ++i) for(int j = 0; j < N; ++j)
%asy fin.asy
We can also interactively append asymptote code to these files:
%%asy fin.asy
label("I added this new label right now!", (2.0,0,0), NE);
To save the intermediate .asy
code file and output image, use the -r --root
argument.
%%asy --root cube
import three;
currentprojection=orthographic(5,4,2, center=true);
size(5cm);
size3(3cm,5cm,8cm);
draw(unitbox);
dot(unitbox, red);
label("$O$", (0,0,0), NW);
label("(1,0,0)", (1,0,0), S);
label("(0,1,0)", (0,1,0), E);
label("(0,0,1)", (0,0,1), Z);
!head cube.asy
import three; currentprojection=orthographic(5,4,2, center=true); size(5cm); size3(3cm,5cm,8cm); draw(unitbox); dot(unitbox, red); label("$O$", (0,0,0), NW);