MetaPost 示例

双色球

u = 1.1cm;
drawoptions(withcolor 0.7red);

path a;
a = fullcircle scaled 1cm;
for i = 0 upto 6:
  draw a shifted (i*u,0);
endfor;

label.top(btex $01$ etex, (0,-6.2)) shifted (0,0);
label.top(btex $07$ etex, (0,-6.2)) shifted (1u,0);
label.top(btex $11$ etex, (0,-6.2)) shifted (2u,0);
label.top(btex $14$ etex, (0,-6.2)) shifted (3u,0);
label.top(btex $15$ etex, (0,-6.2)) shifted (4u,0);
label.top(btex $26$ etex, (0,-6.2)) shifted (5u,0);
label.top(btex 11 etex, (0,-5)) shifted (6u,0) withcolor 0.9blue;
draw a shifted (6u,0) withcolor 0.9blue;

长尾曲线

path p[];
p1 = (10,-5) -- (10,50);
p2 = (-5,0) -- (90,0);
p3 = (80,-5) -- (80,10);
p4 = (10,40) for i = 2 upto 9:
  .. (10*i, 40/i) endfor;
p5 = buildcycle(p1, p2, p3, p4);
filldraw p5 withcolor (0.15, 0.68, 0.38);
draw p1; draw p2; draw p3; draw p4;

pickup pencircle;
label.rt(btex $p_1$ etex, (10,50)) withcolor .8white;
label.bot(btex $p_2$ etex, (-5,0)) withcolor .8white;
label.rt(btex $p_3$ etex, (80,10)) withcolor .8white;
label.top(btex $p_4$ etex, point 3.5 of p4) withcolor .8white;

方向指针

sr = 50;
br = 60;
for angle = 0 step 30 until 330:
  s := sind angle;
  c := cosd angle;
  drawarrow (0,0) -- (sr * c, sr * s);
  label(decimal(angle), (br * c, br * s));
endfor;

带刻度的直线

u := 1cm;
draw (-1u,0) -- (6u,0) withcolor red;
for i := 0 upto 5:
  label.top(decimal(i), (i*u, -0.4u));
  draw(i*u, 0)--(i*u, 0.1u);
endfor

更多刻度的直线

u := 1cm;
drawdblarrow (-1u,0) -- (6u,0) withcolor red;
for i := 0 upto 5:
  label.top(decimal(i), (i*u, -0.4u));
  draw(i*u, 0)--(i*u, 0.15u);
  if i<5 : 
    for j := 1 upto 9:
      draw((i+j/10)*u, 0)--((i+j/10)*u, 0.1u) withcolor 0.5*white;
      if j=5 : draw((i+0.5)*u, 0)--((i+0.5)*u, 0.1u) withcolor blue; fi
    endfor
  fi
endfor

勾股定律

u := 1cm;

pair a, b, c;
a := (0,0);
b := (4u,0);
c := (4u,3u);
draw a--b--c--a;

pickup pencircle scaled 4pt;

label.bot(btex $x_2 - x_1$ etex, 1/2[a,b]);
label.rt(btex $y_2 - y_1$ etex, 1/2[c,b]);
label.lft(btex $d$ etex, 1/2[c,a]);

dotlabel.bot(btex $(x_1, y_1)$ etex, a);
dotlabel.rt(btex $b$ etex, b);
dotlabel.top(btex $(x_2, y_2)$ etex, c);

巧算尺寸

《啊哈!灵机一动》第 2 章:几何:关于图形的谜题。

u:=0.4cm;

% draw circle with radius r
r:=9u;
pair w,n,e,s;
w:=(-r,0); n:=(0,r); e:=(r,0); s:=(0,-r);
draw w..n..e..s..cycle;

% draw box
r:=5u;
x:=sqrt(56)*u;
draw (r,x)--(-r,x)--(-r,-x)--(r,-x)--(r,x) dashed evenly;

% draw rhombus, and fill it
draw (0,x)--(-r,0)--(0,-x)--(r,0)--(0,x);
fill (0,x)--(-r,0)--(0,-x)--(r,0)--(0,x)--cycle withcolor .9white;

% draw circle axis after fill rhombus
draw w--e;
draw n--s;

% dot label
dotlabel.urt(btex $A$ etex, (0,0));
dotlabel.urt(btex $B$ etex, (5u,0));
dotlabel.urt(btex $C$ etex, (9u,0));
dotlabel.urt(btex $D$ etex, (0,-x));

% line label
label.top(btex 5 etex, 1/2[(0,0), (5u,0)]);
label.top(btex 4 etex, 1/2[(5u,0), (9u,0)]);
label.top(btex ? etex, 1/2[(5u,0), (0,-x)]);

32 bits 的数组

u:=0.5cm;

draw(0,0)--(32u,0);
draw(0,1u)--(32u,1u);
draw(0,1.3u)--(32u,1.3u) withcolor .8white;

for i:=0 upto 32:
  draw(i*u,0)--(i*u, 1u);
  draw(i*u,1.3u)--(i*u,1.5u) withcolor .8white;
endfor

% filled bits

def myf(expr i,v) =
  label.top(decimal(v), 1/2[(i*u,0.1u),((i+1)*u,0.1u)]);
enddef;

for i:=0 upto 31:
  label.top(decimal(i), 1/2[(i*u,1.4u),((i+1)*u,1.4u)]) withcolor .8white;
  if i < 16: myf(i,0); else: myf(i,1); fi
endfor

% arrow

draw(0,-0.6u)--(0,-1.2u);
draw(16u,-0.6u)--(16u,-1.2u);
drawarrow(4u,-0.9u)--(0,-0.9u);
drawarrow(12u,-0.9u)--(16u,-0.9u);
label.top(btex $filled\ with\ 0$ etex, 1/2[(4u,-1.4u),(12u,-1.4u)]);

TCP 定时器

来自《TCP/IP 详解卷 1》18.2.1 TCP 的 500ms 定时器:

u:=1.2cm;
pickup pencircle scaled 2pt;
for i:=11 downto 0:
  draw(-i*u,-0.4u)--(-i*u,0.4u);
  label.top(decimal(i),(-i*u,0.7u));
endfor
draw(-13u,0)--(1u,0);
draw(-12u,-0.4u)--(-12u,0.4u);

drawdblarrow(-11u,1.5u)--(0u,1.5u);
label.top(btex 11 ticks * 500ms/tick = 5.5s etex, 1/2[(-11u,1.8u),(0u,1.8u)]);

drawdblarrow(-11.9u,-0.4u)--(-11.1u,-0.4u);
label.bot(btex start timer(6s) etex, 1/2[(-12u,-0.4u),(-11u,-0.4u)]);

drawarrow(0,-1u)--(0,-0.5u);
label.bot(btex start new timer(24s) etex, 1/2[(-2u,-1u),(2u,-1u)]);

drawdblarrow(-7u,-0.6u)--(-6u,-0.6u);
label.bot(btex 1 tick(500ms) etex, 1/2[(-7u,-0.8u),(-6u,-0.8u)]);

京ICP备12052177号-1