Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262065
  • 博文数量: 41
  • 博客积分: 2013
  • 博客等级: 大尉
  • 技术积分: 523
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-08 23:13
文章分类

全部博文(41)

文章存档

2010年(41)

我的朋友

分类:

2010-02-28 18:39:11

初学metapost,感觉很容易上手。于是尝试用metapost绘一棵树,效果如下:


 1 beginfig(1)
 2 %% len: length
 3 %% dire: direction
 4 %% pos: position
 5 %% w: width for pen
 6 %% d: depth
 7 %% s: scale
 8 %% sign: -1->left, 1->right
 9 vardef drawsubtree(expr len,dire,pos,w,s,sign,d)=
10 if d>0:
11     save newpos;pair newpos;
12     save newdir;numeric newdir;
13     save scale; numeric scale;
14     scale := s + uniformdeviate(0.1);
15     save dirminus; numeric dirminus;
16     dirminus := 1000/(20+5d);
17     
18     newdir := (dire+55-dirminus+sign*sign)+uniformdeviate(10);
19     newpos := (len,0) rotated newdir shifted pos;
20     draw pos--newpos withpen pencircle scaled w withcolor 0.2white+.1green;
21     drawsubtree(scale*len,newdir,newpos,.5w,s,sign-1,(d-1));
22     newdir := (dire-65+dirminus-sign*sign)+uniformdeviate(10);
23     newpos := (len,0) rotated newdir shifted pos;
24     draw pos--newpos withpen pencircle scaled w withcolor 0.2white+.1green;
25     drawsubtree(scale*len,newdir,newpos,.5w,s,sign+1,(d-1));
26 else:
27     %draw pos withpen pencircle scaled w;
28     drawleave(pos,2len,.5w);
29 fi;
30 enddef;
31
32 vardef drawleave(expr pos,size,w)=
33     save a,b,c;
34     numeric a,b,c;
35     for i=0 upto 5:
36      a := uniformdeviate(size)-.5size;
37      b := uniformdeviate(size)-.5size;
38      c := uniformdeviate(size)-.5size;
39      draw pos..(a,b)+pos withpen pencircle scaled w withcolor .6green+.1red;
40      draw pos..(b,c)+pos withpen pencircle scaled w withcolor .6green+.1red;
41     endfor;
42 enddef;
43
44 width:=5bp;
45 d:=10;
46 u:=2cm;
47 pair pos; pos=(0,u);
48 draw origin--pos withpen pencircle scaled width withcolor 0.2white+.1green;
49 drawsubtree(.5u,90,pos,.5width,0.7,0,d);
50 endfig;
51 end
阅读(954) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~