Como posso preencher um caminho complexo com Metapost/Metafun?

Como posso preencher um caminho complexo com Metapost/Metafun?

Existe alguma maneira de produzir exatamente o seguinte código PS e SVG com Metapost/ Metafun? Infelizmente, unfillnão funcionará para mim, pois há um plano de fundo na página resultante. E este exemplo é simplificado, pois o caminho real é muito mais complexo.

Saída postscript desejada:

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 100 100
%%BeginProlog
%%EndProlog%%Page: 1 1
newpath 0 100 moveto 100 100 lineto 100 0 lineto 0 0 lineto 0 100 lineto closepath
        10 90 moveto 10 10 lineto 90 10 lineto 90 90 lineto 10 90 lineto closepath
fill         
%%EOF

Saída SVG desejada:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="125" height="125">
 <g transform="matrix(1.25,0,0,-1.25,0,125)">
  <g transform="scale(0.1,0.1)">
   <path d="m 0,1000 1000,0 L 1000,0 0,0 0,1000 z m 100,-100 0,-800 800,0 0,800 -800,0" id="3"
         style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" />
  </g>
 </g>
</svg>

Responder1

Existe um truque padrão para fazer isso no MetaPost. Decidi incluir a solução porque o preenchimento do MetaPost realmente não preenche nada, mas preenche com a cor de fundo.

O seguinte mostra a solução.

beginfig( 0 )

path clockwisepath, anticlockwisepath;
clockwisepath := (0,100) -- (100,100) -- (100,0) -- (0,0) -- (0,100) -- cycle;
anticlockwisepath := (10,90) -- (10,10) -- (90,10) -- (90,90) -- (10,90) -- cycle;

fill clockwisepath -- anticlockwisepath -- cycle;

endfig;

end.

A seguir está o eps produzido:

%!PS
%%BoundingBox: 0 0 100 100 
%%HiResBoundingBox: 0 0 100 100 
%%Creator: MetaPost 1.902
%%CreationDate: 2014.12.19:1511
%%Pages: 1
%%BeginProlog
%%EndProlog
%%Page: 1 1
 0 0 0 setrgbcolor
newpath 0 100 moveto 100 100 lineto 100 0 lineto 0 0 lineto 0 100 lineto
        0 100 lineto 10 90 lineto 10 10 lineto 90 10 lineto 90 90 lineto
        10 90 lineto 10 90 lineto
 closepath fill
showpage
%%EOF

Observe que o código não produz exatamente o mesmo eps, mas para preenchê-lo realmente não importa.

Responder2

Preencher e descarregar:

%% naid.mp
u:=1bp;
beginfig(1);
fill (0,0)--(100u,0)--(100u,100u)--(0,100u)--cycle;
unfill (10u,10u)--(90u,10u)--(90u,90u)--(10u,90u)--cycle;
endfig;
end.

Aqui está a saída de mpost naid.mp:

%!PS
%%BoundingBox: 0 0 100 100 
%%HiResBoundingBox: 0 0 100 100 
%%Creator: MetaPost 1.803
%%CreationDate: 2013.12.23:1356
%%Pages: 1
%%BeginProlog
%%EndProlog
%%Page: 1 1
 0 0 0 setrgbcolor
newpath 0 0 moveto
100 0 lineto
100 100 lineto
0 100 lineto
 closepath fill
 1 1 1 setrgbcolor
newpath 10 10 moveto
90 10 lineto
90 90 lineto
10 90 lineto
 closepath fill
showpage
%%EOF

e aqui está a saída de

mpost '\outputformat:="svg";outputtemplate:="%j-%c.svg";input naid.mp'

no arquivonaid-1.svg

<?xml version="1.0"?>
<!-- Created by MetaPost 1.803 on 2013.12.23:1445 -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100.000000" height="100.000000" viewBox="0 0 100.000000 100.000000">
<!-- Original BoundingBox: 0.000000 0.000000 100.000000 100.000000 -->
  <path d="M0.000000 100.000000L100.000000 100.000000L100.000000 -0.000000L0.000000 -0.000000Z" style="fill: rgb(0.000000%,0.000000%,0.000000%);stroke: none;"></path>
  <path d="M10.000000 90.000000L90.000000 90.000000L90.000000 10.000000L10.000000 10.000000Z" style="fill: rgb(100.000000%,100.000000%,100.000000%);stroke: none;"></path>
</svg>

Responder3

Nunca usei o Metapost, mas como o Asymptote é baseado nele, uma resposta do Asymptote pode ajudar alguém a descobrir uma resposta do Metapost.

Faça um arquivo foo.asycom o seguinte conteúdo:

fill((0,100)--(100,100)--(100,0)--(0,0)--cycle ^^ (10,90)--(10,10)--(90,10)--(90,90)--cycle);

A execução asy foo.asya partir da linha de comando fornece um arquivo eps foo.epscom o seguinte conteúdo (módulo com algumas diferenças óbvias, por exemplo, horário de criação):

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 255 345 356 446
%%HiResBoundingBox: 255.5 345.5 355.5 445.5
%%Creator: Asymptote 2.23
%%CreationDate: 2013.12.23 10:10:49
%%Pages: 1
%%Page: 1 1
/Setlinewidth {0 exch dtransform dup abs 1 lt {pop 0}{round} ifelse
idtransform setlinewidth pop} bind def
gsave
 255.5 345.5 translate
newpath 0 100 moveto
 100 100 lineto
 100 0 lineto
 0 0 lineto
 0 100 lineto
closepath
 10 90 moveto
 10 10 lineto
 90 10 lineto
 90 90 lineto
 10 90 lineto
closepath
0 setgray
0.5 Setlinewidth
1 setlinecap
1 setlinejoin
10 setmiterlimit
fill
grestore
showpage
%%EOF

O resultado como um gráfico:

insira a descrição da imagem aqui

informação relacionada