# MetaPost: How to fill cycle path with slanted lines?

Suppose I have build a cycle path with bulidcycle:

path pa[];
pa.0:=(-6u,0){dir 90} .. (0,8u){dir 0};

pa.1:=(-6u,0){dir -90} .. (0,-3u){dir 0};

pa.2:=(3u,-6u)--(3u,12u);

pa.3:=buildcycle(pa.01,pa.11,pa.2);

draw pa.3 withcolor red;


Then it is quite easy to fill it with color,

fill pa.3 withcolor red;


My problem is how to fill it with slanted lines?

Can we define a function, named as draw_fill(expr dr, pa), which will fill the path pa with lines in the direction dr?

## EDIT

After a while, I figure out that we can do it by clip as: first to define a function called draw_clip,

def draw_clip(expr pat, len, gap) =
begingroup
u:=10pt;
for i=-len upto len:
draw (-len*u*dir(40)--len*u*dir(40)) shifted (0,i*u*gap);
endfor;
clip currentpicture to pat;
endgroup;
enddef;


Then before any draw, do the clip:

draw_clip(pa.3,20,.5,40);


maybe this is a not so perfect solution (as you must clip it first before anyother draw). So my question can be changed into what is the best way to do it?

Another small question will be that if you want to mark the region by a label, then the label need to be changed into something like

%fix the label
picture lab;
lab=thelabel.rt(btex $\Omega$ etex, (4u, 2u));
unfill bbox lab;
draw lab

• “Fill” a wider rectangular region by repeating the line and then clip with the path. Commented Jan 21, 2014 at 8:33
• B. Jackowski's hatching package could be useful for this: ctan.org/pkg/hatching Commented Jan 21, 2014 at 15:40

Yet others examples with the hatchingpackage (B. Jackowski), package I know better by now. It is fairly simple since it redefines the withcolor primitive in such a way that its rgb parameters become hatching parameters: slope angle, space between consecutive hatching lines, line width. This last parameter must be made negative, to avoid confusion with a real color parameter.

input hatching;
u := 1cm;
path circle; circle = fullcircle scaled 10u;
beginfig(1);
hatchfill circle withcolor (45, 5mm, -.5bp);
draw circle;
endfig;
end.


Of course one can specify colors and other parameters for hatching, using the hatchingoptionscommand (similar to the stronger drawoptions command that can be used instead):

input hatching;
u := 1cm;
path circle; circle = fullcircle scaled 10u;
beginfig(2);
hatchoptions (withcolor red dashed evenly);
hatchfill circle withcolor (-45, 7mm, -.5bp);
draw circle;
endfig;
end.


hatching can also use arbitrary patterns instead of straight lines, but I've not tried these yet.

Hope that helps!

You can use the image operator to avoid manipulating currentpicture. Here's an example that shows how to fill a random cyclic path with slanted lines.

beginfig(1);
% make the filler
theta := 20; gap := 8; N := 10;
picture lines; lines = image(
for i=-N upto N:
draw ((left--right) scaled 100) rotated theta shifted (gap*i*up);
endfor );

% make a random shape
z0 = right scaled 50;
path shape; shape = z0 for i=1 upto 9: .. (z0 rotated 36i)+(uniformdeviate 10, uniformdeviate 10) endfor .. cycle;

% clip the filler to the shape and draw it, then draw the shape
clip lines to shape;
draw lines withcolor .674 red withpen pencircle scaled 0.1bp;
draw shape;
endfig;


which makes this:

And here's another example with a regular shape but a more random pattern.

beginfig(2);
a := 3; L := 30;
picture waves; waves = image( for j=-20 upto 20:
draw ((-10L,0) for i=-9 upto 10: .. (i*L-3/4L,+a+normaldeviate){right}
.. (i*L-1/4L,-a+normaldeviate){right} .. (i*L,0)
endfor) shifted (uniformdeviate 1/5L, 3.5*a*j); endfor);

path shape; shape = fullcircle scaled 100;

clip waves to shape;
draw waves withcolor .66blue;
draw shape;
endfig;


which makes:

• If I change your code into a function, why is not work in mpgraphics? I find it work in a single file (as function), but not work in mpgraphics. Commented Jan 21, 2014 at 13:40
• @user19832 I tried the code both with mpgraphics and gmp, it works. You shouldn't add beginfig(1); and endfig; when you use MP code in those packages. Commented Jan 21, 2014 at 15:55

Just another solution with PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}(8,6)
\psset{fillstyle=vlines}
\pscircle(2,2){1}
\psccurve(6,5)(5,3)(4,2)(4,1)(7,3)(7,6)
\end{pspicture}
\end{document}