4
beginfig(1)
input hatching;
      u := 5 mm;
      path xtelg,ytelg;
  xtelg := (-4u,0) -- (6u,0);
  ytelg := (0,-4u) -- (0,9u);
  drawarrow xtelg;
  drawarrow ytelg;
  path parab, paraa,m,n,kujund;
  parab:= (0u,8u) for i=0.5 step .5 until 6: .. (i*u, (i*i-6*i+8)*u) endfor;
  paraa:= (-3u,5u) for i=-2 step .5 until 3: ..(i*u,(i*i-4)*u) endfor;
  draw parab;
  draw paraa;
  m:=(0,-4u)--(0,8u) for i=0 step 0.5 until 2: ..(i*u,(i**2-6*i+8)*u) endfor;
  n:=(0,-4u) for i=0 step 0.5 until 2: ..(i*u,(i**2-4)*u) endfor;
  kujund:=buildcycle(m,n);
  hatchfill kujund withcolor(45,1mm,.5bp);
  draw kujund;
endfig;
end

Like I mentioned above, I don't know why the hatchfill won't fill my graph.

2 Answers2

4

Instead of

kujund:=buildcycle(m, n);

try

kujund:=buildcycle(reverse m, n);

or

kujund:=buildcycle(m, reverse n);

Either way, it works for me:

enter image description here

By the way, the buildcycle macro can be tricky if used with only two paths. See the Metapost documentation p. 30-32 for more details about how it works. Given this way of working, it seems that by asking

buildcycle(m,n)

it leads Metapost to build a cycle between either only two points (I guess (0,-4) and the intersection of the two parabolas) or even only one point (I suppose (0,-4)) hence the absence of apparent result. Whereas

buildcycle(m, reverse n)

leads it to build the requested cycle between the expected intersection points.

Franck Pastor
  • 18,756
  • note that if you really want hatching, you need to make the third components of the "color" negative. The fill is coming out as pale yellow here because the color (45, 1mm, 0.5bp) is being interepreted as (1, 1, 0.5). – Thruston Mar 02 '20 at 14:12
1

Here's a slightly simpler version that gets rid of the temporary paths m and n, and uses the y-axis directly in the buildcycle command (which also gets rid of the need to reverse one of the paths).

prologues := 3;
outputtemplate := "%j%c.eps";

input hatching

beginfig(1);
    numeric u;
    u = 5 mm;

    path xtelg,ytelg;
    xtelg = (4 left -- 6 right) scaled u;
    ytelg = (4 down -- 9 up) scaled u;

    path parab, paraa, kujund;
    parab = ((0, 8) for i=1/2 step 1/2 until 6: .. (i, i**2 - 6i + 8) endfor) scaled u;
    paraa = ((-3, 5) for i=-2 step 1/2 until 3: .. (i, i**2 - 4) endfor) scaled u;

    kujund = buildcycle(ytelg, parab, paraa);

    drawoptions(withcolor 1/2 white);
    hatchfill kujund withcolor (45, 3, -1/2);
    drawoptions();

    draw parab;
    draw paraa;
    drawarrow xtelg;
    drawarrow ytelg;

endfig;
end.

enter image description here

Thruston
  • 42,268