| 1 |
% spaceinvader.mp |
| 2 |
% L. Nobre G. |
| 3 |
% 2012 |
| 4 |
|
| 5 |
prologues := 1; |
| 6 |
|
| 7 |
vardef renewcolore = |
| 8 |
color tmpcolo; |
| 9 |
tmpcolo = (uniformdeviate(0.8),uniformdeviate(0.8),uniformdeviate(0.8)); |
| 10 |
drawoptions( withcolor tmpcolo ); |
| 11 |
enddef; |
| 12 |
|
| 13 |
primarydef A xor B = ((not A) and B) or (A and not B) enddef; |
| 14 |
|
| 15 |
def strai( expr FlagVer, FlagForward, IndI, IndJ ) = |
| 16 |
begingroup |
| 17 |
path tmppath; |
| 18 |
if FlagVer: |
| 19 |
tmppath = (IndI*u,(IndJ-1)*u+mar)---(IndI*u,IndJ*u-mar); |
| 20 |
else: |
| 21 |
tmppath = ((IndI-1)*u+mar,IndJ*u)---(IndI*u-mar,IndJ*u); |
| 22 |
fi; |
| 23 |
if not FlagForward: |
| 24 |
tmppath := reverse tmppath; |
| 25 |
fi; |
| 26 |
( tmppath ) |
| 27 |
endgroup |
| 28 |
enddef; |
| 29 |
|
| 30 |
beginfig(1); |
| 31 |
string st[]; |
| 32 |
st16 = "..............M."; |
| 33 |
st15 = ".............M.."; |
| 34 |
st14 = "MM..........M..."; |
| 35 |
st13 = "..MMM......M...."; |
| 36 |
st12 = ".....M....M....."; |
| 37 |
st11 = "MMM...M..M...MMM"; |
| 38 |
st10 = "..MMM.MMMM.MMM.."; |
| 39 |
st9 = "....MMMMMMMM...."; |
| 40 |
st8 = "....M..MM..M...."; |
| 41 |
st7 = "....MMMMMMMM...."; |
| 42 |
st6 = "....MMM..MMM...."; |
| 43 |
st5 = "...MMMMMM.MMMMM."; |
| 44 |
st4 = "..MM..MMMM..M.MM"; |
| 45 |
st3 = "MMM..MM..MMM...M"; |
| 46 |
st2 = "M...MM.....MMM.M"; |
| 47 |
st1 = "...MM........M.."; |
| 48 |
numeric i, j, hv[][][]; |
| 49 |
boolean b[][]; |
| 50 |
numeric refi, refj, pn, u, mar, numa, numb, numc; |
| 51 |
boolean startfound, forward, statever, pathfinished; |
| 52 |
path frontier[]; |
| 53 |
picture borderpic; |
| 54 |
color cola, colb, colc; |
| 55 |
pen thepen; |
| 56 |
u = 6mm; |
| 57 |
thepen = pencircle scaled 0.2u; |
| 58 |
mar = 0.3u; |
| 59 |
startfound = false; |
| 60 |
pn = 0; |
| 61 |
for j=1 upto 16: |
| 62 |
for i=1 upto 16: |
| 63 |
if substring (i-1,i) of st[j] = ".": |
| 64 |
b[i][j] = false; |
| 65 |
else: |
| 66 |
b[i][j] = true; |
| 67 |
fi; |
| 68 |
endfor; |
| 69 |
endfor; |
| 70 |
for i=0 upto 17: |
| 71 |
b[i][0] := false; |
| 72 |
b[i][17] := false; |
| 73 |
b[0][i] := false; |
| 74 |
b[17][i] := false; |
| 75 |
endfor; |
| 76 |
for j=0 upto 16: |
| 77 |
for i=0 upto 16: |
| 78 |
if b[i][j] xor b[i+1][j]: |
| 79 |
hv[1][i][j] = 1; |
| 80 |
draw strai( true, true, i, j ); |
| 81 |
else: |
| 82 |
hv[1][i][j] = 0; |
| 83 |
fi; |
| 84 |
if b[i][j] xor b[i][j+1]: |
| 85 |
hv[0][i][j] = 1; |
| 86 |
draw strai( false, true, i, j ); |
| 87 |
else: |
| 88 |
hv[0][i][j] = 0; |
| 89 |
fi; |
| 90 |
endfor; |
| 91 |
endfor; |
| 92 |
for i=0 upto 17: |
| 93 |
hv[0][17][i] := 0; |
| 94 |
hv[1][i][17] := 0; |
| 95 |
endfor; |
| 96 |
borderpic = currentpicture; |
| 97 |
endfig; |
| 98 |
|
| 99 |
def trioone( expr flstat, flforw ) = |
| 100 |
begingroup |
| 101 |
color tmpcolor; |
| 102 |
if flstat and flforw: |
| 103 |
tmpcolor = (0,0,0); |
| 104 |
elseif flstat and (not flforw): |
| 105 |
tmpcolor = (0,1,-1); |
| 106 |
elseif (not flstat) and flforw: |
| 107 |
tmpcolor = (1,0,1); |
| 108 |
else: |
| 109 |
tmpcolor = (1,-1,0); |
| 110 |
fi; |
| 111 |
( tmpcolor ) |
| 112 |
endgroup |
| 113 |
enddef; |
| 114 |
|
| 115 |
def triotwo( expr flstat, flforw ) = |
| 116 |
begingroup |
| 117 |
color tmpcolor; |
| 118 |
if flstat and flforw: |
| 119 |
tmpcolor = (1,0,1); |
| 120 |
elseif flstat and (not flforw): |
| 121 |
tmpcolor = (1,0,-1); |
| 122 |
elseif (not flstat) and flforw: |
| 123 |
tmpcolor = (0,1,0); |
| 124 |
else: |
| 125 |
tmpcolor = (0,-1,0); |
| 126 |
fi; |
| 127 |
( tmpcolor ) |
| 128 |
endgroup |
| 129 |
enddef; |
| 130 |
|
| 131 |
def triothr( expr flstat, flforw ) = |
| 132 |
begingroup |
| 133 |
color tmpcolor; |
| 134 |
if flstat and flforw: |
| 135 |
tmpcolor = (0,1,0); |
| 136 |
elseif flstat and (not flforw): |
| 137 |
tmpcolor = (0,0,-1); |
| 138 |
elseif (not flstat) and flforw: |
| 139 |
tmpcolor = (1,0,0); |
| 140 |
else: |
| 141 |
tmpcolor = (1,-1,1); |
| 142 |
fi; |
| 143 |
( tmpcolor ) |
| 144 |
endgroup |
| 145 |
enddef; |
| 146 |
|
| 147 |
beginfig(2); |
| 148 |
% draw borderpic; |
| 149 |
forever: |
| 150 |
startfound := false; |
| 151 |
for j=0 upto 16: |
| 152 |
for i=1 upto 16: |
| 153 |
if (not startfound) and (hv[0][i][j] = 1): |
| 154 |
startfound := true; |
| 155 |
refi := i; |
| 156 |
refj := j; |
| 157 |
fi; |
| 158 |
endfor; |
| 159 |
endfor; |
| 160 |
exitunless startfound; |
| 161 |
statever := false; |
| 162 |
forward := true; |
| 163 |
frontier[incr(pn)] = strai( statever, forward, refi, refj ); |
| 164 |
hv[0][refi][refj] := 2; |
| 165 |
pathfinished := false; |
| 166 |
forever: |
| 167 |
cola := trioone(statever,forward); |
| 168 |
colb := triotwo(statever,forward); |
| 169 |
colc := triothr(statever,forward); |
| 170 |
numa := hv[redpart cola][refi+greenpart cola][refj+bluepart cola]; |
| 171 |
numb := hv[redpart colb][refi+greenpart colb][refj+bluepart colb]; |
| 172 |
numc := hv[redpart colc][refi+greenpart colc][refj+bluepart colc]; |
| 173 |
if numa = 1: |
| 174 |
refi := refi+greenpart cola; |
| 175 |
refj := refj+bluepart cola; |
| 176 |
forward := statever xor forward; |
| 177 |
statever := not statever; |
| 178 |
frontier[pn] := frontier[pn]..strai( statever, forward, refi, refj ); |
| 179 |
hv[redpart cola][refi][refj] := 2; |
| 180 |
elseif numa = 2: |
| 181 |
frontier[pn] := frontier[pn]..cycle; |
| 182 |
pathfinished := true; |
| 183 |
elseif numb = 1: |
| 184 |
refi := refi+greenpart colb; |
| 185 |
refj := refj+bluepart colb; |
| 186 |
frontier[pn] := frontier[pn]..strai(statever,forward,refi,refj); |
| 187 |
hv[redpart colb][refi][refj] := 2; |
| 188 |
elseif numb = 2: |
| 189 |
frontier[pn] := frontier[pn]..cycle; |
| 190 |
pathfinished := true; |
| 191 |
elseif numc = 1: |
| 192 |
refi := refi+greenpart colc; |
| 193 |
refj := refj+bluepart colc; |
| 194 |
forward := not (statever xor forward); |
| 195 |
statever := not statever; |
| 196 |
frontier[pn] := frontier[pn]..strai( statever, forward, refi, refj ); |
| 197 |
hv[redpart colc][refi][refj] := 2; |
| 198 |
elseif numc = 2: |
| 199 |
frontier[pn] := frontier[pn]..cycle; |
| 200 |
pathfinished := true; |
| 201 |
fi; |
| 202 |
exitif pathfinished; |
| 203 |
endfor; |
| 204 |
renewcolore; |
| 205 |
fill frontier[pn] withpen thepen; |
| 206 |
endfor; |
| 207 |
show pn; |
| 208 |
endfig; |
| 209 |
|
| 210 |
end. |