Draw border of a line

You can over-draw it using a pen with the background colour and a slightly narrower width.

Here's an example: (compile with asy or integrate into your existing LaTeX doc).

unitsize(1cm);
guide sausage = (0,0) -- (2,3);
draw(sausage, black+5);
draw(sausage, white+4.2);

this produces:

enter image description here

The draw(*guide*, *pen*) command draws the guide with the specified pen. To set the width of your pen, you just add a real number to an existing pen. In this case I've used the predefined (coloured) pens black and white. Adding 5 to black makes a pen that's 5 PostScript points wide. If you wanted one that's 5mm wide then you could do black+5mm thanks to the clever way that Asymptote deals with units.


I have taken the idea in this post, using the graph package to construct the path[] getBorder(path, pen) function as shown in the following code.

unitsize(1inch);
import graph;

path[] getBorder(path p, pen origPen)
{
    real offset = linewidth(origPen)/2.0/72.0;
    pair offsetPoint(real t) { return point(p, t) + offset*(rotate(90)*dir(p,t)); }
    path path1 = graph(offsetPoint, 0, length(p), operator ..);
    offset = -offset;
    path path2 = graph(offsetPoint, 0, length(p), operator ..);

    path[] paths;
    if (cyclic(p)) { paths = path1^^path2; }
    else { paths = path1..reverse(path2)..cycle; }

    return paths;
}

pen origPen = 10+black;

draw(getBorder((0,0){E}..{S}(2,0), origPen), red);
draw(getBorder((0,1)--(1,0), origPen), blue);
draw(getBorder(shift(1,0.5)*scale(0.5)*unitcircle, origPen), green);

enter image description here

The code works for cyclic or non-cyclic paths. The origPen definition determines the resulting width of the path outline. The function assumes that the origPen is defined with a roundcap.

Unfortunately, the nature of the function means that sharp corners or small radii will not be treated properly. Maybe someone else knows how to overcome this problem.

enter image description here