網路城邦
上一篇 回創作列表 下一篇   字體:
MetaPost語言﹣宏練習(三)
2013/07/17 08:45:51瀏覽278|回應0|推薦0

%這是做象棋的圖。先定義棋盤。

path mark, mark=(.1,.25)--(.1,.1)--(.25,.1);

boardUnit:=1cm;

def markAround(expr s, t) =

draw mark scaled s shifted t;

draw mark rotated 90 scaled s shifted t;

draw mark rotated (-90) scaled s shifted t;

draw mark rotated 180 scaled s shifted t;

enddef;

def markLeft (expr s, t) =

draw mark scaled s shifted t;

draw mark rotated (-90) scaled s shifted t;

enddef;

def markRight(expr s,t)=

draw mark rotated 90 scaled s shifted t;

draw mark rotated 180 scaled s shifted t;

enddef;

def cBoard(expr t)=

for i=1 upto 8:

draw ((0,0)--(0,4t)) shifted (i*t,0);

draw ((0,5t)--(0,9t)) shifted (i*t,0);

endfor

for i=1 upto 9:

draw ((0,0)--(8t,0)) shifted (0,t);

endfor

markAround(t, (t,2t));

markAround(t, (t,7t));

markAround(t, (7t,2t));

markAround(t, (7t,7t));

markAround(t, (2t,3t));

markAround(t, (4t,3t));

markAround(t, (6t,3t));

markAround(t, (2t,6t));

markAround(t, (4t,6t));

markAround(t, (6t,6t));

markLeft(t, (0,3t));

markLeft(t, (0,6t));

markRight(t, (8t,3t));

markRight(t, (8t,6t));

draw (3t,0)--(5t,2t);

draw (3t,2t)--(5t,0);

draw (3t,7t)--(5t,9t);

draw (3t,9t)--(5t,7t);

draw (0,0)--(0,9t)--(8t,9t)--(8t,0)--cycle;

draw (-.1t,-.1t)--(8.1t,-.1t)--(8.1t,9.1t)--(-.1t,9.1t)--cycle withpen pencircle scaled 1.2;

enddef;

def drawcBoard = cBoard(boardUnit) enddef;

%現在要考慮棋子要怎樣處理了。棋子紅、黑各有七種棋子,為了簡化輪入棋子程式碼,我定義黑棋是1到7號,紅棋是11﹣17號。而輪入則用putChess1(x,y)的方式將棋子畫在棋盤上面,所以要用到vardef來做這項工作。先搜尋一下,將(5c09)、士(58eb)、象(8c61)、車(8eca)、馬(99ac)、包(5305)、卒(5352)、帥(5e25)、仕(4ed5)、相(76f8)、俥(4fe5)、傌(508c)、炮(70ae)、兵(5175)等字的unicode碼,然後再開始處理棋子的問題。既然要使用vardef來處理,所以棋子的部分必須要能處理@所代表的值和傳入的二個數值。

def getChar(expr $) =

begingroup

save p, q, pa, m, n; picture p, q; path pa[];

if $=1:

p:=glyph 7 of "asong5c";

m:=0;

n:=t/(ypart (urcorner p) - ypart (lrcorner p));

for i within p:

pa[m]=pathpart i shifted -(center p) scaled m;

m:=m+1;

endfor

%show m;%主要檢查字有幾個path

q:=image(

fill fullcircle scaled .7t;

for i=0 utpo 3:

unfill pa[i] scaled .5;

endfor

undraw fullcircle scaled .6t;

);

elseif $=2:

p:=glyph 235 of "asong58";

m:=0;

n:=boardUnit/(ypart ulcorner p - ypart llcorner p);

for i within p:

pa[m]:=pathpart i shifted -(center p) scaled n;

m:=m+1;

endfor

%show m;

q:=image(

fill fullcircle scaled .7boardUnit;

unfill pa[0] scaled .45;

undraw fullcircle scaled .6boardUnit;

);

elseif $=3:
p:=glyph 97 of "asong8c";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit;
unfill pa[3] scaled .5;
for i=0 upto 2:
fill pa[i] scaled .5;
endfor
undraw fullcircle scaled .6boardUnit;
);
elseif $=4:
p:=glyph 202 of "asong8e";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit;
unfill pa[4] scaled .5;
for i=0 upto 3:
fill pa[i] scaled .5;
endfor
undraw fullcircle scaled .6boardUnit;
);
elseif $=5:
p:=glyph 172 of "asong99";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit;
for i=3 upto 7:
unfill pa[i] scaled .45;
endfor
for i=0 upto 2:
fill pa[i] scaled .45;
endfor
undraw fullcircle scaled .6boardUnit;
);
elseif $=6:
p:=glyph 5 of "asong53";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit;
unfill pa[2] scaled .45;
unfill pa[1] scaled .45;
fill pa[0] scaled .45;
undraw fullcircle scaled .6boardUnit;
);
elseif $=7:
p:=glyph 82 of "asong53";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit;
unfill pa[2] scaled .5;
unfill pa[1] scaled .5;
unfill pa[0] scaled .5;
undraw fullcircle scaled .6boardUnit;
);
elseif $=11:
p:=glyph 37 of "asong5e";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa3 scaled .5;
fill pa2 scaled .5;
for i=0 upto 1:
unfill pa[i] scaled .5;
endfor
draw fullcircle scaled .6boardUnit;
);
elseif $=12:
p:=glyph 213 of "asong4e";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa0 scaled .5;
fill pa1 scaled .5;
draw fullcircle scaled .6boardUnit;
);
elseif $=13:
p:=glyph 248 of "asong76";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa4 scaled .45;
fill pa3 scaled .45;
for i=0 upto 2:
unfill pa[i] scaled .45;
endfor
draw fullcircle scaled .6boardUnit;
);
elseif $=14:
p:=glyph 229 of "asong4f";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa5 scaled .5;
fill pa4 scaled .5;
for i=0 upto 3:
unfill pa[i] scaled .5;
endfor
draw fullcircle scaled .6boardUnit;
);
elseif $=15:
p:=glyph 140 of "asong50";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa8 scaled .5;
fill pa7 scaled .5;
fill pa6 scaled .5;
fill pa5 scaled .5;
fill pa4 scaled .5;
fill pa3 scaled .5;
for i=0 upto 2:
unfill pa[i] scaled .5;
endfor
draw fullcircle scaled .6boardUnit;
);
elseif $=16:
p:=glyph 174 of "asong70";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa3 scaled .45;
fill pa2 scaled .45;
fill pa1 scaled .45;
unfill pa0 scaled .45;
draw fullcircle scaled .6boardUnit;
);
 elseif $=17:
p:=glyph 117 of "asong51";
m:=0;
n:=boardUnit/(ypart ulcorner p - ypart llcorner p);
for i within p:
pa[m]:=pathpart i shifted -(center p) scaled n;
m:=m+1;
endfor
%show m;
q:=image(
fill fullcircle scaled .7boardUnit withcolor 1;
draw fullcircle scaled .7boardUnit;
fill pa3 scaled .45;
fill pa2 scaled .45;
fill pa1 scaled .45;
unfill pa0 scaled .45;
draw fullcircle scaled .6boardUnit;
);
else:
show "Not for cChar!";
fi

q

endgroup

enddef;

vardef putChess[](expr a,b)= draw getChar(@) shifted (boardUnit*(a, b)) enddef;

beginfig(1);

boardUnit:=1.5cm;

drawBoard;

putChess11(4,0);

putChess12(3,0);

putChess12(5,0);

putChess13(6,0);

putChess13(2,0);

putChess14(0,0);

putChess14(8,0);

putChess15(1,0);

putChess15(7,0);

putChess16(1,2);

putChess16(7,2);

putChess17(0,3);

putChess17(2,3);

putChess17(4,3);

putChess17(6,3);

putChess17(8,3);

putChess1(4,9);

putChess2(3,9);

putChess2(5,9);

putChess3(6,9);

putChess3(2,9);

putChess4(0,9);

putChess4(8,9);

putChess5(1,9);

putChess5(7,9);

putChess6(1,7);

putChess6(7,7);

putChess7(0,6);

putChess7(2,6);

putChess7(4,6);

putChess7(6,6);

putChess7(8,6);

endfig;

end;

%如果使用的是楷字粗體會效更好一點。但我只有這個字體所以就將就一下了。

( 興趣嗜好電腦3C )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=paraquat&aid=7945802