Wie kann ich einen komplexen Pfad mit Metapost/Metafun füllen?

Wie kann ich einen komplexen Pfad mit Metapost/Metafun füllen?

Gibt es eine Möglichkeit, mit Metapost/ genau den folgenden PS- und SVG-Code zu erstellen Metafun? Leider unfillfunktioniert das bei mir nicht, da die resultierende Seite einen Hintergrund enthält. Und dieses Beispiel ist abgespeckt, da der tatsächliche Pfad viel komplexer ist.

Gewünschte Postscript-Ausgabe:

%!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

Gewünschte SVG-Ausgabe:

<?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>

Antwort1

In MetaPost gibt es hierfür einen Standardtrick. Ich habe mich entschieden, die Lösung einzuschließen, da MetaPosts Unfill eigentlich nichts ungefüllt macht, sondern mit der Hintergrundfarbe füllt.

Nachfolgend sehen Sie die Lösung.

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.

Das Folgende sind die erstellten EPS:

%!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

Beachten Sie, dass der Code nicht genau dasselbe EPS erzeugt, aber zum Füllen spielt das wirklich keine Rolle.

Antwort2

Füllen und Entfüllen:

%% 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.

Hier ist die Ausgabe von 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

und hier ist die Ausgabe von

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

im Ordnernaid-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>

Antwort3

Ich habe Metapost noch nie verwendet, aber da Asymptote darauf basiert, könnte eine Asymptote-Antwort möglicherweise jemandem dabei helfen, eine Metapost-Antwort zu finden.

Erstellen Sie eine Datei foo.asymit folgendem Inhalt:

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

Das Ausführen asy foo.asyüber die Kommandozeile ergibt dann eine EPS-Datei foo.epsmit folgendem Inhalt (modulo einiger offensichtlicher Unterschiede, z. B. Erstellungszeit):

%!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

Das Ergebnis als Grafik:

Bildbeschreibung hier eingeben

verwandte Informationen