Professional Documents
Culture Documents
1.79
symptote
Asymptote 1.79
http://asymptote.sourceforge.net
20049 Andy Hammerlindl, John Bowman, and Tom Prince.
GNU Lesser General Public LicenseGNU
LICENSE /
leoliu.pku@gmail.com 2009 7 6
ii
Asymptote 1.79
LATEX
7
4.1 draw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 ll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 clip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.4 label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
17
5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.2 pathguide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.5 framepicture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.9.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.11.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.11.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
5.12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.12.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
iii
iv
5.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.15 static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
84
Asymptote 1 http://www.artofproblemsolving.com/
Wiki/index.php/Asymptote:_Basics
Asymptote (0, 0) (100, 100)
asy
draw((0,0)--(100,100));
draw((0,0)--(100,100));
2
big point TEX TEX 1/72 inch point, pt TEX
1 pt = 1/72.27 inch
3
1
2
asy -V test
-V PostScript PostScript
test.eps -o
draw((0,0)--(100,0)--(100,100)--(0,100)--cycle);
size(101,101);
draw((0,0)--(1,0)--(1,1)--(0,1)--cycle);
(0,0)--(1,0)--(1,1)--(0,1)--cycle unitsquare
box((0,0),(1,1))
pt1 pt = 1/72.27 inchcmmm inches5 size
0 45 5.5 size
size(0,3cm);
draw(unitsquare);
1cm
unitsize(1cm);
draw(unitsquare);
4
MSDOS Windows
5
inch inches
3
x y
unitsize(1cm,2cm);
draw(unitsquare);
Asymptote LATEX
size(0,3cm);
draw(unitsquare);
label("$A$",(0,0),SW);
label("$B$",(1,0),SE);
label("$C$",(1,1),NE);
label("$D$",(0,1),NW);
D C
A B
size(100,0);
draw((1,0){up}..{left}(0,1));
-- ..
cycle
0.06%
path unitcircle=E..N..W..S..cycle;
4
{dir} dir
c0 z0 c1 z1 Bzier
6
z0 z1 c0 c1 m5
(z0 , c0 , c1 , z1 ) Bzier t = 1/2
c0 m1 c1
m3 m5 m4
m0
m2
z0 z1
m0 , m1 m2 m3 m4 m5
(z0 , m0 , m3 , m5 ) (m5 , m4 , m2 , z1 )
[0, 1] t
t = 1/2
Bzier
1
0.75 John D. Hobby, Discrete and Computational Geometry 1, 1986
6
Bzier
METAPOST METAPOST: A Users Manual 4 Curves METAPOST ConTEXt
METAFUN 1
5
draw((100,0)..tension 2 ..(100,100)..(0,100));
draw((100,0)..tension 2 and 1 ..(100,100)..(0,100));
draw((100,0)..tension atleast 1 ..(100,100)..(0,100));
0 1
METAPOST ...
Asymptote .. tension atleast 1 .. :: ...
Asymptote 66 5.11.3
draw((0,0){up}..(100,25){right}..(200,0){down});
draw((0,0){up}::(100,25){right}::(200,0){down});
size(0,100);
path unitcircle=E..N..W..S..cycle;
path g=scale(2)*unitcircle;
filldraw(unitcircle^^g,evenodd+yellow,black);
7
6
PostScript evenodd8
zerowinding
filldraw(unitcircle^^reverse(g),yellow,black);
import three;
dotgranularity=0; // Render dots as spheres.
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);
8
9
layer layer
TEX PostScript
currentpicture
4.1 draw
pic p g L align
arrow bar margin legend marker
arrow bar 1 legend
7
8
c
a
a b
4.2 FILL 9
dotfactor 6
45 5.5
4.2 ll
p pic g
filldraw
picture opic;
fill(opic,g,p);
add(pic,opic,origin);
g pic
p fillrule
stroke=true draw(pic,g,fillrule+zerowinding) 6
g p rgb(pen)
cmyk(pen) latticeshading.asy 7
a--b pena penb 8
a ra pena b rb penb
size(200);
pen[][] p={{white,grey,black},
{red,green,blue},
{cyan,magenta,yellow}};
latticeshade(unitsquare,p);
unitsize(1cm);
path g = ellipse((0,0), 4, 1);
axialshade(g, green, (-4,0), yellow, (4,0));
axialshade(shift(0,-2.5)*g, stroke=true,
green+linewidth(2mm), (-4,0), yellow, (4,0));
4.2 FILL 11
shade.asyring.asy shadestroke.asy 9
z edges fillrule p
Gouraud
z g p Gouraud
Gouraud.asy solids.asy 10 Gouraud
http://partners.adobe.com/public/developer/en/ps/sdk/TN5600.SmoothShading.pdf
b 4 n n 4 p n 4
fillrule
z Coons p
n = 1
size(100);
radialshade(W..N..E--(0,0),stroke=true,
red+linewidth(30),(0,0),0.25,yellow,(0,0),1);
10
p Gouraud.asy
(0,1)
2
1
(1,0) (1,0)
0 (0,0)
12
4.3 clip
4.4 label
size(200);
pen[] p={red,green,blue,magenta};
path g=(0,0){dir(45)}..(1,0)..(1,1)..(0,1)..cycle;
tensorshade(g,p);
dot(g);
12
picture pic;
path C1 = circle((0,0), 1cm), C2 = circle((1cm,0), 1cm);
fill(C1, paleblue); draw(pic, C2, linewidth(2mm));
fill(pic, C1, heavyblue); clip(pic, C2);
add(pic);
4.4 LABEL 13
44 5.4
rotate(45)*xscale(2)*L x L 45
PostScript shift(10,0)*Lembed
Shift
Rotate
Rotate(pair z)
z
Slant
Scale
0 length(g) arctime 25
5.2 arctime position Relative(real)
13
13
Label position
position BeginPoint=Relative(0);
position MidPoint=Relative(0.5);
position EndPoint=Relative(1);
align pair
Relative(pair) 14 LeftSideCenter RightSide
Relative(W)Relative((0,0)) Relative(E) LeftSideCenter RightSide
15
dir b arrowlength
align
dir
string graphic(string name, string options="") PostScript
Encapsulated PostScriptEPSname options
bb=llx lly urx urywidth=value
height=valueangle=valuescale=factor clip=bool
draft=boollayer()
label(graphic("file.eps","width=1cm"),(0,0),NE);
layer();
position Relative(value)
14
Relative(N) Label position align
Relative(0.5)=MidPoint
1
0.5
LeftSide N Relative(0.8)
W E
RightSide S
)
(S
ve
ti
la
Re
Label
0 position align 2
15
13 Label plain pair
4.4 LABEL 15
overwrite 34 5.3
plain_Label.asy object
16
Pythagoras.asy 4
16
Asymptote
The Vector Graphics Language
Andy Hammerlindl, John Bowman, and Tom Prince
http://asymptote.sourceforge.net
symptote
size(11.7cm,11.7cm);
asy(nativeformat(),"logo");
fill(unitcircle^^(scale(2/11.7)*unitcircle),
evenodd+rgb(124/255,205/255,124/255));
label(scale(1.1)*minipage(
"\centering\scriptsize\textbf{\LARGE{\ttAsymptote}\\
\smallskip
\smallTheVectorGraphicsLanguage}\\
\smallskip
\textsc{AndyHammerlindl,JohnBowman,andTomPrince}
http://asymptote.sourceforge.net\\
",8cm),(0,0.6));
label(graphic("logo."+nativeformat(),"height=7cm"),(0,-0.22));
clip(unitcircle^^(scale(2/11.7)*unitcircle),evenodd);
//
// x real
real x;
// real x 1
x=1.0;
// x 1
if(x == 1.0) {
write("x1.0");
} else {
write("x1.0");
}
// 10
for(int i=0; i < 10; ++i) {
write(i);
}
//
int[] array={1,1,2,3,5};
for(int k : array) {
write(k);
1
C, C++ Java do
17
18
5.1
Asymptote
void void
bool b = true;
b true
bool b;
false
int 0 intMin
intMax
real 0.0
realEpsilon realDigits realMin
realMax
pair z=(3,4);
z=(z.x,-z.y);
z=z.x-I*z.y;
z=conj(z);
I (0,1)
pair conf(pair z)
z
real length(pair z)
z |z|
5.1 19
pair z=(3,4);
length(z);
5abs(pair) length(pair)
pair unit(pair z)
z
real xpart(pair z)
z.x
real ypart(pair z)
z.y
real length(triple v)
v |v|abs(triple) length(triple)
triple unit(triple v)
v
real xpart(triple v)
v.x
2
v 0 O three (0,0,0)
5.1 21
real ypart(triple v)
v.y
real zpart(triple v)
v.z
\" "
\\ \\
' ANSI C
\' '
\" "
\? ?
\\
\a
\b
\f
3
4
22
\n
\r
\t
\v
\0\377
\x0xFF 5
"" + 0
int length(string s)
s
string reverse(string s)
s
int hex(string s)
s
time();
time("%a%b%d%H:%M:%S%Z%Y");
6
format("%04d", 3) "0003" Asymptote hlll
%d %i %o%x %X%u
# # 0 0x0X
%*d
format("%"+string(4)+"d", 3)
7
Asymptote Windows
L %f %e %g
# format(string, int)
1.77 $
format("%010.5f", pi) 0003.14159 format("%+.3g", 0.00001234) +1.23e-05
format("$%+.3g$", 0.00001234) $+1.23\!\times\!10^{-5}$
8
K&R C99
24
Unix date
seconds("Mar0211:12:36AMPST2007","%b%d%rPST%Y");
seconds(time("%b%d%r%z%Y"),"%b%d%r%z%Y");
seconds(time("%b%d%r%Z%Y"),"%b%d%r"+time("%Z")+"%Y");
1+(seconds()-seconds("Jan1","%b%d"))/(24*60*60);
time(seconds()-24*60*60);
void abort(string s)
s
s
%a %A
%b %B
%c %C 100
%d 0131 %D %m/%d/%y
%e 131 %F %Y-%m-%d ISO 8601
%g ISO 8601 0099 %G
%h %b %H 24 0023
%I 12
0112 %j 001366
%m 0112 %M 0059
%n %p AM PM
%r 12 %R %H:%M
%S 0060 %t
%T %H:%M:%S ISO 8601 %u 17 1
%U 0053 %w 06 0
%W 0053 %V ISO 8601 0153
%x %X
%y 0099 %Y
%z UTC ISO 8601 %% %
"+0800" UTC
8 0
9
(int)time("%j")
5.2 PATHGUIDE 25
void exit()
10
void beep()
5.2 pathguide
path nullpath
circle(pair c, real r) c r Bzier
unitcircle 1 12
graph.asy Circle 13
import graph;
path Circle(pair c, real r, int n=nCircle);
path unitcircle=E..N..W..S..cycle;
4 (1,0)
13
nCircle graph nCircle 400
0.06%
nCircle
26
graph.asy n Bzier
import graph;
path Arc(pair c, real r, real angle1, real angle2, bool direction,
int n=nCircle);
path Arc(pair c, real r, real angle1, real angle2, int n=nCircle);
path Arc(pair c, explicit pair z1, explicit pair z2,
bool direction=CCW, int n=nCircle);
size(300,0);
pair[] z=new pair[10];
path p=z[0]..z[1]---z[2]::{up}z[3]
&z[3]..z[4]--z[5]::{up}z[6]
&z[6]::z[7]---z[8]..{up}z[9];
14
plain c z1 z2 c c z1
5.2 PATHGUIDE 27
draw(p,grey+linewidth(4mm));
dot(z);
sign < 0 p t
sign > 0 sign = 0
30
p (x,0)
real[] mintimes(path p)
2 p
real[] maxtimes(path p)
2 p
struct slice {
path before,after;
}
p knife n p
n
METAPOST buildcycle 19
19
METAPOST p1 , p2 , , pk pi pi+1 pi pi+1
METAPOST: A Users Manual 9.1 Building Cycles
20
= 2 = 1 = 0 = 1 = 2
H
= (t0 ) (0) = 1
2i
dz
C za
C : z = z(t), (t [0, t0 ]) z a
(r(t), (t))
21
fillrule zerowinding windingnumber != 0 fillrule evenodd
windingnumber%2 != 0 windingnumber
32
size(200);
int n=30;
real a=1.5;
real width=2a/n;
guide hat;
path solved;
draw(hat);
dot(hat,red);
draw(solved,dashed);
guide g;
for(int i=0; i < 10; ++i)
5.2 PATHGUIDE 33
g=g--(i,i);
path p=g;
path p;
for(int i=0; i < 10; ++i)
p=p--(i,i);
nullpath
a..{up}nullpath..b;
c..nullpath{up}..d;
e..{up}nullpath{down}..f;
a..nullpath..{up}b;
c{up}..nullpath..d;
e{down}..nullpath..{up}f;
5.3
Asymptote 7
currentpen METAPOST
pickup defaultpen
+
dashed+red+green red+green+dashed
red+dashed+green *
1 26
RGB 27 [0, 1]
CMYK 28 [0, 1]
pen invisible;
TEX
\phantom bool invisible(pen)
6 RGB
27
redgreenblue
28
cyanmagentayellow black
36
import x11colors;
68 CMYK TEX
import texcolors;
Green
Asymptote asycolors.sty LATEX LATEX CMYK Asymptote
LATEX LATEX
7.17 slide 29
LATEX Asymptote LATEX
usepackage("asycolors");
pen p=hsv(180,0.5,0.75);
29
5.3 37
pen solid=linetype("");
pen dotted=linetype("04");
pen dashed=linetype("88");
pen longdashed=linetype("248");
pen dashdotted=linetype("8808");
pen longdashdotted=linetype("24808");
pen Dotted=dotted+1.0;
pen Dotted(pen p=currentpen) {return dotted+2*linewidth(p);}
solid defaultpen(pen)
defaultpen(2);
pen p=red+0.5;
PostScript linecap 31
30
Asymptote PostScript
31
line cap PostScript
38
pen squarecap=linecap(0);
pen roundcap=linecap(1);
pen extendcap=linecap(2);
PostScript linejoin 32
pen miterjoin=linejoin(0);
pen roundjoin=linejoin(1);
pen beveljoin=linejoin(2);
PostScript fillrule
pen zerowinding=fillrule(0);
pen evenodd=fillrule(1);
basealign
pen nobasealign=basealign(0);
pen basealign=basealign(1);
squarecap
roundcap
extendcap
32
PostScript
5.3 39
nobasealign defaultpen(pen)
baseline TEX int basealign(pen p=currentpen)
import fontsize;
x-cm http://www.ctan.org/tex-archive/help/Catalogue/entries/
fix-cm LATEX
real fontsize(pen p=currentpen) real lineskip(pen p=currentpen)
LATEX NFSS
opacity 0 1 blend
-
"Compatible","Normal","Multiply","Screen","Overlay","SoftLight",
"HardLight","ColorDodge","ColorBurn","Darken","Lighten","Difference",
"Exclusion","Hue","Saturation","Color","Luminosity"
http://partners.adobe.com/public/developer/en/pdf/PDFReference16.pdf
import patterns;
void add(string name, picture pic, pair lb=0, pair rt=0);
size(0,90);
import patterns;
34
size(0,150);
settings.outformat="pdf";
begingroup();
fill(shift(1.5dir(120))*unitcircle,green+opacity(0.75));
fill(shift(1.5dir(60))*unitcircle,red+opacity(0.75));
fill(unitcircle,blue+opacity(0.75));
endgroup();
5.3 41
add("tile",tile());
add("filledtilewithmargin",tile(6mm,4mm,red,Fill),(1mm,1mm),(1mm,1mm));
add("checker",checker());
add("brick",brick());
real s=2.5;
filldraw(unitcircle,pattern("tile"));
filldraw(shift(s,0)*unitcircle,pattern("filledtilewithmargin"));
filldraw(shift(2s,0)*unitcircle,pattern("checker"));
filldraw(shift(3s,0)*unitcircle,pattern("brick"));
size(0,100);
import patterns;
add("hatch",hatch());
add("hatchback",hatch(NW));
add("crosshatch",crosshatch(3mm));
real s=1.25;
filldraw(unitsquare,pattern("hatch"));
filldraw(shift(s,0)*unitsquare,pattern("hatchback"));
filldraw(shift(2s,0)*unitsquare,pattern("crosshatch"));
42
PostScript patterns.asy
9 4.2
35
size(0,100);
import patterns;
real d=4mm;
picture tiling;
path square=scale(d)*unitsquare;
axialshade(tiling,square,white,(0,0),black,(d,d));
fill(tiling,shift(d,d)*square,blue);
add("shadedtiling",tiling);
filldraw(unitcircle,pattern("shadedtiling"));
pen makepen(path)
path nib(pen)
METAPOST
size(200);
pen convex=makepen(scale(10)*polygon(8))+grey;
draw((1,0.4),convex);
draw((0,0)---(1,1)..(2,0)--cycle,convex);
pen nonconvex=scale(10)*
makepen((0,0)--(0.25,-1)--(0.5,0.25)--(1,0)--(0.5,1.25)--cycle)+red;
draw((0.5,-1.5),nonconvex);
draw((0,-1.5)..(1,-0.5)..(2,-1.5),nonconvex);
35
PostScript 3
5.3 43
nullpath
yscale(2)*currentpen
overwrite
Allow
defaultpen(pen)
Suppress
SuppressQuiet
Move
PostScript
MoveQuiet
PostScript
defaultpen() resetdefaultpen()
44
5.4
Asymptote (x,y) t=(t.x,t.y,t.xx,t.xy,t.yx,t.yy)
(x',y')
x'=t.x+t.xx*x+t.xy*y
y' = t.y + t.yx * x + t.yy * y
transform identity();
5.5 FRAMEPICTURE 45
5.5 framepicture
frame frame PostScript
picture newframe f
bool empty(frame f) true erase(frame)
pair min(frame) pair max(frame) (, ) (, )
src dest
38
label align 12 4.4 f align
envelope
39
38
39
f f L
46
picture pic;
new picture
size
x y 0 PostScript pic
identity()
x y 0
keepAspect Aspect true x
y
keepAspect IgnoreAspect false x
y
pic x x y y
x y pic
pic box(min,max)
p
shipout pic format prefix.fotmat
shipout() shipout()
Portrait orientation
orientation=Landscape
shipout(Landscape);
pic
pic 2D
filltype
FillDraw
Fill
NoFill
.
Draw
UnFill
40
0.25 cm
shipout(bbox(0.25cm));
bbox(p,Fill) p
pic dir
user false PostScript
point pic
user false PostScript
add
picture pic1;
real size=50;
size(pic1,size);
fill(pic1,(0,0)--(50,100)--(100,0)--cycle,red);
picture pic2;
size(pic2,size);
fill(pic2,unitcircle,green);
picture pic3;
size(pic3,size);
fill(pic3,unitsquare,blue);
picture pic;
add(pic,pic1.fit(),(0,0),N);
add(pic,pic2.fit(),(0,0),10S);
add(pic.fit(),(0,0),N);
add(pic3.fit(),(0,0),10S);
void erase(picture pic=currentpicture);
TEX 47
void texreset();
void usepackage(string s, string options="");
texpreamble("\usepackage["+options+"]{"+s+"}");
LATEX
44
save() restore
45
plain
46
47
52
5.6
Asymptote CSV XDR External Data Repre-
sentation
input(string name, bool check=true, string comment="#")
file fin=input("test.txt");
real a=fin;
check false
bool error(file) true comment
name
update=false
update=true
-globalwrite -nosafe
stdin stdout
null
T
file stdout
s x suffix
none flushendl
5.6 53
file fout=output("test.txt");
write(fout,1); // "1"
write(fout); //
write(fout,"List:",1,2,3); // "List: 1 2 3"
write(output("transcript.asy"),history());
transcript.asy
int delete(string s) s -globalwrite -nosafe
int rename(string from, string to)
from to -globalwrite -nosafe
5.7
int x=3; x 3 3
real x=2*sin(pi/2);
int x=2;
int x=5*x;
x x x
x 10
bool false
string
transform
pen
frame picture
file null
code
y 3
operator init
5.8
C++
public restricted
private
this
50 51
struct T {
int x;
}
T foo=new T;
T bar=foo;
bar.x=5;
50
C/C++
51
C/C++
56
int Tcount=0;
struct T {
int x;
++Tcount;
}
T foo=new T;
new T Tcount
52
null
struct tree {
int value;
tree left;
tree right;
}
struct S {
real a=1;
real f(real a) {return a+this.a;}
}
S s; // new S s
52
Tcount T 82 5.15
53
5.8 57
write(s.f(2)); // 3
write((s+s).f(0)); // 2
Person
struct Person {
string firstname;
string lastname;
}
Person
Person(string,string)
struct Person {
string firstname;
string lastname;
Person.Person
Person
struct Person {
string firstname;
string lastname;
struct parent {
real x;
void operator init(int x) {this.x=x;}
void virtual(int) {write(0);}
void f() {virtual(1);}
}
struct child {
parent parent;
real y=3;
void operator init(int x) {parent.operator init(x);}
void virtual(int x) {write(x);}
parent.virtual=virtual;
void f()=parent.f;
}
parent p=parent(1);
child c=child(2);
write(c); // 2;
p.f(); // 0;
c.f(); // 1;
write(c.parent.x); // 2;
write(c.y); // 3;
54
C++
55
C++
child parent unravel parent; parent.x parent
_f(parent obj=this, int) virtual(int) this
60
5.9
5.9.1
Asymptote
int quotient(int x, int y)
x/y
+
-
*
/
% p q
q*quotient(p,q)+p%q == p
^
** ^
56
==
!=
<
<=
>=
>
&& 57
&
||
|
^
!
Asymptote C
5.9.2
C +, -, *, /, % ^ ++
--
int i=1;
i += 2;
int j=++i;
int i=1;
i=i+2;
int j=i=i+1;
i++ i-- --
i++ i-- (++i-1) (--i+1)
5.9.3
operator
}
for(int i=1; i < g.length; ++i) {
write(g[i]);
write();
G=dots(G,g[i]);
}
return G;
}
5.10
int x=2;
real y=2.0;
real cm=72/2.540005;
write(3x);
write(2.5x);
write(3y);
write(-1.602e-19 y);
write(0.5(x,y));
write(2x^2);
write(3x+2y);
write(3(x+2y));
write(3sin(x));
write(3(sin(x))^2);
write(10cm);
6
5
6
-3.204e-19
(1,1)
8
5.11 63
10
18
2.72789228047704
2.48046543129542
283.464008929116
5.11
Asymptote
55
5.8
Asymptote
1.
int x, x();
x=5;
x=new int() {return 17;};
x=x(); // x() 17 x
2.
int sqr(int x)
{
return x*x;
}
sqr=null; //
3.
int c();
c=a; //
4.
64
5. f
f f
59
void f() {
write("hi");
}
void g() {
f();
}
g(); // "hi"
g(); // "bye"
f(); // "overloaded"
g(); // "bye"
6. null
void g(bool b) {
59
operator init()
null f g f g f f
g
5.11 65
if(b) f(b);
else write(b);
}
f=new void(bool b) {
write(b);
g(false);
};
Asymptote
C++
GNU libsigsegv 2.4
5.11.1
Asymptote C++
78 5.13
5.11.2
C C++
-d Asymptote
34 4 x y
66
int x;
write(f(4,(x=3)));
int x;
x=3;
write(f(4,3));
43
Asymptote f(2,seven) 9
f(x=2,x=seven) f(x=seven,2)
int() int f
f(x=seven) f(seven)
5.11.3
//
int sum(... int[] nums) {
int total=0;
for(int i=0; i < nums.length; ++i)
total += nums[i];
return total;
}
sum(1,2,3,4); // 10
sum(); // 0
5.11 67
//
int subtract(int start ... int[] subs) {
for(int i=0; i < subs.length; ++i)
start -= subs[i];
return start;
}
subtract(10,1,2); // 7
subtract(10); // 10
subtract(); //
subtract
nums sum
null 60
Asymptote C++
60
empty arraynull array 69 5.12
null C/C++
68
g(3,4); //
//
h(1,2); //
//
i(3,4); //
//
5.11.4
Asymptote libm real(real) sin, cos, tan, asin, acos, atan,
exp, log, pow10, log10, sinh, cosh, tanh, asinh, acosh, atanh, sqrt, cbrt, fabs, expm1, log1p
identity61 Asymptote n Bessel J(int n, real)
Y(int n, real) gamma erf erfc
real(real, real) atan2, hypot, fmod, remainder62
degrees(real radians) radians(real degrees)
Degrees(real radians) [0, 360) Asymptote
Sin, Cos, Tan, aSin, aCos aTan sqrt, sin, cos, exp,
log gamma
floor, ceil round63
Floor, Ceil Round
intMax intMin
sgn1, 0 1
abs(int) abs(real) fabs(real) abs(pair)
length(pair)
srand(int) int rand() 0 randMax
61
sin cos tan asin acos atan exp
log e pow10 log10 10 sinh cosh tanh asinh
acosh atanh sqrt cbrt fabs expm1 x 7 exp x 1
log1p x 7 log(1 + x) identity
62
p
atan2(real y, real x) y/x hypot(real x, real y) x2 + y 2 fmod remainder
63
floor xceil x round
5.12 69
import gsl;
write(Si(1.0));
Asymptote
fprime x Newton-Raphson f
verbose=trueiterations
realMax
5.12
[] A i A[i]
64
stats.asy
65
int
70
B A A[B] A B
Java 17 66
real[] A;
real[] A=null;
real[] A={0,1,2};
Asymptote
copy
T[] A
int length
void cyclic(bool b)
bool cyclicflag
int[] keys
T push(T x)
void append(T[] a)
T pop()
void delete()
bool initialized(int n)
A.length A.cyclic(true)
cyclic
A.cyclicflag cyclic
A.keys
n A.keys 0 n-1 A.keys
key
66
for (elem : array) { statements }
5.12 71
int[] A={1};
A.push(2); // A {1,2}
A.append(A); // A {1,2,1,2}
int f(int)=A.push;
f(3); // A {1,2,1,2,3}
int g()=A.pop;
write(g()); // 3
A.delete(0); // A {2,1,2}
A.delete(0,1); // A {2}
A.insert(1,3); // A {2,3}
A.insert(1 ... A); // A {2,2,3,3}
A.insert(2,4,5); // A {2,2,4,5,3,3}
[]
real a,A[];
a real A real[]
T alias, array, copy, concat,
sequence, map transpose T T[]
new T[]
T[]
new T[n]
n n
int[] sequence(int n)
72
n >= 1 {0,1,...,n-1}
int[] reverse(int n)
n >= 1 {n-1,n-2,...,0}
T[] copy(T[] a)
a
T[][] copy(T[][] a)
a
T[][][] copy(T[][][] a)
a
a b 68 true
T[] sort(T[] a)
T a 69
T[][] sort(T[][] a)
T a
string[][] a={{"bob","9"},{"alice","5"},{"pete","7"},{"alice","4"}};
// 0 1
write(sort(a));
alice 4
alice 5
bob 9
pete 7
T[][] transpose(T[][] a)
a
T sum(T[] a)
T a T bool a
T min(T[] a)
T min(T[][] a)
T min(T[][][] a)
T a
T max(T[] a)
T max(T[][] a)
T max(T[][][] a)
68
69
a
70
n S3
o ai1 ,i2 ,i3 0 6 i1 , i2 , i3 < n
ai(1) ,i(2) ,i(3) 0 6 i1 , i2 , i3 < n, S3
74
T a
int n=4;
pair[] f=sequence(n);
write(f);
pair[] g=fft(f,-1);
write();
write(g);
f=fft(g,1);
write();
write(f/n);
import math;
real[][] a={{1,-2,3,0},{4,-5,6,2},{-7,-8,10,5},{1,50,1,-2}};
real[] b={7,19,33,3};
real[] x=solve(a,b);
write(a); write();
write(b); write();
write(x); write();
write(a*x);
a warn false a
tridiagonal tridiagonal
real[][] identity(int n)
n n
real[][] inverse(real[][] a)
a
Asymptote
C++
real[] a={1,2};
real[] b={3,2};
76
file fin=csv(input("test.txt"));
real[] A=fin;
dimension(file,int)
file fin=input("test.txt");
real[] A=dimension(fin,10);
A 10
0
file fin=input("test.txt");
real[][] A=dimension(fin,2,3);
real[][][] B=dimension(fin,2,3,4);
read1, read2
read3
file fin=input("test.txt");
real[] A=read1(fin);
real[][] B=read2(fin);
real[][][] C=read3(fin);
write(file,T[]), write(file,T[][]), write(file,T[][][])
5.12 77
5.12.1
Asymptote Python slice A
A[m:n] A m n
int[] x={0,1,2,3,4,5,6,7,8,9};
int[] y=x[2:6]; // y={2,3,4,5};
int[] z=x[5:10]; // z={5,6,7,8,9};
int[] x={0,1,2,3,4,5,6,7,8,9};
int[] y=x[:4]; // y={0,1,2,3}
int[] z=x[5:]; // z={5,6,7,8,9}
int[] w=x[:]; // w={0,1,2,3,4,5,6,7,8,9}, x
A[m:n] [m, n)
int[] x={0,1,2,3,4,5,6,7,8,9};
x.cyclic(true);
int[] y=x[8:15]; // y={8,9,0,1,2,3,4}.
int[] z=x[-5:5]; // z={5,6,7,8,9,0,1,2,3,4}
int[] w=x[-3:17]; // w={7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6}
x[m:n]=x
x[m:n]=copy(x) x[m:m]=y x x[m]
y
A 10 A[8:12], A[-3:1] A[5:25]
A[3:7], A[7:10], A[-3:0] A[103:107]
A[0]
A A[A.length:A.length] A[0:0]
A.append()
5.13
Asymptote int realint pairreal pairpair pathpair guide
path guideguide pathreal penpair[] guide[]pair[] path[]path
path[] guide path[] three.asy
explicit 0
write(f(0));
real[] a={2.5,-3.5};
int[] b=(int []) a;
write(stdout,b); // 2,-3
operator cast
struct rpair {
real radius;
real angle;
}
5.14 79
rpair x;
x.radius=1;
x.angle=pi/6;
write(x); // (0.866025403784439,0.5)
100
100
x*100 int
real
operator ecast
5.14
Asymptote Asymptote
access graph;
graph.axes();
x y graph
graph
non-private
unravel
struct matrix {
real a,b,c,d;
}
real det(matrix m) {
unravel m;
return a*d-b*c;
}
import graph;
access graph;
unravel graph;
Asymptote import
as
settings Asymptote
Asymptote 2.5 73
73
5.14 81
s Asymptote
eval("import"+s,true);
asy
embedded true
embedded false settings
74
Asymptote
real a=1;
code s=quote {
write(a);
};
eval(s,true); // 1
graph
include graph;
include "/usr/local/share/asymptote/graph.asy";
imports true
74
82
5.15 static
struct s {
int count() {
static int c=0;
++c;
return c;
}
}
s c count
int factorial(int n) {
int helper(int k) {
static int x=1;
x *= k;
return k == 1 ? x : helper(k-1);
}
return helper(n);
}
factorial helper x
struct A {
struct B {
static pair z;
}
}
A z
}
}
for(int i=0; i < k; ++i) {
A a;
a.helper();
}
return A.x;
}
pow x
void f();
for(int i=0; i < 10; ++i) {
int x=i;
if(x==5) {
f=new void () { write(x); }
}
}
f();
x f() 5
static
void f() {
static int x;
for(int i=0; i < 10; ++i) {
static int y;
}
}
x y f
struct A {
static int x;
}
84
pair A;
int y=A.x;
A.x A Asymptote
85
86
ArcArrow, 7 beveljoin, 38
ArcArrows, 7 binput, 53
arclength, 28 Black, 35
arctime, 28 black, 35
array, 71 Blank, 7
Arrow, 7 blue, 35
arrow, 14 Bookman, 39
arrowhead, 7 bool, 18
arrowlength, 14 bool3, 18
Arrows, 7 boutput, 53
arrowsize, 7 box, 15, 45
as, 80 bp, 1
asin, 68 break, 17
asinh, 68 brick, 40
Aspect, 46 brown, 35
asy, 81 buildcycle, 30
asycolors.sty, 36 Bzier , 4
atan, 68 , 4
atan2, 68 , 4
atanh, 68
attach, 50 calculateTransform, 47
AvantGarde, 39 cast, 78
axialshade, 10 cbrt, 68
azimuth, 20 CCW, 26
cd, 52
Bars, 7 Center, 14
barsize, 7 change.system, 58
basealign, 38 change.user, 58
baseline, 14 chartreuse, 35
bbox, 47 checker, 40
beep, 25 child.system, 58
BeginArcArrow, 7 child.user, 58
BeginArrow, 7 Circle, 25
BeginBar, 7 circle, 25
BeginDotMargin, 8 clear, 53
BeginMargin, 8 clip, 12
BeginPenMargin, 8 close, 53
BeginPoint, 13 cm, 2
87
cmyk, 35 darkbrown, 35
CMYK , 35 darkcyan, 35
code, 55, 81 darkgray, 35
colatitude, 20 darkgreen, 35
colorless, 35 darkmagenta, 35
colors, 35 darkolive, 35
colorspace, 35 dashdotted, 37
comma, 52 dashed, 37
complement, 72 deepblue, 35
concat, 72 deepcyan, 35
conf, 18 deepgray, 35
continue, 17 deepgreen, 35
controls, 4 deepmagenta, 35
controlSpecifier, 33 Default, 7
convert, 54 default, 18
copy, 72 defaultformat, 9
cos, 68 defaultpen, 34, 35
cosh, 68 degrees, 19
Courier, 39 delete, 54, 70
cputime, 58 diagonal, 75
cross, 21 dimension, 76
crosshatch, 41 dir, 19, 20, 27, 28
CSV, 52 dirSpecifier, 33
csv, 76 dirtime, 28
cubicroots, 75 do, 17
curl, 5 dot, 9, 19, 21, 74
curlSpecifier, 34 dotfactor, 8
currentpen, 34, 51 DotMargin, 8
currentpicture, 51 DotMargins, 8
currentprojection, 51 Dotted, 37
CW, 26 dotted, 37
Cyan, 35 down, 3
cyan, 35 Draw, 7, 48
cycle, 3 draw, 7, 9, 11, 15
cyclic, 27, 33, 70
cyclicflag, 70 E, 3
ecast, 79
darkblue, 35 ellipse, 15, 26, 45
88
else, 17 filltype, 47
empty, 45 find, 22, 72
EndArcArrow, 7 firstcut, 30
EndArrow, 7 fit, 47
EndBar, 7 fixedscaling, 46
EndDotMargin, 8 flush, 52, 53
endl, 52 fmod, 68
EndMargin, 8 font, 39
EndPenMargin, 8 fontcommand, 39
EndPoint, 13 fontsize, 39
ENE, 3 fontsize.asy, 39
envelope, 15, 45 for, 17
eof, 53 format, 23
eol, 53 frame, 45
erase, 22, 45, 51 from, 79
erf, 68 fuchsia, 35
erfc, 68 functionshade, 12
error, 52, 53
gamma, 68
eval, 81
getc, 52
evenodd, 6, 38
getint, 53
exit, 24
getpair, 53
exp, 68
getreal, 53
expi, 19, 20
getstring, 53
explicit, 78
gettriple, 53
expm1, 68
gouraudshade, 11
extendcap, 37
graphic, 14
extension, 30
gray, 34, 35
green, 35
fabs, 68
guide, 31
false, 18, 54
fft, 74 hatch, 41
file, 52 heavyblue, 35
Fill, 7, 48 heavycyan, 35
fill, 9 heavygray, 35
FillDraw, 7, 47 heavygreen, 35
filldraw, 9 heavymagenta, 35
filloutside, 10 heavyred, 35
fillrule, 38 Helvetica, 39
89
hex, 23 lastcut, 30
history, 54 LATEX NFSS , 39
HookHead, 7 latitude, 20
hsv, 36 latticeshade, 10
hypot, 68 layer, 7
left, 3
I, 18
LeftSide, 14
identity, 44, 68, 75
legend, 8
if, 17
length, 18, 20, 22, 27, 33, 70
IgnoreAspect, 46
libm , 68
import, 80
lightblue, 35
inch, 1
lightcyan, 35
inch, 2
lightgray, 35
inches, 2
lightgreen, 35
include, 81
lightmagenta, 35
init, 55, 58
lightolive, 35
initialized, 70
lightred, 35
input, 52
lightyellow, 35
insert, 22, 70
line, 76
inside, 31
int, 18 linecap, 37
interior, 31 linejoin, 38
interp, 61 linetype, 37
intersect, 29 linewidth, 37
intersectionpoint, 30 list, 81
intersectionpoints, 30 locale, 23
intersections, 29 log, 68
intMax, 18 log10, 68
intMin, 18 log1p, 68
inverse, 44, 75 longdashdotted, 37
invisible, 35 longdashed, 37
longitude, 20
J, 68
Magenta, 35
keys, 70
magenta, 35
Label, 13 makepen, 42
label, 3, 12, 13 map, 72
labelmargin, 8, 12 Margin, 8
Landscape, 47 Margins, 8
90
polar, 20 relpoint, 29
pop, 70 reltime, 28
Portrait, 47 remainder, 68
position, 13 rename, 54
postcontrol, 28 replace, 22
postscript, 51 restore, 51
PostScript , 39 restricted, 55
pow10, 68 reverse, 22, 29, 33, 72
precision, 53 rfind, 22
precontrol, 28 rgb, 34, 35
prepend, 45 RGB , 34
private, 55 right, 3
pt, 1 RightSide, 14
pt, 2 Rotate, 13
public, 55 rotate, 44
purple, 35 roundbox, 15, 45
push, 70 roundcap, 37
roundjoin, 38
quadraticroots, 75
royalblue, 35
quit, 1
quote, 81
safe, 54
quotient, 60
save, 51
RadialShade, 48 saveline, 54
radialshade, 10 Scale, 13
radius, 28 scale, 44, 47
read1, 76 scroll, 53
read2, 76 search, 72
read3, 76 seconds, 24
readline, 54 seek, 53
real, 18 seekeof, 53
realDigits, 18 sequence, 71
realEpsilon, 18 settings.scroll, 53
realMax, 18 Shift, 13
realMin, 18 shift, 44
realmult, 19, 21 shiftless, 45
red, 35 shipout, 46
reflect, 44 SimpleHead, 7
Relative, 13, 14 sin, 68
92
single, 53 TEX , 39
sinh, 68 texcolors, 36
size, 2, 27, 33, 46, 48 TeXHead, 7
Slant, 13 texpath, 15
slant, 44 texpreamble, 51
sleep, 25 texreset, 51
slice, 30 this, 55
solid, 37 tile, 40
solve, 74, 75 time, 23, 24
sort, 73 times, 29, 30
split, 23 TimesRoman, 39
springgreen, 35 transform, 44
sqrt, 68 transpose, 73
squarecap, 37 tridiagonal, 74
static, 82 triple, 19
stdin, 52 true, 18
stdout, 52 TrueMargin, 8
straight, 27 truepoint, 49
string, 21, 23 typedef, 25
strokepath, 31
UnFill, 7, 48
struct, 55
unfill, 12
subpath, 29
uniform, 72
substr, 22
unit, 19, 20
sum, 73
unitcircle, 3, 25
Suppress, 43
unitsize, 2, 46
SuppressQuiet, 43
unitsquare, 2
Symbol, 39
unravel, 80
system, 54
up, 3
UpsideDown, 47
tab, 52
usepackage, 51
tan, 68
usleep, 25
tanh, 68
tell, 53
void, 18
tension, 4
tension atleast, 4 while, 17
tensionSpecifier, 33 white, 35
tensorshade, 11 windingnumber, 31
tex, 51 write, 52, 58, 76
93
x, 18, 19 , 54
x , 19, 20 , 12
x11colors, 36 , 63
XDR, 53 , 19, 20
xinput, 53 , 21
XOR, 61 , 5
xoutput, 53 , 53
xpart, 19, 20 , 21
xscale, 44 , 21
, 12
Y, 68
, 44
y, 18, 19
, 44
y , 19, 20
, 44
Yellow, 35
, 44
yellow, 35
, 44
ypart, 19, 20
, 44
yscale, 44
, 44
z, 19 , 44
z , 21 , 44
ZapfChancery, 39 , 44
ZapfDingbats, 39 , 46
zerowinding, 6, 38 , 8
zpart, 21 , 25
, 25
, 19
, 14
, 4
, 9
, 1
, 17
, 44
, 18
, 20
, 1, 18
, 4 , 1
, 4 , 39
, 63 , 21
, 63 , 22
, 63 , 22
, 63 , 18
, 65 , 18, 19
, 63 , 12, 14
slice, 77 , 79
94
, 7 , 56
, 18
, 45 Coons , 11
, 19 Gouraud , 11
, 1 , 12
, 21 , 11
, 55 , 11
, 4 , 10
, 17 , 10
, 10
, 55
, 18 , 34
, 1 , 1
, 2 , 19, 21
, 61 , 3
, 17 , 8
, 4 , 34
, 17 , 38
, 7 , 39
, 18 , 39
, 69 , 38
slice, 77 , 34
null array, 70 , 38
empty array, 70 , 34
, 18 , 42
, 52 , 37
, 21 , 37
, 17 , 37
, 57 , 43
, 12 , 38
, 13 , 39
, 3, 12 , 40
, 13 , 34
, 26 , 9
, 18, 20 , 7
, 17 , 55
, 70 , 55
, 70 , 55
95
, 56 blue, 35
null array, 70 brown, 35
empty array, 70 chartreuse, 35
, 18 Cyan, 35
, 7 cyan, 35
, 59, 78 darkblue, 35
, 20 darkbrown, 35
, 20 darkcyan, 35
, 55 darkgray, 35
, 55 darkgreen, 35
, 56 darkmagenta, 35
, 4 darkolive, 35
, 59 deepblue, 35
, 3 deepcyan, 35
deepgray, 35
, 1
deepgreen, 35
, 1
deepmagenta, 35
, 59
fuchsia, 35
, 18, 19
gray, 35
, 31
green, 35
, 25
heavyblue, 35
, 8
heavycyan, 35
heavygray, 35
, 61
heavygreen, 35
, 61
heavymagenta, 35
, 60
heavyred, 35
, 61
lightblue, 35
, 61
lightcyan, 35
, 44 lightgray, 35
, 26 lightgreen, 35
, 19, 21 lightmagenta, 35
, 17 lightolive, 35
, 62 lightred, 35
static, 82 lightyellow, 35
, 26 Magenta, 35
magenta, 35
Black, 35 mediumblue, 35
black, 35 mediumcyan, 35
96
mediumgray, 35
mediumgreen, 35
mediummagenta, 35
mediumred, 35
mediumyellow, 35
olive, 35
orange, 35
paleblue, 35
palecyan, 35
palegray, 35
palegreen, 35
palered, 35
paleyellow, 35
pink, 35
purple, 35
red, 35
royalblue, 35
springgreen, 35
white, 35
Yellow, 35
yellow, 35
, 35
, 63
, 55