Node: Drawing and Filling Paths Intro, Previous: Declaring and Initializing Paths, Up: Paths

Drawing and Filling Paths

The easiest way to draw a Path is with no arguments.

     Point pt[5];
     pt[0].set(-1, -2);
     pt[1].set(0, -3);
     pt[2].set(1, 0);
     pt[3].set(2, 1);
     pt[4].set(-1, 2);
     Path pa("..", true, &pt[0], &pt[1], &pt[2], &pt[3], &pt[4], 0);

[Figure 12. Not displayed.]

Fig. 12.

Since pa is closed, it can be filled as well as drawn. The following example uses fill() with a Color argument, in order to avoid having a large splotch of black on the page. Common Colors are declared in the namespace Colors. See Color Reference.


[Figure 13. Not displayed.]

Fig. 13.

Closed Paths can be filled and drawn, using the function filldraw(). This function draws the Path using the pen specified, or MetaPost's currentpen by default. A Color for drawing the Path can also be specified, otherwise, the default color (currently Colors::black) is used. In addition, the Path is filled using a second Color, which can be specified, or the background_color (Colors::background_color), by default. Filling a Path using the background color causes it to hide objects that lie behind it. See Surface Hiding, for a description of the surface hiding algorithm, and examples. Currently, this algorithm is quite primitive and only works for simple cases.

     Point p0(-3, 0, 1);
     Point p1(3, 1, 1);

[Figure 14. Not displayed.]

Fig. 14.

The following example uses arguments for the Colors used for drawing and filling, and the pen. The empty string argument before the pen argument is a placeholder for the dash pattern argument.

     pa.filldraw(black, gray, "",
        "pensquare xscaled 3mm yscaled 1mm rotated 60");

[Figure 15. Not displayed.]

Fig. 15.

Paths can also be "undrawn", "unfilled", and "unfilldrawn", using the corresponding functions:

     p0.undraw(p1, "", "pencircle scaled 3mm");

[Figure 16. Not displayed.]

Fig. 16.

     Path q;
     q = pa;
     q.scale(.5, .5);

[Figure 17. Not displayed.]

Fig. 17.

The function unfilldraw() takes a Color argument for drawing the Path, which is *Colors::background_color by default. This makes it possible to unfill the Path while drawing the outline with a visible Color. On the other hand, it also makes it necessary to specify *Colors::background_color or Colors::white, if the user wants to use the dash pattern and/or pen arguments, without drawing the Path.

     q.unfilldraw(white, "", "pensquare xscaled 3mm yscaled 1mm");

[Figure 18. Not displayed.]

Fig. 18.

The following example demonstrates the use of unfilldraw() with black as its Color argument. Unfortunately, it also demonstrates one of the limitations of the surface hiding algorith: The line from p0 to p1 is hidden by the filled Path pa. Since the portion of pa covered by Path q has been unfilled, the line from p_0 to p_1 should be visible as it passes through q. However, from the point of view of 3DLDF, there is no relationship between pa and q; nor does it "know" whether a Path has been filled or unfilled. If it's on a Picture, it will hide objects lying behind it, unless the surface hiding algorithm fails for another reason. See Surface Hiding, for more information.

     q.unfilldraw(black, "", "pensquare xscaled 3mm yscaled 1mm");

[Figure 19. Not displayed.]

Fig. 19.

See Paths; Drawing and Filling, for more information, and complete descriptions of the functions.